如何在多语言网站中翻译 JS 中的单词?

How to translate words in JS in multilingual website?

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

因此,当您单击照片时,它会出现在脚本框(如 fancybox)中,并且有一些标题,如 "Loading.., Close (Esc), Next, Previous, 1 of 2"。他们始终以英语显示,具体取决于网站上所选的语言。我希望它使用选定的语言。

怎么做?

可以用不同的语言创建字符串,为它们提供翻译并将它们放入 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 文件中正确设置这些字符串? 我该如何解决我的问题?

感谢您的帮助。

您必须在您的网站 header 中创建一个简单的 object,例如:

<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>'};

这是 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