我的网站上有以下语言选择器小部件:
jQuery(function($) {
var setLng = $.url().param('setLng');
var setLngCookie = $.cookie('i18next');
var language;
if (setLngCookie) {
language = setLngCookie;
}
else {
if (setLng) {
language_complete = setLng.split("-");
} else {
language_complete = navigator.language.split("-");
}
language = (language_complete[0]);
}
//Build the language selector.
$.getJSON("../locales/config.json", function(objData) {
var arrHtml = [];
var strClass;
$.each(objData, function(strKey, objLanguage) {
strClass = (language == objLanguage.locale) ? " class=\"selected-language\"" : ""; //Make sure the language selector has the correct language selected on initial page load.
arrHtml.push("<a id=\"lang-" + objLanguage.locale + "\" data-i18n=\"language." + objLanguage.locale + "\"" + strClass + "></a>");
});
$("<div/>", {
'class': 'language-list',
html: arrHtml.join('')
}).appendTo('#language-menu');
$("li.language-menu").live("click", function(){
$("#language-menu").toggle();
});
});
function setLanguage() {
// save to use translation function as resources are fetched
$(".tzm-i18n").i18n();
$(".page-i18n").i18n();
$(".menu").i18n();
$(".user-menu").i18n();
$(".search-form").i18n();
$(".footer-i18n").i18n();
$("#language-menu").hide();
}
i18n.init({
lng: language,
debug: true
}, setLanguage);
// language selector
$("#language-menu a").live("click", function() {
var booReload = false; // TRUE = reload the page; FALSE = do not reload the page
var $this = $(this);
var value = $this.attr("id");
var arrValueParts = value.split("-");
var language = arrValueParts[1];
if (booReload) {
window.location.href = "/index.html?setLng=" + language;
} else {
i18n.init({
lng: language,
debug: true
}, setLanguage);
}
$("#language-menu a").removeClass("selected-language");
$this.addClass("selected-language");
});
});
和
(zmgc)☺ tree -L 2 locales
locales
├── README.md
├── config.json
├── de
│ └── translation.json
├── dev
│ └── translation.json
├── en
│ └── translation.json
├── fr
│ └── translation.json
├── jp
│ └── translation.json
├── pt
│ └── translation.json
├── rs
│ └── translation.json
├── ru
│ └── translation.json
└── us
└── translation.json
所以每次我在locals / xx中添加一个目录,其中xx是来自IETF语言标签的国家代码,我必须手动更新../locales/config.json!
当地人/ config.json
{
"de": {
"locale": "de",
"code": "de-DE"
},
"en": {
"locale": "en",
"code": "en-UK"
},
"fr": {
"locale": "fr",
"code": "fr-FR"
},
"jp": {
"locale": "jp",
"code": "jp-JP"
},
"pt": {
"locale": "pt",
"code": "pt-BR"
},
"rs": {
"locale": "rs",
"code": "rs-RS"
},
"ru": {
"locale": "ru",
"code": "ru-RU"
},
"us": {
"locale": "us",
"code": "en-US"
}
}
示例locals / en / translation.json
{
"locale": "en"
,"code": "en-UK"
,"tzm": "Chapters - Zeitgeist Movement"
,"menu": {
"projects": "Projects"
,"map": "Map"
,"calendar": "Calendar"
,"forums": "Forums"
,"more": "More »"
}
,"search": {
"advanced": "Advanced"
,"search": "Search"
}
,"user": {
"gravatar": "Gravatar"
,"profile": "Profile"
,"login": "Login"
,"log-out": "Log out"
,"contact": "Contact"
,"help": "Help"
}
,"footer": {
"zeitgeist": "Zeitgeist"
}
}
有没有办法通过遍历locals /目录并构建可用语言并从可用语言列表中排除locals / dev来构建它?
答案 0 :(得分:1)
不是来自JavaScript。
我建议您在服务器上构建一个脚本,为您生成配置文件。然后,您就可以在客户端上使用相同的代码。
答案 1 :(得分:1)
除非您通过HTTP公开目录,否则不会; JavaScript在客户端上执行,通常无法自由访问服务器端目录布局。
在服务器端构建配置文件,或通过API公开文件列表。