web2py 2.0: New in National Languages Support SubsystemUse default.py (new
future, now in trunk):
New in National Languages Support Subsystem: 1. Improved Algorithm For
Selecting Appropriate Translation Language From Existing In "languages/"
Directory Previously it was necessary to exactly indicate the expected
language codes in filenames from "languages/*" because different Internet
browsers mark, for example, the Russian language in several ways, as "ru", "
ru-ru" or "ru-by", etc. So it was necessary to keep all files ("ru.py", "
ru-ru.py" and "ru-by.py") with the same content in the "languages/"
directory. Especially for this case the list of languages (and not a
particular language) was invented to use in T.current_languages () and
T.force(). Previously it was necessary to make records the following way:
T.set_current_languages
('en','en-au','en-bz','en-ca','en-cb','en-gb','en-ie','en-jm','en-nz','en-ph','en-tt','en-us','en-za','en-zw')
:)))
What has changed? The way of language selection in T.force() has changed.
Now the language selection algorithm is the following:
Let given language be: "aa-bb"
Language file search way is: "aa-bb.py" -> "aa.py" -> "aa*.py"
So now all the languages such as "aa*" will match a file with filename:
aa.py
This allows us to define default language in each language group (e.g. "en*",
"ru*", "uk*", "fr*", ...).
For example, having "pt-pt.py" and "pt-br.py" translation files, we can
make one of them default in the "pt" language group and save it as "pt.py".
Let it be "pt-pt.py" file. Then all "pt*" language codes (except "pt-br",
which we have specified) will switch to "pt-pt" in the file "pt.py"
The last step is especially interesting -> aa*.py
This selection allows you to select the first *"parallel"* language, if
there is no default language in a group.
For example, if we leave both language files pt-pt.py and pt-br.py, without
creating a pt.py file, the first language that comes in the list (most
likely in alphabetical order) will be selected when you specify the
language code as 'pt' (it may be necessary when using "languages
/default.py" file, see below).
However here comes the question - what specific language is default in the '
pt' group: 'pt-pt' or 'pt-br'? Now we cannot learn it from the file name.
Here we come to the following improvements:
2. Pseudo-keys: '!langcode!'' And '!langname!' Two pseudo-keys are added to
language files:
- !langcode! - specifies the real language code (by default it is the
same as the language code from filename)
- !langname! - specifies the language name *in national subscription*.
It is used to select language by a customer. Why is the language name
written in national subscription? That is because a customer cannot know
English, right? She will select the world which she can read. By default
this string is the same as the !langcode!
So in uk.py (*Ukrainian*) language file we can write:
{
'!langcode!': 'uk-ua',
'!langname!': 'Українська',
...
}
Why do these pseudo-keys start with '!'? - That is because ASCII-code of
'!' is less than codes of all other printable characters (except SPACE).
And dictionary is saved in alphabetical order (see write_dict() in
gluon/languages.py). So these keys will be in the first positions in
language files.
A new function T.get_possible_languages_info(lang=None) which gets
information from these pseudo-keys is added to translator class. This
function returns information (tuple(langcode, langname, lang_mtime)) about
the specified language or dictionary with all possible languages from
APP/languages directory. The language code from filename is the key in this
directory.
- language code from filename is stored in T.accepted_language (uk.py==>
T.accepted_language == 'uk')
- language code from !langcode! pseudo-keys can be accessed using
T('!langcode!') or T.get_possible_languages_info(T.accepted_language)[0]
- language name in national subscription can be accessed using
T('!langname!') or T.get_possible_languages_info(T.accepted_language)[1]
*3*. Language File "languages/default.py" From now on, any language can be
declared as system default! It is necessary for monolingual/bilingual
websites in languages other than English. First, let give the definition:
what is the system-default-language? It is a language in which messages are
written in source files using T() (see below) functions: e.g. T("message in
default-language").
In Web2py Core all messages are written in English. But the user
application can use a different language in T() function call. To use the
user language as the default language it is necessary to translate all
web2py core messages into this language. The languages/default.py is used
for this purpose. How it works:
*MESSAGES IN T("") MESSAGES IN DEFAULT LANGUAGE
MESSAGES IN AA LANGUAGE*
CORE T("English Msg") =>default.py=> "User lang msg"
CORE T("English Msg") =>AA.py ====================================>
"AA language message"
USERAPP T("User lang msg") =============> "User lang msg"
USERAPP T("User lang msg") =>default.py=> "Changed User lang msg" (change
msg w/o changing sources)
USERAPP T("User lang msg") =>AA.py ====================================>
"AA language message"
*Additional Benefits:* default.py is also convenient for english users.
They can change any message (english => english) or date format without
sources correction, simply stating "translation" in default.py
To make your language the default language it is enough to rename the
language file to default.py:
# cd welcome/languages
# mv uk.py default.py
and make sure that pseudo-keys '!langcode!' and '!langname!' are correctly
filled. E.g.:
$cat default.py
{
'!langcode!': 'en-us',
'!langname!': 'English (US)',
'Here I will put the awesome title': 'My Title', # <<< now you can change
"default" messages with your ones
...
}
Using default.py we don't get default language in 'en' group (because our
filename is 'default' but not an language code). But using third rule from
p.1 all "en*" languages will be successfully switched to 'en-us' "parallel"
language.
*NOTE:* default.py is not automatically updated with translations (using
translator.findT()) like the other language files, so any changes should be
done manually.
With the best regards,
Vladyslav Kozlovskyy (Ukraine)
P.S. Please fill free to send any bug reports, propositions or questions to
me about this subsystem.
Четвер, 12 липня 2012 р. 20:37:16 UTC+3 користувач rochacbruno написав:
>
> I need something like this.
>
> <h1> {{=T("Here I will put the awesome title")}} </h1>
>
> So I will create files for languages:
>
> en.py
>
> {"Here I will put the awesome title": "My Title"}
>
> pt-br.py
>
> {"Here I will put the awesome title": "Meu título"}
>
> an so..
>
> Should it works?
>