Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set the options parameter to initialize. #18

Open
jonalxh opened this issue Feb 8, 2019 · 2 comments
Open

How to set the options parameter to initialize. #18

jonalxh opened this issue Feb 8, 2019 · 2 comments

Comments

@jonalxh
Copy link

jonalxh commented Feb 8, 2019

Hi.

I was reading the documentation but I'm not able to find how to set the options parameter to begin getting texts.

var i18n = window.i18n(options);

I think it is just a JSON with some values, but I don't know what key, value pairs.

I hope you could help me.

@guillaumepotier
Copy link
Owner

Hi there,

I should update the documentation to explain better what you could pass as options.

If you look here, you could override these options: domain, locale, ctxt_delimiter. These are not settings you want to often override.

What I think you want is to load translations catalogue like this:

// i18n.setMessages(domain, locale, messages, plural_form);
i18n.setMessages('messages', 'fr', {
  "Welcome": "Bienvenue",
  "There is %1 apple": [
    "Il y a %1 pomme",
    "Il y a %1 pommes"
  ]
}, 'nplurals=2; plural=n>1;');

or with a JSON:

// i18n.loadJSON(jsonData /*, domain */);
var json = {
  "": {
    "language": "fr",
    "plural-forms": "nplurals=2; plural=n>1;"
  },
  "Welcome": "Bienvenue",
  "There is %1 apple": [
    "Il y a %1 pomme",
    "Il y a %1 pommes"
  ]
};
i18n.loadJSON(json, 'messages');

Then call i18n.setLocale('fr'); to use the catalog you just loaded, and try i18n.gettext('Welcome');, you'll see French! ;-)

@boydkelly
Copy link

  • If you are satisfied with the defaults do you still need a statement similar to:
    var i18n = window.i18n before loading the json?

  • Can one put either:
    <script src="/path/to/dist/gettext.iife.js" type="text/javascript"></script>
    or
    <script src="/path/to/dist/gettext.js" type="text/javascript"></script>

in the webpage header and then access i18n in other scripts?

I need to translate something in an existing script and have added this to the script:

 var i18n = window.i18n                                                                                                                                                          
  i18n.setMessages('messages', 'fr', {                                                                                                                                            
    Welcome: 'Bienvenue',                                                                                                                                                         
    contents: 'Sur cette page...',                                                                                                                                                
    'There is %1 apple': [                                                                                                                                                        
      'Il y a %1 pomme',                                                                                                                                                          
      'Il y a %1 pommes',                                                                                                                                                         
    ],                                                                                                                                                                            
  }, 'nplurals=2; plural=n>1;')    

But I am getting a console error Uncaught TypeError: e.setMessages is not a function

I don't know that that e is or where it is comming from. Is there something I may have missed?

note: the linter wanted me to put strings in single quotes.

https://devel.coastsystems.net/julakan/fr/udhr/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants