我在创建这段代码时遇到了困难。我用的是
$scope.convertToLanguage = function ($language) {
var fileref = document.createElement("script");
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", "whatever the filename is");
if (typeof fileref != "undefined") {
document.getElementsByTagName("head")[0].appendChild(fileref)
}
}
我试图通过加载这些语言环境中的任何一个来使源动态化
<script src = "angular-locale_de-de.js"> </script>
angular-locale_es-es.js
angular-locale_it-it.js
angular-locale_fr-fr.js
脚本将加载。但是,语言不会改变。当我从前面的问题中读到的那样,我们使用区域设置来转换选择器的语言。
我想更改"**ng-click**"
活动中的语言。
请帮忙。
我无法使用 i18n 做到这一点。但是,我通过修改角度引导程序本身(日期选择器部分)来正确处理它,您将在控制器中看到它。我使用了一个 javascript 数组,然后使用 case 执行条件,然后我用不同的语言从我的数组中调用相应的月份/天数。看看这个。
this.modes = [
{
name: 'day',
getVisibleDates: function (date, selected) {
var year = date.getFullYear(), month = date.getMonth(), firstDayOfMonth = new Date(year, month, 1);
var difference = startingDay - firstDayOfMonth.getDay(),
numDisplayedFromPreviousMonth = (difference > 0) ? 7 - difference : -difference,
firstDate = new Date(firstDayOfMonth), numDates = 0;
if (numDisplayedFromPreviousMonth > 0) {
firstDate.setDate(-numDisplayedFromPreviousMonth + 1);
numDates += numDisplayedFromPreviousMonth; // Previous
}
numDates += getDaysInMonth(year, month + 1); // Current
numDates += (7 - numDates % 7) % 7; // Next
var days = getDates(firstDate, numDates), labels = new Array(7);
for (var i = 0; i < numDates; i++) {
var dt = new Date(days[i]);
days[i] = makeDate(dt, format.day, (selected && selected.getDate() === dt.getDate() && selected.getMonth() === dt.getMonth() && selected.getFullYear() === dt.getFullYear()), dt.getMonth() !== month);
}
//I Edited this part
for (var j = 0; j < 7; j++) {
labels[j] = global.shortDays[j];
}
monthDisplay = dateFilter(date, format.dayTitle);
var e, f, i, s;
for (var i = 0; i < 12; i++) {
if (global.languageValidator === "de") {
monthDisplay = monthDisplay.replace(global.defaultMonths[i], global.germanMonths[i]);
monthDisplay = monthDisplay.replace(global.frenchMonths[i], global.germanMonths[i]);
monthDisplay = monthDisplay.replace(global.italianMonths[i], global.germanMonths[i]);
monthDisplay = monthDisplay.replace(global.spanishMonths[i], global.germanMonths[i]);
}
else if (global.languageValidator === "fr") {
monthDisplay = monthDisplay.replace(global.defaultMonths[i], global.frenchMonths[i]);
monthDisplay = monthDisplay.replace(global.germanMonths[i], global.frenchMonths[i]);
monthDisplay = monthDisplay.replace(global.italianMonths[i], global.frenchMonths[i]);
monthDisplay = monthDisplay.replace(global.spanishMonths[i], global.frenchMonths[i]);
}
else if (global.languageValidator === "it") {
monthDisplay = monthDisplay.replace(global.defaultMonths[i], global.italianMonths[i]);
monthDisplay = monthDisplay.replace(global.germanMonths[i], global.italianMonths[i]);
monthDisplay = monthDisplay.replace(global.frenchMonths[i], global.italianMonths[i]);
monthDisplay = monthDisplay.replace(global.spanishMonths[i], global.italianMonths[i]);
}
else if (global.languageValidator === "es") {
monthDisplay = monthDisplay.replace(global.defaultMonths[i], global.spanishMonths[i]);
monthDisplay = monthDisplay.replace(global.germanMonths[i], global.spanishMonths[i]);
monthDisplay = monthDisplay.replace(global.frenchMonths[i], global.spanishMonths[i]);
monthDisplay = monthDisplay.replace(global.italianMonths[i], global.spanishMonths[i]);
}
}
//return { objects: days, title: dateFilter(date, format.dayTitle), labels: labels }; //orignal return
return { objects: days, title: monthDisplay, labels: labels }; //modified return
//until here
},
compare: function (date1, date2) {
return (new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()) - new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()));
},
split: 7,
step: { months: 1 }
},
{
name: 'month',
getVisibleDates: function (date, selected) {
var months = new Array(12), year = date.getFullYear();
for (var i = 0; i < 12; i++) {
var dt = new Date(year, i, 1);
//months[i] = makeDate(dt, format.month, (selected && selected.getMonth() === i && selected.getFullYear() === year));
//Modified by AARON
if (global.languageValidator === "de") {
months[i] = makeDate(dt, format.month, (selected && selected.getMonth() === i && selected.getFullYear() === year));
months[i].label = global.germanMonths[i];
}
else if (global.languageValidator === "fr") {
months[i] = makeDate(dt, format.month, (selected && selected.getMonth() === i && selected.getFullYear() === year));
months[i].label = global.frenchMonths[i];
}
else if (global.languageValidator === "it") {
months[i] = makeDate(dt, format.month, (selected && selected.getMonth() === i && selected.getFullYear() === year));
months[i].label = global.italianMonths[i];
}
else if (global.languageValidator === "es") {
months[i] = makeDate(dt, format.month, (selected && selected.getMonth() === i && selected.getFullYear() === year));
months[i].label = global.spanishMonths[i];
}
else
{
months[i] = makeDate(dt, format.month, (selected && selected.getMonth() === i && selected.getFullYear() === year));
}
//console.log("Label = " + months[i].label);
}
//console.log({ objects: months, title: dateFilter(date, format.monthTitle) });
return { objects: months, title: dateFilter(date, format.monthTitle) };
},
compare: function (date1, date2) {
return new Date(date1.getFullYear(), date1.getMonth()) - new Date(date2.getFullYear(), date2.getMonth());
},
split: 3,
step: { years: 1 }
},
我建议您使用角度翻译模块,该模块将帮助您轻松地在语言环境之间转换。
您将能够引用所需的任何翻译文件,并随意异步加载所需的翻译文件。为此,您还需要可选的angular-translate-loader-static-files
插件。
例:
$translateProvider.useStaticFilesLoader({ // reference any locale-xxxxx.json locale file
prefix: 'locale-',
suffix: '.json'
});
$translateProvider.preferredLanguage('en'); // use "en" by default
$translateProvider.determinePreferredLanguage(); // automatically determine best locale from browser config
$translate.use('de'); // force the use of "de" at runtime
更重要的是,这个插件在 i18n 周围有大量有用的功能。即使我只有一个可用的语言环境,我也会在所有项目中使用它;)
请参阅以下演示:http://angular-translate.github.io/docs/#/guide/07_multi-language (
希望有帮助