How to find the cldr name from Microsoft CultureInfo in javascript?

800 views Asked by At

I want to know if there is somewhere a defined mapping from the Microsoft CultureInfo (which could be looked up here MS-LCID (Windows Language Code ID)) to the Unicode cldr Language Code.

I am currently using jQuery and globalize.js to validate the user input of our asp.net-core site. Our implementation looks similar to this example validationScript.cshtml (asp.net-core code)

We only had to change the script section like this:

<script type="text/javascript">
var culture = "@System.Globalization.CultureInfo.CurrentUICulture";

$.when(
  $.get("/lib/newTestLocalization/cldr-core/supplemental/likelySubtags.json"),
  $.get("/lib/newTestLocalization/cldr-numbers-modern/main/" + culture + "/numbers.json"),
  $.get("/lib/newTestLocalization/cldr-core/supplemental/numberingSystems.json"),
  $.get("/lib/newTestLocalization/cldr-core/supplemental/timeData.json"),
  $.get("/lib/newTestLocalization/cldr-core/supplemental/weekData.json")
).then(function() {
    console.log("sucessfully loaded cldr data");
    // Normalize $.get results, we only need the JSON, not the request statuses.
    return [].slice.apply(arguments, [0]).map(function(result) {
      return result[0];
    });
  },
  function() { console.log("Error  loading cldr data!"); }
).then(Globalize.load, function ()
  { console.log("Error  loading cldr data!"); }
).then(function () {
  Globalize.locale(culture);

  console.log("finished Globalize.locale !");
}); 

</script>

If i switch the site to one of the following :

  1. CultureInfo("zh-CHS")
  2. CultureInfo("zh-CHT")
  3. CultureInfo("de-DE")
  4. CultureInfo("ja-JP")
  5. CultureInfo("en-US")

the globalize.js is not working because there is no cldr folder for any of the language IDs above.

I looked it up here cldr-numbers-full/main/ (JSON data for CLDR 33 release), but could not find any of the IDs above.

So my question is: "Is there somewhere a defined mapping from MS-LCIDs to cldr-IDs, if this is a right question to ask?

And my second question is: what is the current standard/best-practice to use?

  1. MS-LCIDs
  2. cldr IDs
  3. Or one of these here (IETF language tag)
  4. or these (ISO 639)
  5. or ...
1

There are 1 answers

0
user1911091 On BEST ANSWER

Finally found the solution here

At the end of _ValidationScriptsPartial.cshtml add the following code.

@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment HostingEnvironment
@{
    string GetDefaultLocale()
    {
        const string localePattern = "lib\\cldr-data\\main\\{0}";
        var currentCulture = System.Globalization.CultureInfo.CurrentCulture;
        var cultureToUse = "en-GB"; //Default regionalisation to use

        if (System.IO.Directory.Exists(System.IO.Path.Combine(HostingEnvironment.WebRootPath, string.Format(localePattern, currentCulture.Name))))
            cultureToUse = currentCulture.Name;
        else if (System.IO.Directory.Exists(System.IO.Path.Combine(HostingEnvironment.WebRootPath, string.Format(localePattern, currentCulture.TwoLetterISOLanguageName))))
            cultureToUse = currentCulture.TwoLetterISOLanguageName;

        return cultureToUse;
    }
}

<script type="text/javascript">
    var culture = "@GetDefaultLocale()";
    $.when(
        $.get("/lib/cldr-data/supplemental/likelySubtags.json"),
        $.get("/lib/cldr-data/main/" + culture + "/numbers.json"),
        $.get("/lib/cldr-data/supplemental/numberingSystems.json"),
        $.get("/lib/cldr-data/main/" + culture + "/ca-gregorian.json"),
        $.get("/lib/cldr-data/main/" + culture +"/timeZoneNames.json"),
        $.get("/lib/cldr-data/supplemental/timeData.json"),
        $.get("/lib/cldr-data/supplemental/weekData.json")
    ).then(function () {
        // Normalize $.get results, we only need the JSON, not the request statuses.
        return [].slice.apply(arguments, [0]).map(function (result) {
            return result[0];
        });
    }).then(Globalize.load).then(function () {
        Globalize.locale(culture);
    });
</script>