在 jquery globalize 中设置和更改语言环境
setting and changing locale in jquery globalize
我正在尝试了解在使用 jquery-globalize 库时自动设置区域设置并可能动态更改它的最佳实践。
在 jquery 中起床和 运行 的要求全球化,或者至少据我了解是:
- 包括所需的 JavaScript 文件
- 加载 CLDR 数据
- 设置语言环境
第 1 步不在本题的讨论范围内;将其视为前进的先决条件。
第 2 步被记录为普通 JavaScript 看起来像这样,在一个示例中,他们称为 "dynamic" 加载:
$.when(
$.get( "cldr/main/en/ca-gregorian.json" ),
$.get( "cldr/supplemental/likelySubtags.json" ),
$.get( "cldr/supplemental/timeData.json" ),
$.get( "cldr/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() {
// Your code goes here.
});
如果我正确理解代码,它会使用一系列承诺:获取 JSON,对其进行规范化,然后 运行 通过 Globalize.load,然后执行 "Your code".
但是,从我通常使用的词的意义上讲,这不是动态的。它不响应用户输入或状态更改。你在前面陈述 "load the English calendar information".
还需要其他模块。我不认为 globalize 是您的语言目录结构的 "aware",这意味着您还需要加载所有这些其他文件,以某种方式将它们传递到 Globalize.load(JSON)
。
然后,尽管文档有点混乱,但我认为您需要设置语言环境,这将是步骤 #3。
Globalize.locale( "en" )
最后,回到问题:
- 为了设置语言环境,您需要了解一种文化。从用户代理字符串或类似字符串中提取它是一种好习惯吗?如果我没有提供检测到的区域设置,我是否应该手动回退到给定的语言?
换句话说,是否有内置于 globalize 中的功能来处理抓取浏览器或系统文化并尝试自动使用它,或者我是否总是需要显式调用 Globalize.locale()
?
我只能设置加载对应CLDRJSON的文化。因此,在语言更改或加载时,我是否需要调用一组与示例 "load" 脚本类似的调用?我不怀疑简单地调用 Globalize.locale(newLocale)
会做任何事情,除非 JSON 被加载。是这样的吗?:
function changeLocale(语言环境){
// "validLocales" 未在此示例中显示,但可以将其想象成我在分发版中明确提供 CLDR 的语言环境数组
如果(validLocales.indexOf(语言环境)!== -1){
$.when(
$.get( "cldr/main/" + locale + "/ca-gregorian.json" ),
).then(函数() {
// 归一化函数
}).then(Globalize.load).then(函数(){
// 其余的东西函数
});
} 别的 {
// 处理无效的语言环境
}
?
这就是文档中看起来...很清楚...的所有内容,但没有像这样明确记录,所以我不确定我是否遗漏了我的默认行为不必编写代码(因为它们已经是固有的)。
- 为了让 Web 应用程序的消费者(部署它的 Web 管理员)能够实现可扩展的全球化,这是否是一个文档问题? “你不能只切换到 'fr',你需要下载 CLDR 文件,将它们放在 X 目录中,然后更新 "validLocales" 可配置参数,然后修改初始化脚本以设置新的默认值?与 "add a new language file with an ISO country code, such as fr.json".
相比,期望有人部署我的应用程序来承担这一切似乎相当沉重
我个人推荐使用 Globalize 构建应用程序的方式是 Application example using webpack and npm。
现在,更直接地回答您的问题...
- In order to set the locale, you need to be aware of a culture. Is it good practice to pull it from a user agent string or somesuch? In the event that I don't provide the detected locale, should I manually fall back to a given language?
In other words, is there functionality built into globalize to handle grabbing the brower or system culture and try using it automatically, or do I always need to explicitly call Globalize.locale()?
Globalize 对应用程序应该如何做没有意见。某些应用程序可能会发现允许其用户 select 站点语言更好。一些应用程序可能会发现使用用户代理信息更好 ()。全球化应该允许这两种用法。
I can only set a culture whose corresponding CLDR JSON is loaded. So on language change or load, do I need to invoke a similar set of calls as the sample "load" script? I don't suspect that simply calling Globalize.locale(newLocale) is going to do anything unless the JSON is loaded. Something like this?:
function changeLocale(locale) { // "validLocales" is not shown in this sample, but imagine it as an array of locales I have explicitly provided CLDR for in my distribution if(validLocales.indexOf(locale) !== -1) {
$.when( $.get( "cldr/main/" + locale + "/ca-gregorian.json" ), ).then(function() { // normalize function }).then(Globalize.load).then(function(){ // rest of stuff function(s) }); } else { // handle invalid locale } ?
This is all what seems... clear-ish... from the documentation, but it isn't explicitly documented like this, so I'm not sure if I'm missing out on default behaviours that I don't have to code for (because they're already inherent).
你是对的。 Globalize.locale(<locale>)
不会执行任何操作,除非预先加载了正确的数据(通过使用 Globalize.load()
加载数据本身或使用 precompiled formatters and parsers 以获得最佳性能)。所以,是的,应用程序开发人员必须处理这个问题。
我之前提到的 Application example using webpack and npm 负责加载数据并自动为生产生成最佳包。显然,Globalize 允许其他几种安排或用法。如果您在这里有任何 other/more 具体问题,请告诉我。
To allow extensible globalization for the consumers of the web application (web administrators deploying it), is it then a matter of documentation? "You can't just switch to 'fr', you need to download the CLDR files, place them in X directory, then update the "validLocales" configurable parameter, then modify the initialization script to set a new default? It all seems rather heavy to expect someone deploying my application to undertake, vs. "add a new language file with an ISO country code, such as fr.json".
当然可以。我相信我们可以对文档和展示我们使用 Globalize 的 Web 应用程序最佳实践的示例做更多的改进。 Application example using webpack and npm 是我们最近进行的更新,我认为这与您的问题非常相关。如有任何问题,请告诉我。
我正在尝试了解在使用 jquery-globalize 库时自动设置区域设置并可能动态更改它的最佳实践。
在 jquery 中起床和 运行 的要求全球化,或者至少据我了解是:
- 包括所需的 JavaScript 文件
- 加载 CLDR 数据
- 设置语言环境
第 1 步不在本题的讨论范围内;将其视为前进的先决条件。
第 2 步被记录为普通 JavaScript 看起来像这样,在一个示例中,他们称为 "dynamic" 加载:
$.when(
$.get( "cldr/main/en/ca-gregorian.json" ),
$.get( "cldr/supplemental/likelySubtags.json" ),
$.get( "cldr/supplemental/timeData.json" ),
$.get( "cldr/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() {
// Your code goes here.
});
如果我正确理解代码,它会使用一系列承诺:获取 JSON,对其进行规范化,然后 运行 通过 Globalize.load,然后执行 "Your code".
但是,从我通常使用的词的意义上讲,这不是动态的。它不响应用户输入或状态更改。你在前面陈述 "load the English calendar information".
还需要其他模块。我不认为 globalize 是您的语言目录结构的 "aware",这意味着您还需要加载所有这些其他文件,以某种方式将它们传递到 Globalize.load(JSON)
。
然后,尽管文档有点混乱,但我认为您需要设置语言环境,这将是步骤 #3。
Globalize.locale( "en" )
最后,回到问题:
- 为了设置语言环境,您需要了解一种文化。从用户代理字符串或类似字符串中提取它是一种好习惯吗?如果我没有提供检测到的区域设置,我是否应该手动回退到给定的语言?
换句话说,是否有内置于 globalize 中的功能来处理抓取浏览器或系统文化并尝试自动使用它,或者我是否总是需要显式调用 Globalize.locale()
?
我只能设置加载对应CLDRJSON的文化。因此,在语言更改或加载时,我是否需要调用一组与示例 "load" 脚本类似的调用?我不怀疑简单地调用
Globalize.locale(newLocale)
会做任何事情,除非 JSON 被加载。是这样的吗?:function changeLocale(语言环境){ // "validLocales" 未在此示例中显示,但可以将其想象成我在分发版中明确提供 CLDR 的语言环境数组 如果(validLocales.indexOf(语言环境)!== -1){
$.when( $.get( "cldr/main/" + locale + "/ca-gregorian.json" ), ).then(函数() { // 归一化函数 }).then(Globalize.load).then(函数(){ // 其余的东西函数 }); } 别的 { // 处理无效的语言环境 }
?
这就是文档中看起来...很清楚...的所有内容,但没有像这样明确记录,所以我不确定我是否遗漏了我的默认行为不必编写代码(因为它们已经是固有的)。
- 为了让 Web 应用程序的消费者(部署它的 Web 管理员)能够实现可扩展的全球化,这是否是一个文档问题? “你不能只切换到 'fr',你需要下载 CLDR 文件,将它们放在 X 目录中,然后更新 "validLocales" 可配置参数,然后修改初始化脚本以设置新的默认值?与 "add a new language file with an ISO country code, such as fr.json". 相比,期望有人部署我的应用程序来承担这一切似乎相当沉重
我个人推荐使用 Globalize 构建应用程序的方式是 Application example using webpack and npm。
现在,更直接地回答您的问题...
- In order to set the locale, you need to be aware of a culture. Is it good practice to pull it from a user agent string or somesuch? In the event that I don't provide the detected locale, should I manually fall back to a given language?
In other words, is there functionality built into globalize to handle grabbing the brower or system culture and try using it automatically, or do I always need to explicitly call Globalize.locale()?
Globalize 对应用程序应该如何做没有意见。某些应用程序可能会发现允许其用户 select 站点语言更好。一些应用程序可能会发现使用用户代理信息更好 ()。全球化应该允许这两种用法。
I can only set a culture whose corresponding CLDR JSON is loaded. So on language change or load, do I need to invoke a similar set of calls as the sample "load" script? I don't suspect that simply calling Globalize.locale(newLocale) is going to do anything unless the JSON is loaded. Something like this?:
function changeLocale(locale) { // "validLocales" is not shown in this sample, but imagine it as an array of locales I have explicitly provided CLDR for in my distribution if(validLocales.indexOf(locale) !== -1) { $.when( $.get( "cldr/main/" + locale + "/ca-gregorian.json" ), ).then(function() { // normalize function }).then(Globalize.load).then(function(){ // rest of stuff function(s) }); } else { // handle invalid locale } ?
This is all what seems... clear-ish... from the documentation, but it isn't explicitly documented like this, so I'm not sure if I'm missing out on default behaviours that I don't have to code for (because they're already inherent).
你是对的。 Globalize.locale(<locale>)
不会执行任何操作,除非预先加载了正确的数据(通过使用 Globalize.load()
加载数据本身或使用 precompiled formatters and parsers 以获得最佳性能)。所以,是的,应用程序开发人员必须处理这个问题。
我之前提到的 Application example using webpack and npm 负责加载数据并自动为生产生成最佳包。显然,Globalize 允许其他几种安排或用法。如果您在这里有任何 other/more 具体问题,请告诉我。
To allow extensible globalization for the consumers of the web application (web administrators deploying it), is it then a matter of documentation? "You can't just switch to 'fr', you need to download the CLDR files, place them in X directory, then update the "validLocales" configurable parameter, then modify the initialization script to set a new default? It all seems rather heavy to expect someone deploying my application to undertake, vs. "add a new language file with an ISO country code, such as fr.json".
当然可以。我相信我们可以对文档和展示我们使用 Globalize 的 Web 应用程序最佳实践的示例做更多的改进。 Application example using webpack and npm 是我们最近进行的更新,我认为这与您的问题非常相关。如有任何问题,请告诉我。