我的网站上有以下语言选择器小部件:
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 !
locals/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 来构建它?