Extending functionality - Nemo64/meteor-translator GitHub Wiki
This packages applies filters to every translation it does. They are responsible to do all the magic like plural, dates etc..
Those filters are essentially just callback functions so it is very easy to extend functionality. Those filters are stored within FilterLists which allow to append or prepend callbacks.
post-process filters
These filters are applied when a translation is requested using translator.get
. There are 2 FilterLists that get applied to the result.
Translator.objectFilter
is called first. It contains by default the message-format filter. If youprepend
a callback you will be able to access the compiled objects before they are processed. Only add filters here, if they change the type of the input, eg. if you had an array in you translation file you can make a string out of it here.Translator.stringFilter
can do some final processing. It is guaranteed that the input is a string. If you want to do modifications on the final string here is where you need to be.
A callback function could look like this:
Translator.stringFilter.append(function (string, data) {
return string.replace(/curse/g, '***');
});
The data variable passed to those filters contains this
key
the requested key, eg.user_area.header
language
the instance of LanguageArray used (including all fallback locales)locale
the locale which was used for this translationparameters
all variables in an objecttranslator
reference of the translator instancemeta
meta information of the language file that was attached by the plugin
pre-process filters
Those filters get applied while the resource handler is doing it's job. This means you can change the way the file gets compiled. Sadly (to my knowledge) there is no way you can define code that is run while the buildPlugin is running, so the only way to hook in here is to modify the translator package itself. I do not recommend that but document it anyways, as it might change in the future.
Like the post-process filters, there are 2 pre-process filter executed in following order:
ResourceHandler.stringFilter
These filters will get the rough input of the file. If the language file isn't well formatted they might get none strings as input so always check you input here. If you get array etc. just pass them though, don't touch 'em.ResourceHandler.objectFilter
These filters may modify the type of there input. The message-format filter lies here. If youappend
a filter you can modify the compiled message-format information. Use this list if you want to do some pre-compilation of eg. a new syntax you introduce. Be sure to hook yourself into the objectFilter of post-process too, to revert the object to a string again.