Translation¶
The toolkit supports internationalization using gettext. Apps have an $
attribute available that they can use when they would like to internationalize
their text. Here is a simple example:
var SomeApp = App.extend(function(self) {
App.call(self);
var $ = self.$;
self.states.add('states:start', function(name) {
return new FreeText(name, {
question: $("Hello! Say something!"),
next: 'states:end'
});
});
self.states.add('states:end', function(name) {
return new EndState(name, {
text: $.dgettext('messages', "That's nice, bye!")
});
});
});
The gettext methods are well documented in the python docs.
Under the hood¶
-
class
LazyText
(method, args)¶ Holds information about text to be translated at a later stage.
Arguments: - method (string) – The gettext method to use for translation
- args (array or arguments) – The args given to the gettext method to perform the translation
-
static
apply_translation
(jed)¶ Accepts a
Jed()
instance and uses it to translate the text.Arguments: - jed (Jed) – The jed instance to translate with
-
static
context
(ctx)¶ Sets the context to use in translations.
Arguments: - ctx (object) – An object containing the context to be used.
$('Hello {{ person }}!').context({person: 'Guy'});
-
class
LazyTranslator
()¶ Constructs
LazyText()
instances holding information for translation at a later stage.- Supports the following gettext methods:
- gettext = fn(key)
- dgettext = fn(domain, key)
- ngettext = fn(singular_key, plural_key, value)
- dngettext = fn(domain, singular_ley, plural_key, value)
- pgettext = fn(context, key)
- dpgettext = fn(domain, context, key)
- npgettext = fn(context, singular_key, plural_key, value)
- dnpgettext = fn(domain, context, singular_key, plural_key, value)
For information on how these methods should be used, see: http://slexaxton.github.io/Jed/
-
static
support
(method)¶ Tells the the translator to support calls to
method
.Arguments: - method (string) – The name of the method to support
-
class
Translator
(jed)¶ Constructs functions of the form
f(text)
, wheretext
is a string or aLazyText()
. If a string is provided, the function acts as a no-op. If a lazy translation is given, the function applies the translation using the translator’sjed
instance.Arguments: - jed (Jed) – A jed instance or options to initialise such a jed instance to translate with.
-
static
jed
¶ Direct access to the translator’s
Jed()
instance.
-
apply_translation
(jed, text)¶ Accepts a jed instance and (possibly lazy translation) text and returns the translation result. If a string is provided, the function acts as a no-op. If a lazy translation is given, the function applies the translation using the
jed
given in the constructor.Arguments: - jed (Jed) – The jed instance to translate with
- text (string or
LazyText()
) – Either a string or an object constructed by one ofLazyTranslator()
‘s translation methods.