API reference - Nemo64/meteor-translator GitHub Wiki

Translator object

Translator.setDefaultLanguage(language)

Sets a default language for the auto detection. If no language the users browser knows is available in your translation files, this fallback will be used. Of course you should use language you have ;)

Arguments
  • language LanguageArray, Array of strings or Locales, Locale or string The language(s) that should be used if no others are found in your translation files.

Translator.setLanguage(language)

Sets the global language for your app.

Arguments
  • language LanguageArray, Array of strings or Locales, Locale or string The language to use when no other language is specified.

Translator.objectFilter

This is a FilterList which will be applied to every translation on the client side. The objectFilter will get all values that are in the translation file rough, meaning it can get objects. This step is probably not what you want, look at the stringFilter.

Example
Translator.objectFilter.prepend(function (value, data) {
  if (_.isObject(value)) {
    return value.toString();
  } else {
    return value;
  }
});

Translator.stringFilter

This is a FilterList which will be applied to every translation on the client side. The stringFilter will get all values before they are returned by the #get method and therefor are guaranteed to be strings. In this step you could implement something like a word filter.

Example
Translator.stringFilter.append(function (value, data) {
  return value.replace(curseWordRegex, '***');
});

Translator methods

translator.constructor([language])

Creates an instance of the translator.

Arguments
  • language LanguageArray, Array of strings or Locales, Locale or string The initial Language that this instance will use. The global standard will be used if this is not specified.

translator.isLoading()

Tells if this translator is still loading language files. This method is reactive so it can be used in templates.


translator.ready(callback, [repeat])

Calls the specified method as soon as all namespaces of this translator instance have been loaded. It will also be called if the loading process has failed but is now finished.

Arguments
  • callback Function A callback function that will be called if the loading process is done.
  • repeat boolean If true the callback will be called again if more namespaces are added and loaded.

translator.setLanguage(language)

Changes the language for this translator instance and invalidates all calls to #get so they reactively change.

Arguments

  • language LanguageArray, Array of strings or Locales, Locale or string The language this instance should have.

Examples

translator.setLanguage('en_US');
translator.setLanguage(['en_US', 'en_GB']);
translator.setLanguage(new Translator.Locale('en_US'));
translator.setLanguage(new Translator.LanguageArray(['en_US', 'en_GB']));

translator.getLanguage()

Gets the currently used language. This means: if this is translator instance hasn't been given a language it returns the global language. If there is no global language it returns the detected langauge. It always returns a LanguageArray.

Examples

translator.getLanguage(); // LanguageArray|['en_US']
translator.setLanguage('de_DE');
translator.getLanguage(); // LanguageArray|['de_DE']

translator.use(namespace)

Will add a namespace (a file) to the collection which this translator instance will search.

Arguments
  • namespace string The name of the namespace which is the filename without extensions
Examples
translator.use('translations/public');
// this will load /translations/public.[locale].lang.yml

translator.get(key, [parameters])

Gets the translation of the specified key.

Arguments
  • key string The key you specified in the translation file. The keys are seperated by a ..
  • parameters object A key-value object with values that you want to give you tranlation string. Be sure to include informations like gender if appropriate.

translator.getCallback(key, [parameters])

This is the same as translator.get but returns a function instead of the value.

Examples
translator.getCallback('some.key'); // => function()
translator.getCallback('some.key')(); // => "some translation"

translator.createHelper()

Creates a helper which can be given to a temple to use {trans 'some.key'}

Examples
Template.my_template.trans = translator.createHelper();