jquery-lang-js用占位符翻译

时间:2019-01-22 14:49:02

标签: javascript jquery html

基于this documentation的jquery-lang-js支持占位符进行翻译。 我的javascript和html实现是这样的:

function changeLangOnClick(lang) {
      console.log('clicked language change');
      window.lang.change(lang);
      console.log(lang);
      Cookies.set('site_lang', lang, { expires: 7 });
      return false;
       }
    var lang = new Lang();
    var shownLang = Cookies.get('site_lang');
    console.log(lang);
    console.log('gets here');
	lang.dynamic('de', 'i18n/de.json');
	lang.dynamic('it', 'i18n/it.json');
    lang.dynamic('en', 'i18n/en.json');
        
        if (typeof shownLang === 'undefined') {
            shownLang = window.navigator.language;
        }
         if(shownLang =="en-US" && (shownLang!="de" || shownLang!="it")){
            shownLang="en";
        }
        
		lang.init({
			defaultLang: 'de',
            currentLang: shownLang
			
		});
<select class="form-control" id="language-select" onchange="changeLangOnClick(value);">
          <option value="de">Deutsch</option>
          <option value="it">Italiano</option>
          <option value="en">English</option>
</select>

<h2 lang="de" placeholder="placeholder1"></h2>

我有这样的i18n / de.json:

{
"placeholder1": "WILLKOMMEN"

}

但是当我切换到de lang时,我会得到:

  

jquery-lang.js:545未捕获的TypeError:无法读取未定义的属性'placeholder1'       在Lang.translate(http://localhost/curtiforti2/js/lang/jquery-lang.js:545:40)       在Lang._translateAttribs(http://localhost/curtiforti2/js/lang/jquery-lang.js:362:26)       在Lang._translateElement(http://localhost/curtiforti2/js/lang/jquery-lang.js:520:8)       在Lang.change(http://localhost/curtiforti2/js/lang/jquery-lang.js:494:11)       在http://localhost/curtiforti2/js/lang/jquery-lang.js:460:20       在Object.success(http://localhost/curtiforti2/js/lang/jquery-lang.js:175:22)       在我(https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:27603)       在Object.fireWith [as resolveWith](https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:28369)       在A(https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:4:13858)       在XMLHttpRequest。 (https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:4:16146

我必须在console.logs上添加它以进行测试,以便选择正确的语言:console.log(lang);,并检查cookie似乎可以保存正确的语言。我的问题只是未定义的'placeholder1'

0 个答案:

没有答案