Back to index

moin  1.9.0~rc2
Namespaces | Classes | Functions | Variables
MoinMoin.i18n Namespace Reference

Namespaces

namespace  msgfmt
namespace  strings
namespace  tools

Classes

class  Translation

Functions

def po_filename
def i18n_init
def bot_translations
def getDirection
def getText
def requestLanguage
def wikiLanguages
def browserLanguages
def get_browser_language

Variables

tuple logging = log.getLogger(__name__)
 languages = None
dictionary system_pages = {}
dictionary translations = {}

Function Documentation

def MoinMoin.i18n.bot_translations (   request)
Return translations to be used by notification bot

This is called by XML RPC code.

@return: a dict (indexed by language) of dicts of translated strings (indexed by original ones)

Definition at line 118 of file __init__.py.

00118 
00119 def bot_translations(request):
00120     """Return translations to be used by notification bot
00121 
00122     This is called by XML RPC code.
00123 
00124     @return: a dict (indexed by language) of dicts of translated strings (indexed by original ones)
00125     """
00126     translations = {}
00127     po_dir = os.path.join('i18n', 'jabberbot')
00128     encoding = 'utf-8'
00129 
00130     for lang_file in glob.glob(po_filename(request, i18n_dir=po_dir, language='*', domain='JabberBot')):
00131         language, domain, ext = os.path.basename(lang_file).split('.')
00132         t = Translation(language, domain)
00133         f = file(lang_file)
00134         t.load_po(f)
00135         f.close()
00136         t.loadLanguage(request, trans_dir=po_dir)
00137         translations[language] = {}
00138 
00139         for key, text in t.raw.items():
00140             translations[language][key] = text
00141 
00142     return translations

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.i18n.browserLanguages (   request)
Return the accepted languages as set in the user browser.

Parse the HTTP headers and extract the accepted languages, according to:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4

Return a list of languages and base languages - as they are specified in
the request, normalizing to lower case.

Definition at line 376 of file __init__.py.

00376 
00377 def browserLanguages(request):
00378     """
00379     Return the accepted languages as set in the user browser.
00380 
00381     Parse the HTTP headers and extract the accepted languages, according to:
00382     http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
00383 
00384     Return a list of languages and base languages - as they are specified in
00385     the request, normalizing to lower case.
00386     """
00387     fallback = []
00388     accepted = request.accept_languages
00389     if accepted:
00390         # Add base language for each sub language. If the user specified
00391         # a sub language like "en-us", we will try to to provide it or
00392         # a least the base language "en" in this case.
00393         for lang, quality in accepted:
00394             lang = lang.lower()
00395             fallback.append(lang)
00396             if '-' in lang:
00397                 baselang = lang.split('-')[0]
00398                 fallback.append(baselang)
00399     return fallback

Here is the caller graph for this function:

Return the language that is supported by wiki and what user browser
would prefer to get. Return empty string if there is no such language
or language_ignore_browser is true.

@param request: the request object
@rtype: string
@return: ISO language code, e.g. 'en'

Definition at line 400 of file __init__.py.

00400 
00401 def get_browser_language(request):
00402     """
00403     Return the language that is supported by wiki and what user browser
00404     would prefer to get. Return empty string if there is no such language
00405     or language_ignore_browser is true.
00406 
00407     @param request: the request object
00408     @rtype: string
00409     @return: ISO language code, e.g. 'en'
00410     """
00411     available = wikiLanguages()
00412     if available and not request.cfg.language_ignore_browser:
00413             for lang in browserLanguages(request):
00414                 if lang in available:
00415                     return lang
00416     return ''
00417 

Here is the call graph for this function:

Here is the caller graph for this function:

Return the text direction for a language, either 'ltr' or 'rtl'. 

Definition at line 255 of file __init__.py.

00255 
00256 def getDirection(lang):
00257     """ Return the text direction for a language, either 'ltr' or 'rtl'. """
00258     return languages[lang]['x-direction']

def MoinMoin.i18n.getText (   original,
  request,
  lang,
  kw 
)
Return a translation of some original text.

@param original: the original (english) text
@param request: the request object
@lang: the target language for the translation
@keyword wiki: True to use the wiki parser/formatter on the translation result,
               False to return the translation result "as is"
@keyword percent: True if we need special escaping because we use the translation
                  result as the left side of a % operator: e.g. % chars need to
                  become %% for that usage. This will only escape generated % chars,
                  e.g. in wiki links to non-ascii pagenames (%XX%XX%XX).
                  False, if we don't use it as a left-side of % operator.
                  Only specify this option for wiki==True, it doesn't do
                  anything for wiki==False.

Definition at line 259 of file __init__.py.

