动态更改Angular Bootstrap Datepicker的语言

时间:2014-12-11 06:05:02

标签: javascript angularjs datepicker angularjs-bootstrap

我在创建此代码时遇到了困难。我用的是

 $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

脚本加载。但是,语言不会改变。当我从之前的问题中读到时,我们使用Locales(s)来转换选择器的语言。

我想更改"**ng-click**"事件中的语言。

请帮忙。

2 个答案:

答案 0 :(得分:2)

不幸的是我无法用i18n做到这一点。但是,我通过修改你在控制器中看到它的角度引导本身(datepicker部分)来做到了。我使用了一个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 }

  },

答案 1 :(得分:1)

我建议您使用angular-translate模块,它可以帮助您轻松地在区域设置之间进行切换。

您将能够引用所需的任何翻译文件,并以您需要的方式异步加载。为此,您还需要optionnal 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

希望有所帮助