summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2019-04-02 23:07:38 -0400
committerLex Neva <github.com@lexneva.name>2019-04-02 23:07:38 -0400
commita6a86973dd54d623394fdbfc61e6e4c0ca263cc0 (patch)
tree2d9c48e16e3d6ca82729a758815d8205d658d1da
parenta9cf553066c3fd5b907593751bb00f77f32ce86a (diff)
add localization for font names and descriptions
-rw-r--r--Makefile1
-rwxr-xr-xbin/inkstitch-fonts-gettext19
-rw-r--r--lib/i18n.py28
-rw-r--r--lib/lettering/font.py25
4 files changed, 68 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index ed86a36c..5635547e 100644
--- a/Makefile
+++ b/Makefile
@@ -37,6 +37,7 @@ inx: locales
messages.po:
rm -f messages.po
bin/pyembroidery-gettext > pyembroidery-format-descriptions.py
+ bin/inkstitch-fonts-gettext > inkstitch-fonts-metadata.py
pybabel extract -o messages.po -F babel.conf --add-location=full --add-comments=l10n,L10n,L10N --sort-by-file --strip-comments -k N_ .
rm pyembroidery-format-descriptions.py
diff --git a/bin/inkstitch-fonts-gettext b/bin/inkstitch-fonts-gettext
new file mode 100755
index 00000000..8a3aa728
--- /dev/null
+++ b/bin/inkstitch-fonts-gettext
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+
+import os
+import json
+
+# generate fake python code containing the names and descriptions of all built-
+# in fonts as gettext calls so that pybabel will extract them into messages.po
+
+fonts_dir = os.path.join(os.path.dirname(__file__), "..", "fonts")
+
+for font in os.listdir(fonts_dir):
+ with open(os.path.join(fonts_dir, font, "font.json")) as font_json:
+ font_metadata = json.load(font_json)
+
+ print "# L10N name of font in fonts/%s" % font
+ print "_(%s)" % repr(font_metadata.get("name", ""))
+
+ print "# L10N description of font in fonts/%s" % font
+ print "_(%s)" % repr(font_metadata.get("description", "")) \ No newline at end of file
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)