mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Add fallback argument to translation(). Request fallbacks on install.
Fixes #500595.
This commit is contained in:
parent
57f61fb80b
commit
1be6419871
3 changed files with 12 additions and 4 deletions
|
@ -123,7 +123,8 @@ If no such file is found, then \code{None} is returned.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{translation}{domain\optional{, localedir\optional{,
|
\begin{funcdesc}{translation}{domain\optional{, localedir\optional{,
|
||||||
languages\optional{, class_}}}}
|
languages\optional{,
|
||||||
|
class_,\optional{fallback}}}}}
|
||||||
Return a \class{Translations} instance based on the \var{domain},
|
Return a \class{Translations} instance based on the \var{domain},
|
||||||
\var{localedir}, and \var{languages}, which are first passed to
|
\var{localedir}, and \var{languages}, which are first passed to
|
||||||
\function{find()} to get the
|
\function{find()} to get the
|
||||||
|
@ -132,7 +133,9 @@ identical \file{.mo} file names are cached. The actual class instantiated
|
||||||
is either \var{class_} if provided, otherwise
|
is either \var{class_} if provided, otherwise
|
||||||
\class{GNUTranslations}. The class's constructor must take a single
|
\class{GNUTranslations}. The class's constructor must take a single
|
||||||
file object argument. If no \file{.mo} file is found, this
|
file object argument. If no \file{.mo} file is found, this
|
||||||
function raises \exception{IOError}.
|
function raises \exception{IOError} if \var{fallback} is false
|
||||||
|
(which is the default), and returns a \class{NullTranslations} instance
|
||||||
|
if \var{fallback} is true.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{install}{domain\optional{, localedir\optional{, unicode}}}
|
\begin{funcdesc}{install}{domain\optional{, localedir\optional{, unicode}}}
|
||||||
|
|
|
@ -230,11 +230,14 @@ def find(domain, localedir=None, languages=None):
|
||||||
# a mapping between absolute .mo file path and Translation object
|
# a mapping between absolute .mo file path and Translation object
|
||||||
_translations = {}
|
_translations = {}
|
||||||
|
|
||||||
def translation(domain, localedir=None, languages=None, class_=None):
|
def translation(domain, localedir=None, languages=None,
|
||||||
|
class_=None, fallback=0):
|
||||||
if class_ is None:
|
if class_ is None:
|
||||||
class_ = GNUTranslations
|
class_ = GNUTranslations
|
||||||
mofile = find(domain, localedir, languages)
|
mofile = find(domain, localedir, languages)
|
||||||
if mofile is None:
|
if mofile is None:
|
||||||
|
if fallback:
|
||||||
|
return NullTranslations()
|
||||||
raise IOError(ENOENT, 'No translation file found for domain', domain)
|
raise IOError(ENOENT, 'No translation file found for domain', domain)
|
||||||
key = os.path.abspath(mofile)
|
key = os.path.abspath(mofile)
|
||||||
# TBD: do we need to worry about the file pointer getting collected?
|
# TBD: do we need to worry about the file pointer getting collected?
|
||||||
|
@ -248,7 +251,7 @@ def translation(domain, localedir=None, languages=None, class_=None):
|
||||||
|
|
||||||
|
|
||||||
def install(domain, localedir=None, unicode=0):
|
def install(domain, localedir=None, unicode=0):
|
||||||
translation(domain, localedir).install(unicode)
|
translation(domain, localedir, fallback=1).install(unicode)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,8 @@ Library
|
||||||
arbitrary shell code can't be executed because a bogus URL was
|
arbitrary shell code can't be executed because a bogus URL was
|
||||||
passed in.
|
passed in.
|
||||||
|
|
||||||
|
- gettext.translation has an optional fallback argument.
|
||||||
|
|
||||||
Tools/Demos
|
Tools/Demos
|
||||||
|
|
||||||
Build
|
Build
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue