| 1 |
Gettext usage in QBzr |
| 2 |
********************* |
| 3 |
Overview |
| 4 |
-------- |
| 5 |
QBzr uses the standard Python/GNU gettext library to create |
| 6 |
localized interface. To use gettext functions you should import |
| 7 |
the bzrlib.plugins.qbzr.lib.i18n module. |
| 8 |
|
| 9 |
Functions |
| 10 |
--------- |
| 11 |
i18n.py module provides the following functions: |
| 12 |
|
| 13 |
gettext(message) |
| 14 |
return translated message as unicode string. |
| 15 |
|
| 16 |
ngettext(singular, plural, n) |
| 17 |
do plural form lookup and return corresponding translation |
| 18 |
as unicode string. |
| 19 |
|
| 20 |
N_(message) |
| 21 |
return message itself. |
| 22 |
|
| 23 |
install() |
| 24 |
enable translations |
| 25 |
|
| 26 |
uninstall() |
| 27 |
disable translations |
| 28 |
|
| 29 |
By default translations are disabled, and therefore gettext() |
| 30 |
and ngettext() functions return original untranslated message. |
| 31 |
|
| 32 |
To enable translations you should call i18n.install() function. |
| 33 |
This function is called automatically for all q-commands. |
| 34 |
|
| 35 |
N_() and gettext() |
| 36 |
------------------ |
| 37 |
Function N_() has only one purpose: mark some string for translation, |
| 38 |
so GNU xgettext utility can extract such string from source code. |
| 39 |
It does effectively nothing and always returns its argument. |
| 40 |
Function N_() should be called only for a string literal and never |
| 41 |
for a variable. |
| 42 |
|
| 43 |
gettext() main purpose is to do message translation lookup |
| 44 |
when translations are enabled. Its second purpose is to mark some string |
| 45 |
for translation, so GNU xgettext utility can extract such string |
| 46 |
from source code. |
| 47 |
|
| 48 |
Function gettext() can be called either for a string literal or for a variable. |
| 49 |
Argument to gettext() can be either plain string or unicode, but always |
| 50 |
in ascii-encoding. Function gettext() always returns an unicode string. |
| 51 |
|
| 52 |
Valid usage of these functions: |
| 53 |
|
| 54 |
==================== ================================================ |
| 55 |
N_(message) -> message without transformation |
| 56 |
gettext(message) -> translated message (as unicode) |
| 57 |
gettext(variable) -> translated message from variable (as unicode) |
| 58 |
gettext(N_(message)) -> translated message (as unicode) |
| 59 |
==================== ================================================ |
| 60 |
|
| 61 |
Incorrect usage: |
| 62 |
|
| 63 |
========================= ====================== |
| 64 |
N_(N_(message)) -> message |
| 65 |
N_(gettext(message)) -> translated message |
| 66 |
gettext(gettext(message)) -> translated message |
| 67 |
========================= ====================== |
| 68 |
|
| 69 |
Although such usage don't raise runtime error, but never use it in such manner. |
| 70 |
|
| 71 |
How to use gettext() and N_() |
| 72 |
----------------------------- |
| 73 |
Simple rules: |
| 74 |
|
| 75 |
- use N_() at import time, that is, for global and class definitions |
| 76 |
- use gettext() inside functions |
| 77 |
- in the tests gettext() always returns original (non-translated) string |
| 78 |
|
| 79 |
Always call gettext() for strings that will appear [verbatim] in GUI forms. |
| 80 |
|
| 81 |
If you need to have both original and translated string (e.g. original string used as |
| 82 |
dictionary key etc.) then use N_() for declaring such original string, and later |
| 83 |
call gettext() to obtain translated string. See also: |
| 84 |
http://www.gnu.org/software/gettext/manual/html_node/Special-cases.html |
| 85 |
(replace gettext_noop() in those examples with N_()). |