【问题标题】:How to translate words in JS in multilingual website?如何在多语言网站中翻译 JS 中的单词?
【发布时间】:2015-04-23 12:19:31
【问题描述】:

我正在使用 nopCommerce,版本 3.50。我的网站是多语言的:俄语、英语、西班牙语。有一个显示产品图片的脚本——magnific-popup。

因此,当您单击照片时,它会出现在脚本框(如 fancybox)中,并且有一些标题,例如“正在加载..、关闭 (Esc)、下一个、上一个、2 个中的 1 个”。它们始终使用英语,独立于网站上选择的语言。我希望它使用选定的语言。

怎么做?

有可能创建不同语言的字符串并为其提供翻译并将它们放入该 jquery.magnific-popup.js 文件中。一个字符串看起来像这样——@T("magnificpopup.right")。但是JS忽略了字符串,可能是因为它是一个单独的脚本,没有触及这个CMS。

options: {
    enabled: false,
    arrowMarkup: '<button title="%title%" type="button" class="mfp-arrow mfp-arrow-%dir%"></button>',
    preload: [0,2],
    navigateByImgClick: true,
    arrows: true,

    tPrev: 'Previous (Left arrow key)',
    tNext: '@T("magnificpopup.right")',
    tCounter: '%curr% of %total%'
}

如何在 JS 文件中正确设置这些字符串? 我该如何解决我的问题?

感谢您的帮助。

【问题讨论】:

    标签: javascript jquery translation nopcommerce


    【解决方案1】:

    这里是 Magnific popup l18n 的文档:http://dimsemenov.com/plugins/magnific-popup/documentation.html#translating

    据此,您应该在页面的 JS 代码中加载 magnific-popup.js 脚本的行之后。

    var language = //you should get it from browser settings/cookies or server side
    
    if(language == 'ru_RU'){
    
    $.extend(true, $.magnificPopup.defaults, {
      tClose: 'Закрыть (Esc)', // Alt text on close button
      tLoading: 'Загрузка...', // Text that is displayed during loading. Can contain %curr% and %total% keys
    //other lines 
      });
    
    } else if (language == 'en_US'){
    
    $.extend(true, $.magnificPopup.defaults, {
      tClose: 'Close (Esc)', // Alt text on close button
      tLoading: 'Loading...', // Text that is displayed during loading. Can contain %curr% and %total% keys
    //other lines 
      });
    } else if ( // other languages
    

    【讨论】:

      【解决方案2】:

      您必须在您的网站标题中创建一个简单的对象,例如:

      <script>
      var mysite_lang = {
      button_title: "<?php echo $lang['button_title'];?>",
      box_title: "<?php echo $lang['box_title'];?>"
      ..etc..
      };
      </script>
      

      在添加你的 js 文件之后。

      现在你可以使用

      options: {
          enabled: false,
          arrowMarkup: '<button title="'+mysite_lang.button_title+'" type="button" class="mfp-arrow mfp-arrow-%dir%"></button>'};
      

      【讨论】:

      • 那行不通。感谢您的帮助。
      猜你喜欢
      • 2016-07-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 2012-08-16
      • 1970-01-01
      相关资源
      最近更新 更多