Ejemplo n.º 1
0
        public AssemblyResultTS Sync(string assembly, string culture, string? @namespace = null)
        {
            Assembly    ass           = AssembliesToLocalize().Where(a => a.GetName().Name == assembly).SingleEx(() => "Assembly {0}".FormatWith(assembly));
            CultureInfo targetCulture = CultureInfo.GetCultureInfo(culture);

            CultureInfo defaultCulture = CultureInfo.GetCultureInfo(ass.GetCustomAttribute <DefaultAssemblyCultureAttribute>().DefaultCulture);

            var cultures = TranslationLogic.CurrentCultureInfos(defaultCulture);
            Dictionary <CultureInfo, LocalizedAssembly> reference = (from ci in cultures
                                                                     let la = DescriptionManager.GetLocalizedAssembly(ass, ci)
                                                                              where la != null || ci == defaultCulture || ci == targetCulture
                                                                              select KVP.Create(ci, la ?? LocalizedAssembly.ImportXml(ass, ci, forceCreate: ci == defaultCulture || ci == targetCulture))).ToDictionary();

            var master  = reference.Extract(defaultCulture);
            var target  = reference.Extract(targetCulture);
            var changes = TranslationSynchronizer.GetAssemblyChanges(TranslationServer.Translator, target, master, reference.Values.ToList(), null, @namespace, out int totalTypes);

            return(new AssemblyResultTS
            {
                totalTypes = totalTypes,
                cultures = cultures.Select(c => c.ToCulturesTS()).ToDictionary(a => a.name),
                types = changes.Types.Select(t => t.Type.Type.ToLocalizableTypeTS().Let(localizedTypes =>
                {
                    localizedTypes.cultures = cultures.ToDictionary(c => c.Name, c => GetLocalizedType(t, c, c.Equals(targetCulture)));
                    return localizedTypes;
                })).ToDictionary(lt => lt.type),
            });
        }
Ejemplo n.º 2
0
        public List <NamespaceSyncStats> SyncStats(string assembly, string culture)
        {
            Assembly    ass            = AssembliesToLocalize().Where(a => a.GetName().Name == assembly).SingleEx(() => "Assembly {0}".FormatWith(assembly));
            CultureInfo targetCulture  = CultureInfo.GetCultureInfo(culture);
            CultureInfo defaultCulture = CultureInfo.GetCultureInfo(ass.GetCustomAttribute <DefaultAssemblyCultureAttribute>().DefaultCulture);

            var targetAssembly  = DescriptionManager.GetLocalizedAssembly(ass, targetCulture) ?? LocalizedAssembly.ImportXml(ass, targetCulture, forceCreate: true) !;
            var defaultAssembly = DescriptionManager.GetLocalizedAssembly(ass, defaultCulture) ?? LocalizedAssembly.ImportXml(ass, defaultCulture, forceCreate: true) !;

            return(TranslationSynchronizer.SyncNamespaceStats(targetAssembly, defaultAssembly));
        }
Ejemplo n.º 3
0
    public static object GetCurrentValidCulture()
    {
        var ci = CultureInfo.CurrentCulture;

        while (ci != CultureInfo.InvariantCulture && !EntityAssemblies.Keys.Any(a => DescriptionManager.GetLocalizedAssembly(a, ci) != null))
        {
            ci = ci.Parent;
        }

        return(ci != CultureInfo.InvariantCulture ? ci : CultureInfo.GetCultureInfo("en"));
    }
Ejemplo n.º 4
0
    private IEnumerable <KeyValuePair <string, string> > GetAllTranslations(Assembly assembly, string from, string to)
    {
        var locFrom = DescriptionManager.GetLocalizedAssembly(assembly, CultureInfo.GetCultureInfo(from));
        var locTo   = DescriptionManager.GetLocalizedAssembly(assembly, CultureInfo.GetCultureInfo(to));

        if (locFrom == null || locTo == null)
        {
            return(Enumerable.Empty <KeyValuePair <string, string> >());
        }

        return((locFrom.Types.JoinDictionary(locTo.Types, (type, ft, tt) => GetAllTranslations(ft, tt))).Values.SelectMany(pairs => pairs));
    }
