Getting the translation - Nemo64/meteor-translator GitHub Wiki
You should look at [Writing language files](Writing language files) first!
The concept is that create an instance of the Translator
and add namespaces to it. You will then get your translated texts from that instance. That way you can also use a translator instance in a package without interfering with outside code.
PageLang = new Translator();
PageLang.use('languages/public'); // the namespace (file name without ending)
PageLang.get('user_login.title'); // => login area
Basically every file is a namespace. You can also use
multiple namespaces.
FrontLang = new Translator();
FrontLang.use("languages/public");
// now every key of languages/public.en_US.lang.yml
// can be accessed with FrontLang.get("key");
MailerLang = new Translator();
MailerLang.use("languages/user_mail");
MailerLang.use("languages/admin_mail");
MailerLang.use("languages/status_mail");
// the mailer translator will now look into 3 namespaces
// there priority is from the first to the last
MyPackageLang = new Translator();
MyPackageLang.use("packages/my-package/lang");
// this can be used if you want to use this translator
// for a meteor package. Other scenarios include mails etc.
This package tries to automatically guess the language of the user using the accept-language
header but you should always include a way for the user to change it! Also you should define a default language which will be used if none of the languages the browser offers exist.
Translator.setDefaultLanguage(['en_US', 'en']);
- Warning If you do not define a global language or a default language your user might have no language at all and see the translation keys which you definetly want to prevent from happening!
- Note The default language is currently only used for the autodetection! It won't have any effect if you set a language like below!
Most of the time your application uses (at least in the frontend) one language. That's why there is a global language for all translator instances!
// still using the automatically detected language (en_US)
FrontLang.get('hello'); // hello!
// ...
// user selects new language
Translator.setLanguage(["de_DE"]);
// this reactively changes all translations
FrontLang.get('hello'); // Hallo!
But that might be to limiting (especially on the server side) so you can overwrite it for a translator.
FrontLang = new Translator();
FrontLang.use("languages/public");
// per translator
FrontLang.setLanguage(["de_DE"]);
FrontLang.get("hello"); // => Hallo!
Currently the server side is a little dumb when it comes to languages. It can't select them automatically and it doesn't know which language was selected on the client. Therefor, if a server-method requires translations, you have to send the current language with the method call.
Meteor.call('sayHi', FrontLang.getLanguage());
Meteor.methods({
sayHi: function (language) {
var trans = new Translator();
trans.use('namespace');
trans.setLanguage(language);
return trans.get('hi');
}
})
var title = FrontLang.get('user_login.title');
// => login area
Also there is a little helper to create a callback function that returns the string. (in this example used with SimpleSchema)
BookSchema = new SimpleSchema({
title: {
type: String,
label: FrontLang.getCallback('book.schema.title')
}
});
A little harder because of the namespaces but doable. You need a little JavaScript setup.
Template.templateName.trans = FrontLang.createHelper();
<template name="template_name">
<h1>{{trans "user_login.title"}}</h1>
</template>