Skip to content

Commit 07c79a0

Browse files
author
davux
committed
Internationalization module
Provide a new module for handling internationalized message translation. This will allow to stop hard-coding English messages in the code. There are 2 main goals: - permit translation in other languages - provide a way for service administrators to customize the messages if they need to. This module is not used anywhere yet.
1 parent 2517fc5 commit 07c79a0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

bitcoim/i18n.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Internationalization module."""
2+
3+
from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
4+
from os.path import join as j
5+
from os import sep
6+
from sys import prefix
7+
8+
_paths = []
9+
for dir in [j(prefix, 'share'), j(prefix, 'local', 'share'), j(sep, 'etc')]:
10+
_paths.append(j(dir, 'bitcoim', 'messages'))
11+
12+
fallbackLangs = ['en']
13+
"""Fallback languages to try in turn, when a translation in a certain language
14+
didn't work.
15+
"""
16+
17+
_parsers = {}
18+
19+
def _(section, key, lang=None, fallback=list(fallbackLangs)):
20+
'''Translate `key` (found in [`section`]) in `lang`. The translation is
21+
looked up in the paths given by `paths`.
22+
If the lookup fails, the translation is intented again with each
23+
language given in the `fallback` list, whose default value is that of
24+
`i18n.fallbackLangs`.
25+
'''
26+
if lang is None:
27+
try:
28+
lang = fallback.pop(0)
29+
except IndexError:
30+
return key
31+
if not lang in _parsers:
32+
_parsers[lang] = SafeConfigParser()
33+
_parsers[lang].read(map(lambda p: j(p, lang), _paths))
34+
try:
35+
return _parsers[lang].get(section, key)
36+
except NoSectionError, NoOptionError:
37+
return _(section, key, None, fallback)

0 commit comments

Comments
 (0)