Ejemplo n.º 5
0
        public ActionResult View(string assembly, string culture, bool searchPressed, string filter)
        {
            Assembly ass = AssembliesToLocalize().Where(a => a.GetName().Name == assembly).SingleEx(() => "Assembly {0}".FormatWith(assembly));

            CultureInfo defaultCulture = CultureInfo.GetCultureInfo(ass.GetCustomAttribute <DefaultAssemblyCultureAttribute>().DefaultCulture);
            CultureInfo targetCulture  = culture == null ? null : CultureInfo.GetCultureInfo(culture);

            Dictionary <CultureInfo, LocalizedAssembly> reference = !searchPressed ? null :
                                                                    (from ci in TranslationLogic.CurrentCultureInfos(defaultCulture)
                                                                     let la = DescriptionManager.GetLocalizedAssembly(ass, ci)
                                                                              where la != null || ci == defaultCulture || ci == targetCulture
                                                                              select KVP.Create(ci, la ?? LocalizedAssembly.ImportXml(ass, ci, forceCreate: true))).ToDictionary();

            ViewBag.filter         = filter;
            ViewBag.searchPressed  = searchPressed;
            ViewBag.Assembly       = ass;
            ViewBag.DefaultCulture = defaultCulture;
            ViewBag.Culture        = targetCulture;

            return(base.View(TranslationClient.ViewPrefix.FormatWith("View"), reference));
        }
Ejemplo n.º 6
0
        private TranslatedSummaryState CalculateStatus(Assembly a, CultureInfo ci)
        {
            var fileName = LocalizedAssembly.TranslationFileName(a, ci);

            if (!System.IO.File.Exists(fileName))
            {
                return(TranslatedSummaryState.None);
            }

            var target = DescriptionManager.GetLocalizedAssembly(a, ci) !;

            CultureInfo defaultCulture = CultureInfo.GetCultureInfo(a.GetCustomAttribute <DefaultAssemblyCultureAttribute>().DefaultCulture);
            var         master         = DescriptionManager.GetLocalizedAssembly(a, defaultCulture) !;

            var result = TranslationSynchronizer.GetMergeChanges(target, master, new List <LocalizedAssembly>());

            if (result.Any())
            {
                return(TranslatedSummaryState.Pending);
            }

            return(TranslatedSummaryState.Completed);
        }
Ejemplo n.º 7
0
        public TranslatedSummaryState Status(Lite <RoleEntity> role)
        {
            if (!System.IO.File.Exists(FileName))
            {
                return(TranslatedSummaryState.None);
            }

            var target = DescriptionManager.GetLocalizedAssembly(Assembly, CultureInfo);

            CultureInfo defaultCulture = CultureInfo.GetCultureInfo(Assembly.GetCustomAttribute <DefaultAssemblyCultureAttribute>().DefaultCulture);
            var         master         = DescriptionManager.GetLocalizedAssembly(Assembly, defaultCulture);

            var result = TranslationSynchronizer.GetMergeChanges(target, master, new List <LocalizedAssembly>());


            if (result.Any(r => role == null || TranslationLogic.GetCountNotLocalizedMemebers(role, CultureInfo, r.Type.Type) > 0))
            {
                return(TranslatedSummaryState.Pending);
            }


            return(TranslatedSummaryState.Completed);
        }
