gettext --- å€èšèªåœéåãµãŒãã¹Â¶
ãœãŒã¹ã³ãŒã: Lib/gettext.py
The gettext module provides internationalization (I18N) and localization
(L10N) services for your Python modules and applications. It supports both the
GNU gettext message catalog API and a higher level, class-based API that may
be more appropriate for Python files. The interface described below allows you
to write your module and application messages in one natural language, and
provide a catalog of translated messages for running under different natural
languages.
ããã§ã¯ Python ã®ã¢ãžã¥ãŒã«ãã¢ããªã±ãŒã·ã§ã³ãå°ååããããã®ããã€ãã®ãã³ããæäŸããŠããŸãã
GNU gettext API¶
The gettext module defines the following API, which is very similar to
the GNU gettext API. If you use this API you will affect the
translation of your entire application globally. Often this is what you want if
your application is monolingual, with the choice of language dependent on the
locale of your user. If you are localizing a Python module, or if your
application needs to switch languages on the fly, you probably want to use the
class-based API instead.
- gettext.bindtextdomain(domain, localedir=None)¶
Bind the domain to the locale directory localedir. More concretely,
gettextwill look for binary.mofiles for the given domain using the path (on Unix):localedir/language/LC_MESSAGES/domain.mo, where language is searched for in the environment variablesLANGUAGE,LC_ALL,LC_MESSAGES, andLANGrespectively.localedir ãçç¥ãããã
Noneã®å ŽåãçŸåš domain ã«å¯Ÿå¿ä»ããããŠãããã±ãŒã«ãã£ã¬ã¯ããªãè¿ãããŸãã [1]
- gettext.textdomain(domain=None)¶
çŸåšã®ã°ããŒãã«ãã¡ã€ã³ã倿Žããã調ã¹ããããŸãã domain ã
Noneã®å ŽåãçŸåšã®ã°ããŒãã«ãã¡ã€ã³ãè¿ãããŸãããã以å€ã®å Žåã«ã¯ãã°ããŒãã«ãã¡ã€ã³ã« domain ãèšå®ãããã®èšå®ãããã°ããŒãã«ãã¡ã€ã³ãè¿ããŸãã
- gettext.gettext(message, /)¶
Return the localized translation of message, based on the current global domain, language, and locale directory. This function is usually aliased as
_()in the local namespace (see examples below).
- gettext.dgettext(domain, message, /)¶
gettext()ãšåæ§ã§ãããæå®ããã domain ããã¡ãã»ãŒãžãæ¢ããŸãã
- gettext.ngettext(singular, plural, n, /)¶
gettext()ãšåæ§ã§ãããè€æ°åœ¢ãèæ ®ããŠããŸãã 翻蚳ãèŠã€ãã£ãå Žåãè€æ°åœ¢ã®éžæå ¬åŒã n ã«é©çšãããã®çµæåŸãããã¡ãã»ãŒãžãè¿ããŸã (èšèªã«ãã£ãŠã¯äºã€ä»¥äžã®è€æ°åœ¢ããããŸã)ã 翻蚳ãèŠã€ãããªãã£ãå Žåã n ã 1 ãªã singular ãè¿ããŸã; ããã§ãªãå Žå plural ãè¿ããŸããè€æ°åœ¢ã®éžæå ¬åŒã¯ã«ã¿ãã°ã®ãããããååŸãããŸãã éžæå ¬åŒã¯èªç±å€æ° n ãæã€ C ãŸã㯠Python ã®åŒã§ã; ãã®åŒã®è©äŸ¡çµæã¯ã«ã¿ãã°ã«ããè€æ°åœ¢ã®ã€ã³ããã¯ã¹ã«ãªããŸãã
.poãã¡ã€ã«ã§çšããããè©³çŽ°ãªææ³ãšãæ§ã ãªèšèªã«ãããéžæå ¬åŒã«ã€ããŠã¯ GNU gettext ããã¥ã¡ã³ã ãåç §ããŠãã ããã
- gettext.dngettext(domain, singular, plural, n, /)¶
ngettext()ãšåæ§ã§ãããæå®ããã domain ããã¡ãã»ãŒãžãæ¢ããŸãã
- gettext.pgettext(context, message, /)¶
- gettext.dpgettext(domain, context, message, /)¶
- gettext.npgettext(context, singular, plural, n, /)¶
- gettext.dnpgettext(domain, context, singular, plural, n, /)¶
Similar to the corresponding functions without the
pin the prefix (that is,gettext(),dgettext(),ngettext(),dngettext()), but the translation is restricted to the given message context.Added in version 3.8.
Note that GNU gettext also defines a dcgettext() method, but
this was deemed not useful and so it is currently unimplemented.
以äžã«ãã® API ã®å žåçãªäœ¿çšæ³ã瀺ããŸã:
import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print(_('This is a translatable string.'))
ã¯ã©ã¹åœ¢åŒã® API¶
The class-based API of the gettext module gives you more flexibility and
greater convenience than the GNU gettext API. It is the recommended
way of localizing your Python applications and modules. gettext defines
a GNUTranslations class which implements the parsing of GNU .mo format
files, and has methods for returning strings. Instances of this class can also
install themselves in the built-in namespace as the function _().
- gettext.find(domain, localedir=None, languages=None, all=False)¶
ãã®é¢æ°ã¯æšæºçãª
.moãã¡ã€ã«æ€çŽ¢ã¢ã«ãŽãªãºã ãå®è£ ããŠããŸããtextdomain()ãšåããã domain ãåŒæ°ã«ãšããŸãããªãã·ã§ã³ã® localedir ã¯bindtextdomain()ãšåãã§ãããŸããªãã·ã§ã³ã® languages ã¯æååãåæãããªã¹ãã§ãåæååã¯èšèªã³ãŒãã衚ããŸããlocaledir ãäžããããŠããªãå Žåãæšæºã®ã·ã¹ãã ãã±ãŒã«ãã£ã¬ã¯ããªã䜿ãããŸãã [2] languages ãäžããããªãã£ãå Žåã以äžã®ç°å¢å€æ°:
LANGUAGEãLC_ALLãLC_MESSAGESãããã³LANGãæ€çŽ¢ãããŸãã空ã§ãªãå€ãè¿ããæåã®åè£ã languages 倿°ãšããŠäœ¿ãããŸãããã®ç°å¢å€æ°ã¯èšèªåãã³ãã³ã§åãã¡æžããããªã¹ããå«ãã§ããªããã°ãªããŸãããfind()ã¯ãã®æååãã³ãã³ã§åå²ããèšèªã³ãŒãã®åè£ãªã¹ããçæããŸããfind()ã¯æ¬¡ã«èšèªã³ãŒããå±éããã³æ£èŠåãããªã¹ãã®åèŠçŽ ã«ã€ããŠã以äžã®ãã¹æ§æ:localedir/language/LC_MESSAGES/domain.moãããªãå®åšãããã¡ã€ã«ã®æ¢çŽ¢ãå埩çã«è¡ããŸãã
find()ã¯äžèšã®ãããªå®åšãããã¡ã€ã«ã§æåã«èŠã€ãã£ããã®ãè¿ããŸãã該åœãããã¡ã€ã«ãèŠã€ãããªãã£ãå ŽåãNoneãè¿ãããŸãã all ãäžããããŠããã°ãå šãã¡ã€ã«åã®ãªã¹ããèšèªãªã¹ããŸãã¯ç°å¢å€æ°ã§æå®ãããŠããé çªã«äžŠã¹ããããã®ãè¿ããŸãã
- gettext.translation(domain, localedir=None, languages=None, class_=None, fallback=False)¶
Return a
*Translationsinstance based on the domain, localedir, and languages, which are first passed tofind()to get a list of the associated.mofile paths. Instances with identical.mofile names are cached. The actual class instantiated is class_ if provided, otherwiseGNUTranslations. The class's constructor must take a single file object argument.è€æ°ã®
.moãã¡ã€ã«ããã£ãå ŽåãåŸãã®ãã¡ã€ã«ã¯åã®ãã¡ã€ã«ã®ãã©ãŒã«ããã¯ãšããŠå©çšãããŸãã ãã©ãŒã«ããã¯ã®èšå®ã®ããã«ãcopy.copy()ã䜿ããã£ãã·ã¥ãã翻蚳ãªããžã§ã¯ããè€è£œããŸã; ããããããšã§ãå®éã®ã€ã³ã¹ã¿ã³ã¹ããŒã¿ã¯ãã£ãã·ã¥ã®ãã®ãšå ±æããããŸãŸã«ãªããŸãã.moãã¡ã€ã«ãèŠã€ãããªãã£ãå Žåã fallback ãåœ (ããã©ã«ãå€) ãªããã®é¢æ°ã¯OSErrorãéåºãã fallback ãçãªãNullTranslationsã€ã³ã¹ã¿ã³ã¹ãè¿ãããŸããããŒãžã§ã³ 3.3 ã§å€æŽ: 以åã¯
IOErrorãéåºãããŸãã; ããã¯çŸåšOSErrorã®ãšã€ãªã¢ã¹ã§ããããŒãžã§ã³ 3.11 ã§å€æŽ: codeset parameter is removed.
- gettext.install(domain, localedir=None, *, names=None)¶
This installs the function
_()in Python's builtins namespace, based on domain and localedir which are passed to the functiontranslation().names ãã©ã¡ãŒã¿ã«ã€ããŠã¯ã翻蚳ãªããžã§ã¯ãã®
install()ã¡ãœããã®èª¬æãåç §ãã ãããAs seen below, you usually mark the strings in your application that are candidates for translation, by wrapping them in a call to the
_()function, like this:print(_('This string will be translated.'))
For convenience, you want the
_()function to be installed in Python's builtins namespace, so it is easily accessible in all modules of your application.ããŒãžã§ã³ 3.11 ã§å€æŽ: names is now a keyword-only parameter.
NullTranslations ã¯ã©ã¹Â¶
翻蚳ã¯ã©ã¹ã¯ãå
ã®ãœãŒã¹ãã¡ã€ã«äžã®ã¡ãã»ãŒãžæååãã翻蚳ãããã¡ãã»ãŒãžæååãžã®å€æåŠçãå®éã«å®è£
ãããŠããã¯ã©ã¹ã§ãã
å
šãŠã®ç¿»èš³ã¯ã©ã¹ã§åºåºã¯ã©ã¹ãšããŠäœ¿ãããŠããã¯ã©ã¹ã NullTranslations ã§ã; ãã®ã¯ã©ã¹ã¯ãç¬èªã®ç¿»èš³ã¯ã©ã¹ãå®è£
ããã®ã«äœ¿ããåºæ¬çãªã€ã³ã¿ãŒãã§ãŒã¹ãæäŸããŠããŸãã
以äžã« NullTranslations ã®ã¡ãœããã瀺ããŸã:
- class gettext.NullTranslations(fp=None)¶
ãªãã·ã§ã³ã® ãã¡ã€ã«ãªããžã§ã¯ã fp ãåããŸãããã®åŒæ°ã¯åºåºã¯ã©ã¹ã§ã¯ç¡èŠãããŸãããã®ã¡ãœãã㯠"ä¿è·ããã (protected)" ã€ã³ã¹ã¿ã³ã¹å€æ° _info ããã³ _charset ãåæåããŸãããããã®å€æ°ã®å€ã¯æŽŸçã¯ã©ã¹ã§èšå®ããããšãã§ããŸããåæ§ã« _fallback ãåæåããŸããããã®å€ã¯
add_fallback()ã§èšå®ãããŸãããã®åŸã fp ãNoneã§ãªãå Žåself._parse(fp)ãåŒã³åºããŸãã- _parse(fp)¶
åºåºã¯ã©ã¹ã§ã¯äœãããªã (no-op) ããã«ãªã£ãŠããŸãããã®ã¡ãœããã®åœ¹å²ã¯ãã¡ã€ã«ãªããžã§ã¯ã fp ãåŒæ°ã«åãããã¡ã€ã«ããããŒã¿ãèªã¿åºããã¡ãã»ãŒãžã«ã¿ãã°ãåæåããããšã§ãããµããŒããããŠããªãã¡ãã»ãŒãžã«ã¿ãã°åœ¢åŒã䜿ã£ãŠããå Žåããã®åœ¢åŒãè§£éããããã«ã¯ãã®ã¡ãœãããäžæžãããªããŠã¯ãªããŸããã
- add_fallback(fallback)¶
fallback ãçŸåšã®ç¿»èš³ãªããžã§ã¯ãã®ä»£æ¿ãªããžã§ã¯ããšããŠè¿œå ããŸãã翻蚳ãªããžã§ã¯ããäžããããã¡ãã»ãŒãžã«å¯ŸããŠç¿»èš³ã¡ãã»ãŒãžãæäŸã§ããªãå Žåããã®ä»£æ¿ãªããžã§ã¯ãã«åãåãããããšã«ãªããŸãã
- gettext(message, /)¶
ãã©ãŒã«ããã¯ãèšå®ãããŠããå Žåããã©ãŒã«ããã¯ã®
gettext()ã«åŠçãç§»è²ããŸãã ããã§ãªãå ŽåãåŒæ°ãšããŠåãåã£ã message ãè¿ããŸãã æŽŸçã¯ã©ã¹ã§äžæžãããã¡ãœããã§ãã
- ngettext(singular, plural, n, /)¶
ãã©ãŒã«ããã¯ãèšå®ãããŠããå Žåããã©ãŒã«ããã¯ã®
ngettext()ã«åŠçãç§»è²ããŸãã ããã§ãªãå Žåã n ã 1 ãªã singular ãè¿ããŸã; ãã以å€ãªã plural ãè¿ããŸãã æŽŸçã¯ã©ã¹ã§äžæžãããã¡ãœããã§ãã
- pgettext(context, message, /)¶
代æ¿ãªããžã§ã¯ããèšå®ãããŠããå Žåã
pgettext()ã代æ¿ãªããžã§ã¯ãã«è»¢éããŸããããã§ãªãå Žåã翻蚳ãããã¡ãã»ãŒãžãè¿ããŸããæŽŸçã¯ã©ã¹ã§äžæžãããã¡ãœããã§ããAdded in version 3.8.
- npgettext(context, singular, plural, n, /)¶
代æ¿ãªããžã§ã¯ããèšå®ãããŠããå Žåã
npgettext()ã代æ¿ãªããžã§ã¯ãã«è»¢éããŸããããã§ãªãå Žåã翻蚳ãããã¡ãã»ãŒãžãè¿ããŸããæŽŸçã¯ã©ã¹ã§äžæžãããã¡ãœããã§ããAdded in version 3.8.
- info()¶
Return a dictionary containing the metadata found in the message catalog file.
- charset()¶
ã¡ãã»ãŒãžã«ã¿ãã°ãã¡ã€ã«ã®ãšã³ã³ãŒãã£ã³ã°ãè¿ããŸãã
- install(names=None)¶
ãã®ã¡ãœããã¯
gettext()ãçµã¿èŸŒã¿åå空éã«ã€ã³ã¹ããŒã«ãã倿°_ã«æçžããŸããIf the names parameter is given, it must be a sequence containing the names of functions you want to install in the builtins namespace in addition to
_(). Supported names are'gettext','ngettext','pgettext', and'npgettext'.Note that this is only one way, albeit the most convenient way, to make the
_()function available to your application. Because it affects the entire application globally, and specifically the built-in namespace, localized modules should never install_(). Instead, they should use this code to make_()available to their module:import gettext t = gettext.translation('mymodule', ...) _ = t.gettext
This puts
_()only in the module's global namespace and so only affects calls within this module.ããŒãžã§ã³ 3.8 ã§å€æŽ:
'pgettext'ãš'npgettext'ã远å ãããŸããã
GNUTranslations ã¯ã©ã¹Â¶
The gettext module provides one additional class derived from
NullTranslations: GNUTranslations. This class overrides
_parse() to enable reading GNU gettext format .mo files
in both big-endian and little-endian format.
GNUTranslations parses optional metadata out of the translation
catalog. It is convention with GNU gettext to include metadata as
the translation for the empty string. This metadata is in RFC 822-style
key: value pairs, and should contain the Project-Id-Version key. If the
key Content-Type is found, then the charset property is used to
initialize the "protected" _charset instance variable, defaulting to
None if not found. If the charset encoding is specified, then all message
ids and message strings read from the catalog are converted to Unicode using
this encoding, else ASCII is assumed.
Since message ids are read as Unicode strings too, all *gettext() methods
will assume message ids as Unicode strings, not byte strings.
The entire set of key/value pairs are placed into a dictionary and set as the
"protected" _info instance variable.
.mo ãã¡ã€ã«ã®ããžãã¯ãã³ããŒãäžæ£ãªå Žåããã¡ãžã£ãŒããŒãžã§ã³çªå·ãäºæãããªããã®ã®å Žåããããã¯ãã®ä»ã®åé¡ããã¡ã€ã«ã®èªã¿åºãäžã«çºçããå Žåã GNUTranslations ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹åã§ OSError ãéåºãããããšããããŸãã
- class gettext.GNUTranslations¶
以äžã®ã¡ãœããã¯åºåºã¯ã©ã¹ã®å®è£ ãããªãŒãã©ã€ããããŠããŸã:
- gettext(message, /)¶
ã«ã¿ãã°ãã message id ãæ€çŽ¢ããŠã察å¿ããã¡ãã»ãŒãžæååã Unicode ã§ãšã³ã³ãŒãããŠè¿ããŸãã message id ã«å¯Ÿå¿ãããšã³ããªãã«ã¿ãã°ã«ååšããããã©ãŒã«ããã¯ãèšå®ãããŠããå Žåãæ€çŽ¢åŠçããã©ãŒã«ããã¯ã®
gettext()ã¡ãœããã«ç§»è²ããŸãã ãã以å€ã®å Žåã¯ã message id èªäœãè¿ãããŸãã
- ngettext(singular, plural, n, /)¶
ã¡ãã»ãŒãž id ã«å¯Ÿããè€æ°åœ¢ãæ€çŽ¢ããŸããã«ã¿ãã°ã«å¯Ÿããæ€çŽ¢ã§ã¯ singular ãã¡ãã»ãŒãž id ãšããŠçšãããã n ã«ã¯ã©ã®è€æ°åœ¢ãçšããããæå®ããŸããè¿ãããã¡ãã»ãŒãžæåå㯠Unicode æååã§ãã
ã¡ãã»ãŒãž id ãã«ã¿ãã°äžã«èŠã€ãããããã©ãŒã«ããã¯ãæå®ãããŠããå Žåã¯ãã¡ãã»ãŒãžæ€çŽ¢èŠæ±ã¯ãã©ãŒã«ããã¯ã®
ngettext()ã¡ãœããã«ç§»è²ãããŸãã ãã以å€ã®å Žåã n ã 1 ãªãã° singular ãè¿ããããã以å€ãªã plural ãè¿ãããŸãã以äžã«äŸã瀺ããŸãã:
n = len(os.listdir('.')) cat = GNUTranslations(somefile) message = cat.ngettext( 'There is %(num)d file in this directory', 'There are %(num)d files in this directory', n) % {'num': n}
- pgettext(context, message, /)¶
ã«ã¿ãã°ãã context ãš message id ãæ€çŽ¢ããŠã察å¿ããã¡ãã»ãŒãžæååãã Unicode ã§ãšã³ã³ãŒãããŠè¿ããŸãã message id ãš context ã«å¯Ÿãããšã³ããªãã«ã¿ãã°ã«ååšããããã©ãŒã«ããã¯ãèšå®ãããŠããå Žåããã©ãŒã«ããã¯æ€çŽ¢ã¯ãªããžã§ã¯ãã®
pgettext()ã¡ãœããã«è»¢éãããŸããããã§ãªãå Žåã message id èªäœãè¿ãããŸããAdded in version 3.8.
- npgettext(context, singular, plural, n, /)¶
ã¡ãã»ãŒãž id ã«å¯Ÿããè€æ°åœ¢ãæ€çŽ¢ããŸããã«ã¿ãã°ã«å¯Ÿããæ€çŽ¢ã§ã¯ singular ãã¡ãã»ãŒãž id ãšããŠçšãããã n ã«ã¯ã©ã®è€æ°åœ¢ãçšããããæå®ããŸãã
context ã«å¯Ÿããã¡ãã»ãŒãž id ãã«ã¿ãã°äžã«èŠã€ãããããã©ãŒã«ããã¯ãªããžã§ã¯ããæå®ãããŠããå Žåãã¡ãã»ãŒãžæ€çŽ¢èŠæ±ã¯ãã©ãŒã«ããã¯ãªããžã§ã¯ãã®
npgettext()ã¡ãœããã«è»¢éãããŸããããã§ãªãå Žåã n ã 1 ãªãã° singular ãè¿ããããã以å€ã«å¯ŸããŠã¯ plural ãè¿ãããŸããAdded in version 3.8.
Solaris ã¡ãã»ãŒãžã«ã¿ãã°æ©æ§ã®ãµããŒã¶
Solaris ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ã¯ãç¬èªã® .mo ãã€ããªãã¡ã€ã«åœ¢åŒãå®çŸ©ããŠããŸããããã®åœ¢åŒã«é¢ããããã¥ã¡ã³ããæã«å
¥ããªããããçŸæç¹ã§ã¯ãµããŒããããŠããŸããã
Catalog ã³ã³ã¹ãã©ã¯ã¿Â¶
GNOME uses a version of the gettext module by James Henstridge, but this
version has a slightly different API. Its documented usage was:
import gettext
cat = gettext.Catalog(domain, localedir)
_ = cat.gettext
print(_('hello world'))
For compatibility with this older module, the function Catalog() is an
alias for the translation() function described above.
ãã®ã¢ãžã¥ãŒã«ãš Henstridge ã®ããŒãžã§ã³ãšã®éã«ã¯äžã€çžéç¹ããããŸã: 圌ã®ã«ã¿ãã°ãªããžã§ã¯ãã¯ãããåã® API ãä»ããã¢ã¯ã»ã¹ããµããŒããããŠããŸãããããã® API ã¯äœ¿ãããŠããªãããããçŸåšã¯ãµããŒããããŠããŸããã
ããã°ã©ã ãã¢ãžã¥ãŒã«ãåœéåãã¶
åœéå (I18N, I-nternationalizatio-N) ãšã¯ãããã°ã©ã ãè€æ°ã®èšèªã«å¯Ÿå¿ãããæäœãæããŸããå°åå (L10N, L-ocalizatio-N) ãšã¯ããã§ã«åœéåãããŠããããã°ã©ã ãç¹å®å°åã®èšèªãæåçãªäºæ ã«å¯Ÿå¿ãããããšãæããŸããPython ããã°ã©ã ã«å€èšèªã¡ãã»ãŒãžæ©èœã远å ããã«ã¯ã以äžã®æé ãèžãå¿ èŠããããŸã:
ããã°ã©ã ãã¢ãžã¥ãŒã«ã§ç¿»èš³å¯Ÿè±¡ãšããæååã«ç¹æ®ãªããŒã¯ãã€ããŠæºåããŸã
ããŒã¯ã¥ãããããã¡ã€ã«ã«äžé£ã®ããŒã«ãèµ°ãããçã®ã¡ãã»ãŒãžã«ã¿ãã°ãçæããŸã
ç¹å®ã®èšèªãžã®ã¡ãã»ãŒãžã«ã¿ãã°ã®ç¿»èš³ãäœæããŸã
use the
gettextmodule so that message strings are properly translated
In order to prepare your code for I18N, you need to look at all the strings in
your files. Any string that needs to be translated should be marked by wrapping
it in _('...') --- that is, a call to the function _. For example:
filename = 'mylog.txt'
message = _('writing a log message')
with open(filename, 'w') as fp:
fp.write(message)
ãã®äŸã§ã¯ãæåå 'writing a log message' ã翻蚳察象åè£ãšããŠããŒã¯ä»ããããŠãããæåå 'mylog.txt' ããã³ 'w' ã¯ãããŠããŸããã
ç¿»èš³å¯Ÿè±¡ã®æååãæœåºããããŒã«ããããŸãã
ãªãªãžãã«ã® GNU gettext 㯠C ãš C++ ã®ãœãŒã¹ã³ãŒããããµããŒãããŸããããæ¡åŒµçã® xgettext 㯠Python ãå«ããå€ãã®èšèªã§æžãããã³ãŒããèªã¿åãã翻蚳ã§ããæååãçºèŠããŸãã
Babel 㯠Python ã®åœéåã©ã€ãã©ãªã§ã翻蚳æååã®æœåºãšã¡ãã»ãŒãžã«ã¿ãã°ã®ã³ã³ãã€ã«ãè¡ã pybabel ã¹ã¯ãªããããããŸãã
François Pinard ãéçºãã xpot ãšåŒã°ããããã°ã©ã ã¯åããããªåŠçãè¡ãã圌㮠po-utils package ã®äžéšãšããŠå©çšå¯èœã§ãã
(Python ã«ã¯ pygettext.py ããã³ msgfmt.py ãšããååã® pure-Python çããã°ã©ã ããããŸã;
ãããã€ã³ã¹ããŒã«ããŠããã Python ãã£ã¹ããªãã¥ãŒã·ã§ã³ããããŸãã
pygettext.py 㯠xgettext ã«äŒŒãããã°ã©ã ã§ãã Python ã®ãœãŒã¹ã³ãŒãããçè§£ã§ããã C ã C++ ã®ãããªä»ã®ããã°ã©ãã³ã°èšèªãæ±ããŸããã
pygettext.py 㯠xgettext ãšåæ§ã®ã³ãã³ãã©ã€ã³ã€ã³ã¿ãŒãã§ãŒã¹ããµããŒãããŠããŸã;
詳ããäœ¿ãæ¹ã«ã€ããŠã¯ pygettext.py --help ãšå®è¡ããŠãã ããã
msgfmt.py 㯠GNU msgfmt ãšãã€ããªäºææ§ããããŸãã
ãã®2ã€ã®ããã°ã©ã ãããã°ã GNU gettext ããã±ãŒãžã䜿ããã« Python ã¢ããªã±ãŒã·ã§ã³ãåœéåã§ããã§ãããã)
xgettext ã pygettext ã®ãããªããŒã«ã¯ãã¡ãã»ãŒãžã«ã¿ãã°ã§ãã .po ãã¡ã€ã«ãçæããŸãã
ãã®ãã¡ã€ã«ã¯äººéãå€èªå¯èœãªæ§é ãããŠããŠããœãŒã¹ã³ãŒãäžã®ããŒã¯ãçããããæååãšããã®æååã®ä»®çœ®ãã®èš³æãäžç·ã«æžã蟌ãŸããŠããŸãã
Copies of these .po files are then handed over to the
individual human translators who write translations for every
supported natural language. They send back the completed
language-specific versions as a <language-name>.po file that's
compiled into a machine-readable .mo binary catalog file using
the msgfmt program. The .mo files are used by the
gettext module for the actual translation processing at
run-time.
How you use the gettext module in your code depends on whether you are
internationalizing a single module or your entire application. The next two
sections will discuss each case.
ã¢ãžã¥ãŒã«ãå°ååãã¶
ã¢ãžã¥ãŒã«ãå°ååããå Žåãã°ããŒãã«ãªå€æŽãäŸãã°çµã¿èŸŒã¿åå空éãžã®å€æŽãè¡ããªãããã«æ³šæããªããã°ãªããŸãããGNU gettext API ã§ã¯ãªããã¯ã©ã¹åœ¢åŒã® API ã䜿ãã¹ãã§ãã
ä»®ã«å¯Ÿè±¡ã®ã¢ãžã¥ãŒã«åã "spam" ãšããã¢ãžã¥ãŒã«ã®åèšèªã«ããã翻蚳ãåãããã .mo ãã¡ã€ã«ã /usr/share/locale ã« GNU gettext 圢åŒã§çœ®ãããŠãããšããŸãããã®å Žåãã¢ãžã¥ãŒã«ã®æåã§ä»¥äžã®ããã«ããŸã:
import gettext
t = gettext.translation('spam', '/usr/share/locale')
_ = t.gettext
ã¢ããªã±ãŒã·ã§ã³ãå°ååãã¶
If you are localizing your application, you can install the _() function
globally into the built-in namespace, usually in the main driver file of your
application. This will let all your application-specific files just use
_('...') without having to explicitly install it in each file.
åçŽãªå Žåã§ã¯ãåã«ä»¥äžã®çãã³ãŒããã¢ããªã±ãŒã·ã§ã³ã®äž»ãã©ã€ããã¡ã€ã«ã«è¿œå ããã ãã§ã:
import gettext
gettext.install('myapplication')
ãã±ãŒã«ã®èŸæžãèšå®ããå¿
èŠãããå Žåãinstall() 颿°ã«æž¡ãããšãåºæ¥ãŸã:
import gettext
gettext.install('myapplication', '/usr/share/locale')
åäœäž (on the fly) ã«èšèªãåãæ¿ãã¶
å€ãã®èšèªãåæã«ãµããŒãããå¿ èŠãããå Žåãè€æ°ã®ç¿»èš³ã€ã³ã¹ã¿ã³ã¹ãçæããŠãäŸãã°ä»¥äžã®ã³ãŒãã®ããã«ãã€ã³ã¹ã¿ã³ã¹ãæç€ºçã«åãæ¿ããŠãããŸããŸããã:
import gettext
lang1 = gettext.translation('myapplication', languages=['en'])
lang2 = gettext.translation('myapplication', languages=['fr'])
lang3 = gettext.translation('myapplication', languages=['de'])
# start by using language1
lang1.install()
# ... time goes by, user selects language 2
lang2.install()
# ... more time goes by, user selects language 3
lang3.install()
翻蚳åŠçã®é 延解決¶
ã³ãŒããæžãäžã§ã¯ãã»ãšãã©ã®ç¶æ³ã§æååã¯ã³ãŒããããå Žæã§ç¿»èš³ãããŸãããããå Žåã«ãã£ãŠã¯ã翻蚳察象ãšããŠæååãããŒã¯ã¯ãããããã®åŸå®éã«ç¿»èš³ãè¡ãããããã«é å»¶ãããå¿ èŠãçããŸããå€å žçãªäŸã¯ä»¥äžã®ãããªã³ãŒãã§ã:
animals = ['mollusk',
'albatross',
'rat',
'penguin',
'python', ]
# ...
for a in animals:
print(a)
ããã§ããªã¹ã animals å
ã®æååã¯ç¿»èš³å¯Ÿè±¡ãšããŠããŒã¯ã¯ãããããæååãåºåããããŸã§å®éã«ç¿»èš³ãè¡ãã®ã¯é¿ããããšããŸãã
ããããç¶æ³ãåŠçããäžã€ã®æ¹æ³ã以äžã«ç€ºããŸã:
def _(message): return message
animals = [_('mollusk'),
_('albatross'),
_('rat'),
_('penguin'),
_('python'), ]
del _
# ...
for a in animals:
print(_(a))
This works because the dummy definition of _() simply returns the string
unchanged. And this dummy definition will temporarily override any definition
of _() in the built-in namespace (until the del command). Take
care, though if you have a previous definition of _() in the local
namespace.
Note that the second use of _() will not identify "a" as being
translatable to the gettext program, because the parameter
is not a string literal.
ããäžã€ã®åŠçæ³ã¯ã以äžã®äŸã®ãããªããæ¹ã§ã:
def N_(message): return message
animals = [N_('mollusk'),
N_('albatross'),
N_('rat'),
N_('penguin'),
N_('python'), ]
# ...
for a in animals:
print(_(a))
In this case, you are marking translatable strings with the function
N_(), which won't conflict with any definition of _().
However, you will need to teach your message extraction program to
look for translatable strings marked with N_(). xgettext,
pygettext, pybabel extract, and xpot all
support this through the use of the -k command-line switch.
The choice of N_() here is totally arbitrary; it could have just
as easily been MarkThisStringForTranslation().
è¬èŸÂ¶
以äžã®äººã ãããã®ã¢ãžã¥ãŒã«ã®ã³ãŒãããã£ãŒãããã¯ãèšèšã«é¢ããå©èšãéå»ã®å®è£ ããããŠæçãªçµéšè«ã«ããè²¢ç®ãããŠãããŸãã:
Peter Funk
James Henstridge
Juan David Ibáñez Palomar
Marc-André Lemburg
Martin von Löwis
François Pinard
Barry Warsaw
Gustavo Niemeyer
èæ³š