Package gluon :: Module languages :: Class translator
[hide private]
[frames] | no frames]

Class translator

source code

object --+
         |
        translator


this class is instantiated by gluon.compileapp.build_environment
as the T object
::
    T.force(None) # turns off translation
    T.force('fr, it') # forces web2py to translate using fr.py or it.py

    T("Hello World") # translates "Hello World" using the selected file

notice 1: there is no need to force since, by default, T uses
   http_accept_language to determine a translation file.
notice 2:
   en and en-en are considered different languages!
notice 3:
   if language xx-yy is not found force() probes other similar
   languages using such algorithm:
    xx-yy.py -> xx.py -> xx-yy*.py -> xx*.py

Instance Methods [hide private]
 
__init__(self, langpath, http_accept_language)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
get_possible_languages_info(self, lang=None)
return info for selected language or dictionary with all possible languages info from APP/languages/*.py args: *lang* (str): language returns: if *lang* is defined: return tuple(langcode, langname, langfile_mtime, pluraldict_fname, pluraldict_mtime, prules_langcode, nplurals, get_plural_id, construct_plural_form) or None
source code
 
get_possible_languages(self)
get list of all possible languages for current applications
source code
 
set_current_languages(self, *languages)
set current AKA "default" languages setting one of this languages makes force() function turn translation off to use default language
source code
 
plural(self, word, n)
get plural form of word for number *n* NOTE: *word" MUST be defined in current language (T.accepted_language)
source code
 
force(self, *languages)
select language(s) for translation
source code
 
__call__(self, message, symbols={}, language=None, lazy=None)
get cached translated plain text message with inserted parameters(symbols) if lazy==True lazyT object is returned
source code
 
apply_filter(self, message, symbols={}, filter=None, ftag=None) source code
 
M(self, message, symbols={}, language=None, lazy=None, filter=None, ftag=None)
get cached translated markmin-message with inserted parametes if lazy==True lazyT object is returned
source code
 
get_t(self, message, prefix='')
user ## to add a comment into a translation string the comment can be useful do discriminate different possible translations for the same string (for example different locations)
source code
 
params_substitution(self, message, symbols)
substitute parameters from symbols into message using %.
source code
 
translate(self, message, symbols)
get cached translated message with inserted parameters(symbols)
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, langpath, http_accept_language)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

get_possible_languages_info(self, lang=None)

source code 

return info for selected language or dictionary with all
    possible languages info from APP/languages/*.py
args:
    *lang* (str): language
returns:
    if *lang* is defined:
       return tuple(langcode, langname, langfile_mtime,
                    pluraldict_fname, pluraldict_mtime,
                    prules_langcode, nplurals,
                    get_plural_id, construct_plural_form)
                    or None

    if *lang* is NOT defined:
       returns dictionary with all possible languages:
    { langcode(from filename):
        ( langcode,        # language code from !langcode!
          langname,
              # language name in national spelling from !langname!
          langfile_mtime,  # m_time of language file
          pluraldict_fname,# name of plural dictionary file or None (when default.py is not exist)
          pluraldict_mtime,# m_time of plural dictionary file or 0 if file is not exist
          prules_langcode, # code of plural rules language or 'default'
          nplurals,        # nplurals for current language
          get_plural_id,   # get_plural_id() for current language
          construct_plural_form) # construct_plural_form() for current language
    }

plural(self, word, n)

source code 
get plural form of word for number *n*
    NOTE: *word" MUST be defined in current language
          (T.accepted_language)

    invoked from T()/T.M() in %%{} tag
args:
    word (str): word in singular
    n (numeric): number plural form created for

returns:
    (str): word in appropriate singular/plural form

force(self, *languages)

source code 

select language(s) for translation

if a list of languages is passed as a parameter, first language from this list that matches the ones from the possible_languages dictionary will be selected

default language will be selected if none of them matches possible_languages.

get_t(self, message, prefix='')

source code 

user ## to add a comment into a translation string the comment can be useful do discriminate different possible translations for the same string (for example different locations)

T(' hello world ') -> ' hello world ' T(' hello world ## token') -> ' hello world ' T('hello ## world## token') -> 'hello ## world'

the ## notation is ignored in multiline strings and strings that start with ##. this is to allow markmin syntax to be translated

params_substitution(self, message, symbols)

source code 

substitute parameters from symbols into message using %. also parse %%{} placeholders for plural-forms processing. returns: string with parameters NOTE: *symbols* MUST BE OR tuple OR dict of parameters!