guillaumepotier / gettext.js

gettext.js is a lightweight yet complete and accurate GNU gettext port for node and the browser.
http://guillaumepotier.github.io/gettext.js
150 stars 48 forks source link

How to set the options parameter to initialize. #18

Open jonalxh opened 5 years ago

jonalxh commented 5 years ago

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 commented 5 years ago

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 commented 2 years ago

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/