00259 
00260 def getText(original, request, lang, **kw):
00261     """ Return a translation of some original text.
00262 
00263     @param original: the original (english) text
00264     @param request: the request object
00265     @lang: the target language for the translation
00266     @keyword wiki: True to use the wiki parser/formatter on the translation result,
00267                    False to return the translation result "as is"
00268     @keyword percent: True if we need special escaping because we use the translation
00269                       result as the left side of a % operator: e.g. % chars need to
00270                       become %% for that usage. This will only escape generated % chars,
00271                       e.g. in wiki links to non-ascii pagenames (%XX%XX%XX).
00272                       False, if we don't use it as a left-side of % operator.
00273                       Only specify this option for wiki==True, it doesn't do
00274                       anything for wiki==False.
00275     """
00276     formatted = kw.get('wiki', False) # 1.6 and early 1.7 (until 2/2008) used 'formatted' with True as default!
00277     percent = kw.get('percent', False)
00278     if original == u"":
00279         return u"" # we don't want to get *.po files metadata!
00280 
00281     global translations
00282     if not lang in translations: # load translation if needed
00283         t = Translation(lang)
00284         t.loadLanguage(request)
00285         translations[lang] = t
00286 
00287     # get the matching entry in the mapping table
00288     translated = original
00289     translation = translations[lang]
00290     if original in translation.raw:
00291         translated = translation.raw[original]
00292         if formatted:
00293             key = (original, percent)
00294             if key in translation.formatted:
00295                 translated = translation.formatted[key]
00296                 if translated is None:
00297                     logging.error("formatting a %r text that is already being formatted: %r" % (lang, original))
00298                     translated = original + u'*' # get some error indication to the UI
00299             else:
00300                 translation.formatted[key] = None # we use this as "formatting in progress" indicator
00301                 translated = translation.formatMarkup(request, translated, percent)
00302                 translation.formatted[key] = translated # remember it
00303     else:
00304         try:
00305             if languages is None:
00306                 # languages not initialized yet
00307                 raise KeyError
00308             language = languages[lang]['x-language-in-english']
00309             dictpagename = "%sDict" % language.replace(' ', '')
00310             dicts = request.dicts
00311             if dictpagename in dicts:
00312                 userdict = dicts[dictpagename]
00313                 translated = userdict[original]
00314             else:
00315                 raise KeyError
00316         except KeyError:
00317             # do not simply return trans with str, but recursively call
00318             # to get english translation, maybe formatted.
00319             # if we don't find an english "translation", we just format it
00320             # on the fly (this is needed for quickhelp).
00321             if lang != 'en':
00322                 logging.debug("falling back to english, requested string not in %r translation: %r" % (lang, original))
00323                 translated = getText(original, request, 'en', wiki=formatted, percent=percent)
00324             elif formatted: # and lang == 'en'
00325                 logging.debug("formatting for %r on the fly: %r" % (lang, original))
00326                 translated = translations[lang].formatMarkup(request, original, percent)
00327     return translated
00328 

def MoinMoin.i18n.i18n_init (   request)
this is called early from request initialization and makes sure we
    have metadata (like what languages are available, direction of language)
    loaded into the global "languages".
    The very first time, this will be slow as it will load all languages,
    but next time it will be fast due to caching.

Definition at line 58 of file __init__.py.

00058 
00059 def i18n_init(request):
00060     """ this is called early from request initialization and makes sure we
00061         have metadata (like what languages are available, direction of language)
00062         loaded into the global "languages".
00063         The very first time, this will be slow as it will load all languages,
00064         but next time it will be fast due to caching.
00065     """
00066     global languages
00067     request.clock.start('i18n_init')
00068     if languages is None:
00069         logging.debug("trying to load translations from cache")
00070         # the scope of the i18n cache needs to be per-wiki, because some translations
00071         # have http links (to some help pages) and they must not point to another
00072         # wiki in the farm (confusing and maybe not even readable due to ACLs):
00073         meta_cache = caching.CacheEntry(request, 'i18n', 'meta', scope='wiki', use_pickle=True)
00074         i18n_dir = os.path.join(request.cfg.moinmoin_dir, 'i18n')
00075         if meta_cache.needsUpdate(i18n_dir):
00076             logging.debug("cache needs update")
00077             _languages = {}
00078             _system_pages = {}
00079             for pagename in strings.all_pages:
00080                 _system_pages[pagename] = ('en', pagename)
00081             for lang_file in glob.glob(po_filename(request, language='*', domain='MoinMoin')): # XXX only MoinMoin domain for now
00082                 language, domain, ext = os.path.basename(lang_file).split('.')
00083                 t = Translation(language, domain)
00084                 f = file(lang_file)
00085                 t.load_po(f)
00086                 f.close()
00087                 logging.debug("loading translation %r" % language)
00088                 encoding = 'utf-8'
00089                 _languages[language] = {}
00090                 for key, value in t.info.items():
00091                     #logging.debug("meta key %s value %r" % (key, value))
00092                     _languages[language][key] = value.decode(encoding)
00093                 for pagename in strings.all_pages:
00094                     try:
00095                         pagename_translated = t.translation._catalog[pagename]
00096                     except KeyError:
00097                         pass
00098                     else:
00099                         _system_pages[pagename_translated] = (language, pagename)
00100             logging.debug("dumping language metadata to disk cache")
00101             try:
00102                 meta_cache.update({
00103                     'languages': _languages,
00104                     'system_pages': _system_pages,
00105                 })
00106             except caching.CacheError:
00107                 pass
00108 
00109         if languages is None: # another thread maybe has done it before us
00110             try:
00111                 logging.debug("loading language metadata from disk cache")
00112                 d = meta_cache.content()
00113                 if languages is None:
00114                     globals().update(d)
00115             except caching.CacheError:
00116                 pass
00117     request.clock.stop('i18n_init')

