简体   繁体   中英

i18next is not working

I'm using phonegap and I need i18n support for this application so I ended up by choosing i18next. Below is my sample code,but i18next is failing,can anyone help me in this? The Output that I'm getting is just a list of links by name "nav.home","nav.page1","nav.page2". Moreover this sample HTML5 code is working only in Chrome and not in Mozilla.

//HTML code
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script type="text/javascript" src="i18next-1.6.3.js" ></script>
  <script type="text/javascript" src="translation.en.json" ></script>
  <script type="text/javascript" >
    $(document).ready(function(){
      i18n.init(function(t) {
        $(".nav").i18n();
        var appName = t("app.name");
      });
    });
  </script>
</head>

<body>
  <ul class="nav">
    <li><a href="#" data-i18n="nav.home"></a></li>
    <li><a href="#" data-i18n="nav.page1"></a></li>
    <li><a href="#" data-i18n="nav.page2"></a></li>
  </ul>
</body>
</html>

translation.en.js file

{
  "app": {
    "name": "i18next"
  },
  "nav": {
    "home": "Home",
    "page1": "Page One",
    "page2": "Page Two"
  }
}

I see two things:

  1. English locale should be located in locales/en/translation.json
  2. You don't have to attach .json file <head> section

Try to do this :

$(document).ready(function(){
    $.i18n.init({
        lng: 'en'
    }, function(t) {
        $(".nav").i18n();
        var appName = t("app.name");
    });
});

I think you are missing the options dictionnary.

Try this (not properly tested as I'm on my phone)

//HTML code
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script type="text/javascript" src="i18next-1.6.3.js" ></script>
  <script type="text/javascript" >
    $(document).ready(function(){
      i18n.init({ 
        resGetPath: 'translation.__lng__.json' 
      }, 
      function(t) {
        $(".nav").i18n();
        var appName = t("app.name");
      });
    });
  </script>
</head>

<body>
  <ul class="nav">
    <li><a href="#" data-i18n="nav.home"></a></li>
    <li><a href="#" data-i18n="nav.page1"></a></li>
    <li><a href="#" data-i18n="nav.page2"></a></li>
  </ul>
</body>
</html>

But I think it would be more normal to do as yarl said and rearrange into folders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM