请原谅我这个问题的天真,我想第一次尝试使用JavaScript。
我有这个返回字符串的JavaScript文件:
var strings = strings || {};
(function() {
strings.get = function(key) {
var locale = "english"; //To be switched based on device locale when localisation occurs...
return localeMap[locale][key];
};
strings.format = function() {
var args = arguments;
var str = strings.get(args[0]);
return str.replace(/{(\d+)}/g, function(match, num) {
return typeof args[+num + 1] != 'undefined' ? args[+num + 1] : match;
});
};
var englishStrings = {
"exercise.requires.max.force" : "You need to record your maximum strength before you can perform exercises. Would you like to record it now?",
"calibration.start.alert" : "Ensure the PeriCoach device is connected and in position. During calibration, follow the on screen instructions, contracting your muscles as hard as you can when it says 'Squeeze' and relaxing completely when it says 'Relax'. You will be required to repeat this four times.",
"calibration.starting" : "Starting...",
};
var localeMap = {
english: englishStrings
};
})();
我想更改它,以便它根据浏览器的区域设置返回一个字符串
我做了类似下面的事情,但我不知道如何更改它以便它返回正确的字符串,
显然var localeMap = {
en-US: englishStrings
it: italianStrings
};
错了...... 请问这是正确的形式?
var strings = strings || {};
(function() {
strings.get = function(key) {
var locale = "english"; //To be switched based on device locale when localisation occurs...
var language = window.navigator.userLanguage || window.navigator.language;
return localeMap[language][key];
};
strings.format = function() {
var args = arguments;
var str = strings.get(args[0]);
return str.replace(/{(\d+)}/g, function(match, num) {
return typeof args[+num + 1] != 'undefined' ? args[+num + 1] : match;
});
};
var englishStrings = {
"testo" : "Inglese",
"exercise.requires.max.force" : "You need to record your maximum strength before you can perform exercises. Would you like to record it now?",
"calibration.start.alert" : "Ensure the PeriCoach device is connected and in position. During calibration, follow the on screen instructions, contracting your muscles as hard as you can when it says 'Squeeze' and relaxing completely when it says 'Relax'. You will be required to repeat this four times.",
"calibration.starting" : "Starting...",
};
var italianStrings = {
"testo" : "Italiano",
"exercise.requires.max.force" : "Italiano",
"calibration.start.alert" : "Italiano",
"calibration.starting" : "Italiano",
};
var localeMap = {
en-US: englishStrings
it: italianStrings
};
})();
答案 0 :(得分:2)
我不完全确定这是否是你所追求的。但是您定义对象localeMap
的方式不正确。
var localeMap = {
"en-US": englishStrings, //note the comma and quotes
"it": italianStrings //quotes aren't necessary here
};
localeMap.it || localeMap["it"]
和localeMap["en-US"]
将返回包含字符串的对象。
修改强>
设置默认值的最简单方法是设置var loc = "en-US";
并在检测到其他区域设置时覆盖它。然后像这样检索字符串:localeMap[loc];
答案 1 :(得分:1)
您应该在html
标记中声明页面的语言,如下所示:
<html lang="en">
为什么呢? http://www.w3.org/TR/html401/struct/dirlang.html#h-8.1
假设您设置了lang属性,那么您可以使用JS / JQ轻松读取它:
使用Javascript:
document.querySelector('html').getAttribute('lang')
使用jQuery:
$('html').attr('lang')