languages Module

This file is part of the web2py Web Framework
Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
Plural subsystem is created by Vladyslav Kozlovskyy (Ukraine) <dbdevelop@gmail.com>

Translation system

class gluon.languages.translator(langpath, http_accept_language)[source]

Bases: object

This class is instantiated by gluon.compileapp.build_environment as the T object

Example

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

Note

  • there is no need to force since, by default, T uses http_accept_language to determine a translation file.
  • en and en-en are considered different languages!
  • 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
M(message, symbols={}, language=None, lazy=None, filter=None, ftag=None, ns=None)[source]

Gets cached translated markmin-message with inserted parametes if lazy==True lazyT object is returned

apply_filter(message, symbols={}, filter=None, ftag=None)[source]
force(*languages)[source]

Selects language(s) for translation

if a list of languages is passed as a parameter, the 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_possible_languages()[source]

Gets list of all possible languages for current application

get_possible_languages_info(lang=None)[source]

Returns info for selected language or dictionary with all possible languages info from APP/languages/*.py It Returns:

  • a tuple containing:

    langcode, langname, langfile_mtime,
    pluraldict_fname, pluraldict_mtime,
    prules_langcode, nplurals,
    get_plural_id, construct_plural_form
    
    or None
    
  • if lang is NOT defined a 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
    }
    
Parameters:lang (str) – language
get_t(message, prefix='')[source]

Use ## 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 needed to allow markmin syntax to be translated

params_substitution(message, symbols)[source]

Substitutes 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!

plural(word, n)[source]

Gets plural form of word for number n invoked from T()/T.M() in %%{} tag

Note

“word” MUST be defined in current language (T.accepted_language)

Parameters:
  • word (str) – word in singular
  • n (numeric) – number plural form created for
Returns:

word – word in appropriate singular/plural form

Return type:

str

set_current_languages(*languages)[source]

Sets current AKA “default” languages Setting one of this languages makes the force() function to turn translation off

translate(message, symbols)[source]

Gets cached translated message with inserted parameters(symbols)

gluon.languages.findT(path, language='en')[source]

Note

Must be run by the admin app

gluon.languages.update_all_languages(application_path)[source]

Note

Must be run by the admin app