如何在javascript中从Microsoft CultureInfo查找cldr名称?

时间:2018-08-06 15:31:33

标签: asp.net-core internationalization jquery-globalize cldr

我想知道是否存在从Microsoft CultureInfo(可以在此处MS-LCID (Windows Language Code ID)处查找)到Unicode cldr语言代码的定义映射。

我目前正在使用jQuery和globalize.js来验证asp.net-core站点的用户输入。我们的实现类似于此示例validationScript.cshtml (asp.net-core code)

我们只需要这样更改脚本部分:

<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>

如果我将站点切换到以下之一:

  1. CultureInfo(“ zh-CHS”)
  2. CultureInfo(“ zh-CHT”)
  3. CultureInfo(“ de-DE”)
  4. CultureInfo(“ ja-JP”)
  5. CultureInfo(“ zh-CN”)

globalize.js无法正常工作,因为上述任何语言ID都没有cldr文件夹。

我在这里cldr-numbers-full/main/ (JSON data for CLDR 33 release)上进行了查找,但是找不到上面的任何ID。

所以我的问题是: “如果有一个正确的问题,是否存在从MS-LCID到cldr-ID的已定义映射?

我的第二个问题是:当前使用的标准/最佳实践是什么?

  1. MS-LCID
  2. cldr ID
  3. 或其中一种here(IETF语言标签)
  4. these(ISO 639)
  5. 或...

1 个答案:

答案 0 :(得分:0)

最后找到了解决方法here

在_ValidationScriptsPartial.cshtml的末尾添加以下代码。

@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>