diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/i18n.py | 28 | ||||
| -rw-r--r-- | lib/lettering/font.py | 25 |
2 files changed, 48 insertions, 5 deletions
diff --git a/lib/i18n.py b/lib/i18n.py index 98f63ec1..f57bbf9c 100644 --- a/lib/i18n.py +++ b/lib/i18n.py @@ -1,7 +1,9 @@ -import sys +import gettext import os from os.path import dirname, realpath -import gettext +import sys + +from .utils import cache _ = translation = None locale_dir = None @@ -33,5 +35,27 @@ def localize(languages=None): _ = translation.ugettext +@cache +def get_languages(): + """return a list of languages configured by the user + + I really wish gettext provided this as a function. Instead, we've duplicated + its code below. + """ + + languages = [] + + for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): + val = os.environ.get(envar) + if val: + languages = val.split(':') + break + + if 'C' not in languages: + languages.append('C') + + return languages + + _set_locale_dir() localize() diff --git a/lib/lettering/font.py b/lib/lettering/font.py index 6f749d28..46e2648d 100644 --- a/lib/lettering/font.py +++ b/lib/lettering/font.py @@ -8,7 +8,7 @@ import inkex from ..elements import nodes_to_elements from ..exceptions import InkstitchException -from ..i18n import _ +from ..i18n import _, get_languages from ..stitches.auto_satin import auto_satin from ..svg import PIXELS_PER_MM from ..svg.tags import SVG_GROUP_TAG, SVG_PATH_TAG, INKSCAPE_LABEL @@ -32,6 +32,25 @@ def font_metadata(name, default=None, multiplier=None): return property(getter) +def localized_font_metadata(name, default=None): + def getter(self): + # If the font contains a localized version of the attribute, use it. + for language in get_languages(): + attr = "%s_%s" % (name, language) + if attr in self.metadata: + return self.metadata.get(attr) + + if name in self.metadata: + # This may be a font packaged with Ink/Stitch, in which case the + # text will have been sent to CrowdIn for community translation. + # Try to fetch the translated version. + return _(self.metadata.get(name)) + else: + return default + + return property(getter) + + class Font(object): """Represents a font with multiple variants. @@ -79,8 +98,8 @@ class Font(object): # we'll deal with missing variants when we apply lettering pass - name = font_metadata('name', '') - description = font_metadata('description', '') + name = localized_font_metadata('name', '') + description = localized_font_metadata('description', '') default_variant = font_metadata('default_variant', FontVariant.LEFT_TO_RIGHT) default_glyph = font_metadata('defalt_glyph', u"�") letter_spacing = font_metadata('letter_spacing', 1.5, multiplier=PIXELS_PER_MM) |
