Ejemplo n.º 1
0
        internal Teritory(EnglishNames names, string id, Dictionary <string, string[]> dir)
        {
            this.id = id;
            name    = names.Teritories[id];
            var values = dir[id];

            foreach (var v in values)
            {
                if (char.IsDigit(v[0]))
                {
                    if (childs == null)
                    {
                        childs = new List <Teritory>();
                    }
                    childs.Add(new Teritory(names, v, dir));
                }
                else
                {
                    if (regions == null)
                    {
                        regions = new List <Region>();
                    }
                    regions.Add(new Region {
                        id = v, name = names.Teritories[v]
                    });
                }
            }
        }
Ejemplo n.º 2
0
    static EnglishNames englishNames()
    {
        // missing trans from comments in supplements, e.g.
        // <languagePopulation type="wbp" populationPercent="0.011"/>	<!--Warlpiri-->
        var missing = allLangs.Except(langTrans.Keys).ToHashSet();
        var els     = supplements.
                      Descendants("languagePopulation").
                      Select(el => new { el, lang = el.Attribute("type").Value }).Where(el => missing.Contains(el.lang)).Select(el => new {
            el.lang, name = (el.el.NextNode as XComment).Value
        }).
                      ToArray();

        foreach (var kv in els)
        {
            langTrans[kv.lang] = kv.name;
        }

        var english = new EnglishNames {
            Teritories = trans("territories"),
            Langs      = langTrans,
            Scripts    = trans("scripts"),
        };

        Json.Serialize(LangsDesignDirs.root + @"patches\cldrEnglish.json", english);

        return(english);
    }
Ejemplo n.º 3
0
        internal static void createTeritoryTree(EnglishNames names)
        {
            var ters = supplements.
                       Descendants("territoryContainment").
                       Single().Elements("group").
                       Where(g => g.Attribute("status") == null && g.Attribute("grouping") == null).
                       ToDictionary(el => el.Attribute("type").Value, el => el.Attribute("contains").Value.Split(' '));

            // testUniq is empty => every region is unique in tree
            var testUniq = ters.SelectMany(kv => kv.Value.Select(Value => new { kv.Key, Value })).GroupBy(v => v.Value).Where(g => g.Count() > 1);

            if (testUniq.Count() > 0)
            {
                throw new Exception();
            }

            // build tree
            var root = new Teritory(names, "001", ters);

            Json.Serialize(LangsDesignDirs.root + @"patches\cldrTeritoryTree.json", root);
        }
Ejemplo n.º 4
0
    static void computeLangRegionEnglish(EnglishNames english)
    {
        var scripts = extractScript().ToDictionary(
            s => s.lang,
            s => s.script + (s.other == null ? "" : "|" + string.Join(",", s.other)));

        // **************************
        // ***** REGION-LANG table: <region,lang>,<population,isOfficial,script>
        var regionLang = supplements.Descendants("territoryInfo").
                         Single().
                         Elements("territory").
                         SelectMany(el => {
            var allPop = int.Parse(el.Attribute("population").Value);
            var region = el.Attribute("type").Value;
            return(el.Elements("languagePopulation").
                   Select(ee => {
                string lang = ee.Attribute("type").Value, key1 = $"{lang}|{region}", key2 = $"{lang}|";
                return new regLang {
                    region = region,
                    lang = lang,
                    population = (int)Math.Round((double)allPop * float.Parse(ee.Attribute("populationPercent")?.Value ?? "0", CultureInfo.InvariantCulture) / 100),
                    isOfficial = ee.Attribute("officialStatus") != null,
                };
            }));
        }).
                         ToArray();

        // **************************
        // ***** population grouping

        // group and sum by <region> or <lang>
        void groupAndSum(regLang[] data, bool byReg, bool noOffOnly = false)
        {
            string friendlyName(regLang d, bool isKey, bool sh = false)
            {
                var actId = byReg == isKey ? d.region : d.lang;

                if (sh)
                {
                    return(actId);
                }
                var actNames = byReg == isKey ? english.Teritories : english.Langs;

                return(actNames[actId]);
            }

            string stringDetail(IGrouping <string, regLang> g, bool officialOnly)
            {
                var sd = string.Join(", ", g.Where(l => officialOnly == l.isOfficial && l.population > 0).
                                     OrderByDescending(l => l.population).
                                     Select(l => $"{friendlyName(l, false, false)}:{friendlyName(l, false, true)}:{l.population}"));

                return(sd == "" ? null : sd);
            }

            var by = byReg ? data.GroupBy(d => d.region) : data.GroupBy(d => d.lang);

            var res = by.Select(g => new LangOrRegion {
                name      = friendlyName(g.First(), true),
                id        = g.Key,
                offSum    = g.Where(l => l.isOfficial).Sum(l => l.population),
                nonOffSum = g.Where(l => !l.isOfficial).Sum(l => l.population),
                off       = stringDetail(g, true),
                nonOff    = stringDetail(g, false),
                likely    = byReg ? null : LocaleIdentifier.Parse(g.Key).MostLikelySubtags().ToString(),
                scripts   = byReg ? null : scripts[g.Key],
            }).
                      Where(r => r.id != "und" && (noOffOnly ? r.offSum == 0 : r.offSum + r.nonOffSum > 0)).
                      OrderByDescending(r => r.offSum + r.nonOffSum).
                      ToArray();

            var nm = byReg ? "Regions" : (noOffOnly ? "LangsNotOfficial" : "Langs");

            Json.Serialize(LangsDesignDirs.root + $"patches\\cldr{nm}.json", res);
        }

        groupAndSum(regionLang, true);
        groupAndSum(regionLang, false, true);
        groupAndSum(regionLang, false);
    }