Ejemplo n.º 8
0
        public ActionResult Sync(string assembly, string culture, bool translatedOnly, Lite <RoleEntity> role)
        {
            Assembly    ass           = AssembliesToLocalize().Where(a => a.GetName().Name == assembly).SingleEx(() => "Assembly {0}".FormatWith(assembly));
            CultureInfo targetCulture = CultureInfo.GetCultureInfo(culture);

            CultureInfo defaultCulture = CultureInfo.GetCultureInfo(ass.GetCustomAttribute <DefaultAssemblyCultureAttribute>().DefaultCulture);

            Dictionary <CultureInfo, LocalizedAssembly> reference = (from ci in TranslationLogic.CurrentCultureInfos(defaultCulture)
                                                                     let la = DescriptionManager.GetLocalizedAssembly(ass, ci)
                                                                              where la != null || ci == defaultCulture || ci == targetCulture
                                                                              select KVP.Create(ci, la ?? LocalizedAssembly.ImportXml(ass, ci, forceCreate: true))).ToDictionary();
            var master = reference.Extract(defaultCulture);

            var target = reference.Extract(targetCulture);

            DictionaryByTypeName(target); //To avoid finding duplicated types on save
            int totalTypes;
            var changes = TranslationSynchronizer.GetAssemblyChanges(TranslationClient.Translator, target, master, reference.Values.ToList(), role, null, out totalTypes);

            ViewBag.Role       = role;
            ViewBag.TotalTypes = totalTypes;
            ViewBag.Culture    = targetCulture;
            return(base.View(TranslationClient.ViewPrefix.FormatWith("Sync"), changes));
        }
Ejemplo n.º 9
0
        public AssemblyResultTS Retrieve(string assembly, string culture, string filter)
        {
            Assembly ass = AssembliesToLocalize().Where(a => a.GetName().Name == assembly).SingleEx(() => "Assembly {0}".FormatWith(assembly));

            CultureInfo defaultCulture = CultureInfo.GetCultureInfo(ass.GetCustomAttribute <DefaultAssemblyCultureAttribute>().DefaultCulture);
            CultureInfo?targetCulture  = culture == null ? null : CultureInfo.GetCultureInfo(culture);

            var cultures = TranslationLogic.CurrentCultureInfos(defaultCulture);

            Dictionary <string, LocalizableTypeTS> types =
                (from ci in cultures
                 let la = DescriptionManager.GetLocalizedAssembly(ass, ci)
                          where la != null || ci == defaultCulture || ci == targetCulture
                          let la2 = la ?? LocalizedAssembly.ImportXml(ass, ci, forceCreate: true)
                                    from t in la2.Types.Values
                                    let lt = new LocalizedTypeTS
            {
                culture = ci.Name,
                typeDescription = new LocalizedDescriptionTS
                {
                    gender = t.Gender?.ToString(),
                    description = t.Description,
                    pluralDescription = t.PluralDescription,
                },
                members = t.Members.Select(kvp => new LocalizedMemberTS {
                    name = kvp.Key, description = kvp.Value
                }).ToDictionary(a => a.name),
            }
                 group lt by t.Type into g
                 select KVP.Create(g.Key.Name, g.Key.ToLocalizableTypeTS().Let(localizedTypes =>
            {
                localizedTypes.cultures = g.ToDictionary(a => a.culture);
                return(localizedTypes);
            })))
                .ToDictionaryEx("types");


            types.ToList().ForEach(lt => lt.Value.FixMembers(defaultCulture));

            if (filter.HasText())
            {
                var complete = types.Extract((k, v) => v.type.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
                                             v.cultures.Values.Select(a => a.typeDescription !).Any(td =>
                                                                                                    td.description != null && td.description.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
                                                                                                    td.pluralDescription != null && td.pluralDescription.Contains(filter, StringComparison.InvariantCultureIgnoreCase)));


                var filtered = types.Extract((k, v) =>
                {
                    var allMembers = v.cultures.Values.SelectMany(a => a.members.Keys).Distinct().ToList();

                    var filteredMembers = allMembers.Where(m => m.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
                                                           v.cultures.Values.Any(lt => lt.members.GetOrThrow(m).description?.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ?? false))
                                          .ToList();

                    if (filteredMembers.Count == 0)
                    {
                        return(false);
                    }

                    foreach (var item in v.cultures.Values)
                    {
                        item.members = item.members.Where(a => filteredMembers.Contains(a.Key)).ToDictionary();
                    }

                    return(true);
                });

                types = complete.Concat(filtered).ToDictionary();
            }

            return(new AssemblyResultTS
            {
                types = types.OrderBy(a => a.Key).ToDictionary(),
                cultures = cultures.Select(c => c.ToCulturesTS())
                           .ToDictionary(a => a.name)
            });
        }