Here is the call graph for this function:

def MoinMoin.i18n.po_filename (   request,
  language,
  domain,
  i18n_dir = 'i18n' 
)
we use MoinMoin/i18n/<language>[.<domain>].mo as filename for the PO file.

    TODO: later, when we have a farm scope plugin dir, we can also load
          language data from there.

Definition at line 50 of file __init__.py.

00050 
00051 def po_filename(request, language, domain, i18n_dir='i18n'):
00052     """ we use MoinMoin/i18n/<language>[.<domain>].mo as filename for the PO file.
00053 
00054         TODO: later, when we have a farm scope plugin dir, we can also load
00055               language data from there.
00056     """
00057     return os.path.join(request.cfg.moinmoin_dir, i18n_dir, "%s.%s.po" % (language, domain))

Here is the caller graph for this function:

def MoinMoin.i18n.requestLanguage (   request,
  try_user = True 
)
Return the user interface language for this request.

The user interface language is taken from the user preferences for
registered users, or request environment, or the default language of
the wiki, or English.

This should be called once per request, then you should get the value from
request object lang attribute.

Unclear what this means: "Until the code for get
text is fixed, we are caching the request language locally."

@param request: the request object
@param try_user: try getting language from request.user
@keyword usecache: whether to get the value form the local cache or
                   actually look for it. This will update the cache data.
@rtype: string
@return: ISO language code, e.g. 'en'

Definition at line 329 of file __init__.py.

00329 
00330 def requestLanguage(request, try_user=True):
00331     """
00332     Return the user interface language for this request.
00333 
00334     The user interface language is taken from the user preferences for
00335     registered users, or request environment, or the default language of
00336     the wiki, or English.
00337 
00338     This should be called once per request, then you should get the value from
00339     request object lang attribute.
00340 
00341     Unclear what this means: "Until the code for get
00342     text is fixed, we are caching the request language locally."
00343 
00344     @param request: the request object
00345     @param try_user: try getting language from request.user
00346     @keyword usecache: whether to get the value form the local cache or
00347                        actually look for it. This will update the cache data.
00348     @rtype: string
00349     @return: ISO language code, e.g. 'en'
00350     """
00351     # Return the user language preferences for registered users
00352     if try_user and request.user.valid and request.user.language:
00353         return request.user.language
00354 
00355     # Or try to return one of the user browser accepted languages, if it
00356     # is available on this wiki...
00357     lang = get_browser_language(request)
00358     if not lang:
00359         available = wikiLanguages() or ["en"]
00360         # Or return the wiki default language...
00361         if request.cfg.language_default in available:
00362             lang = request.cfg.language_default
00363         # If everything else fails, read the manual... or return 'en'
00364         else:
00365             lang = 'en'
00366     return lang
00367 

Here is the call graph for this function:

Return the available user languages in this wiki.
As we do everything in unicode (or utf-8) now, everything is available.

Definition at line 368 of file __init__.py.

00368 
00369 def wikiLanguages():
00370     """
00371     Return the available user languages in this wiki.
00372     As we do everything in unicode (or utf-8) now, everything is available.
00373     """
00374     return languages
00375 

Here is the caller graph for this function:


Variable Documentation

Definition at line 41 of file __init__.py.

Definition at line 31 of file __init__.py.

dictionary MoinMoin.i18n.system_pages = {}

Definition at line 46 of file __init__.py.

dictionary MoinMoin.i18n.translations = {}

Definition at line 48 of file __init__.py.