【问题标题】:How to find the cldr name from Microsoft CultureInfo in javascript?如何在 javascript 中从 Microsoft CultureInfo 中找到 cldr 名称?
【发布时间】:2018-08-06 15:31:33
【问题描述】:

我想知道是否存在从 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. 或...

【问题讨论】:

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


    【解决方案1】:

    终于找到解决办法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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 2018-04-26
      相关资源
      最近更新 更多