From a2cad1f522074b84895897632ac69dcb5deda340 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Mon, 20 Aug 2018 20:42:02 -0400 Subject: [PATCH] generate INX files for all languages --- Makefile | 2 +- babel.conf | 2 +- lib/i18n.py | 10 +++- lib/inx/generate.py | 10 +++- lib/inx/utils.py | 29 ++++++++- messages.po | 106 ++++++++++++++++++++++++++++++++- templates/convert_to_satin.inx | 6 +- templates/embroider.inx | 14 +++-- templates/flip.inx | 8 ++- templates/input.inx | 4 +- templates/install.inx | 8 ++- templates/layer_commands.inx | 14 +++-- templates/object_commands.inx | 14 +++-- templates/output.inx | 4 +- templates/params.inx | 8 ++- templates/print.inx | 8 ++- templates/simulate.inx | 8 ++- templates/zip.inx | 6 +- 18 files changed, 207 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index a7fd0e83f..476a920d6 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ distclean: rm -rf build dist *.spec *.tar.gz .PHONY: inx -inx: +inx: locales mkdir -p inx bin/generate-inx-files diff --git a/babel.conf b/babel.conf index 712fc342c..770e8b7c0 100644 --- a/babel.conf +++ b/babel.conf @@ -3,4 +3,4 @@ [python: **.py] [jinja2: print/templates/*.html] - +[jinja2: templates/*] diff --git a/lib/i18n.py b/lib/i18n.py index 06e0dd498..419c03dc0 100644 --- a/lib/i18n.py +++ b/lib/i18n.py @@ -1,26 +1,32 @@ import sys import os +from os.path import dirname, realpath import gettext _ = translation = None +locale_dir = None # Use N_ to mark a string for translation but _not_ immediately translate it. # reference: https://docs.python.org/3/library/gettext.html#deferred-translations # Makefile configures pybabel to treat N_() the same as _() def N_(message): return message -def localize(): +def _set_locale_dir(): + global locale_dir + if getattr(sys, 'frozen', False): # we are in a pyinstaller installation locale_dir = sys._MEIPASS else: - locale_dir = os.path.dirname(__file__) + locale_dir = dirname(dirname(realpath(__file__))) locale_dir = os.path.join(locale_dir, 'locales') +def localize(languages=None): global translation, _ translation = gettext.translation("inkstitch", locale_dir, fallback=True) _ = translation.gettext +_set_locale_dir() localize() diff --git a/lib/inx/generate.py b/lib/inx/generate.py index 4773a92dc..f9ed799bc 100644 --- a/lib/inx/generate.py +++ b/lib/inx/generate.py @@ -1,8 +1,12 @@ +import os + from .inputs import generate_input_inx_files from .outputs import generate_output_inx_files from .extensions import generate_extension_inx_files +from .utils import iterate_inx_locales, inx_path def generate_inx_files(): - generate_input_inx_files() - generate_output_inx_files() - generate_extension_inx_files() + for locale in iterate_inx_locales(): + generate_input_inx_files() + generate_output_inx_files() + generate_extension_inx_files() diff --git a/lib/inx/utils.py b/lib/inx/utils.py index da0a46148..6103f360c 100644 --- a/lib/inx/utils.py +++ b/lib/inx/utils.py @@ -1,14 +1,18 @@ import os +import gettext from os.path import dirname from jinja2 import Environment, FileSystemLoader -from ..i18n import translation as inkstitch_translation +from ..i18n import translation as default_translation, locale_dir, _, N_ _top_path = dirname(dirname(dirname(os.path.realpath(__file__)))) inx_path = os.path.join(_top_path, "inx") template_path = os.path.join(_top_path, "templates") +current_translation = default_translation +current_locale = "en_US" + def build_environment(): env = Environment( loader = FileSystemLoader(template_path), @@ -16,11 +20,30 @@ def build_environment(): extensions=['jinja2.ext.i18n'] ) - env.install_gettext_translations(inkstitch_translation) + env.install_gettext_translations(current_translation) + env.globals["locale"] = current_locale return env def write_inx_file(name, contents): - inx_file_name = "inkstitch_%s.inx" % name + inx_file_name = "inkstitch_%s_%s.inx" % (name, current_locale) with open(os.path.join(inx_path, inx_file_name), 'w') as inx_file: print >> inx_file, contents + +def iterate_inx_locales(): + global current_translation, current_locale + + locales = sorted(os.listdir(locale_dir)) + for locale in locales: + translation = gettext.translation("inkstitch", locale_dir, languages=[locale], fallback=True) + + # L10N If you translate this string, that will tell Ink/Stitch to + # generate menu items for this language in Inkscape's "Extensions" + # menu. + magic_string = N_("Generate INX files") + translated_magic_string = translation.gettext(magic_string) + + if translated_magic_string != magic_string or locale == "en_US": + current_translation = translation + current_locale = locale + yield locale diff --git a/messages.po b/messages.po index 0dcd185a2..074159c4d 100644 --- a/messages.po +++ b/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2018-08-20 15:49-0400\n" +"POT-Creation-Date: 2018-08-20 20:42-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -608,6 +608,13 @@ msgstr "" msgid "No embroidery file formats selected." msgstr "" +#. If you translate this string, that will tell Ink/Stitch to +#. generate menu items for this language in Inkscape's "Extensions" +#. menu. +#: lib/inx/utils.py:43 +msgid "Generate INX files" +msgstr "" + #: lib/simulator.py:40 msgid "Speed up" msgstr "" @@ -844,7 +851,7 @@ msgstr "" msgid "Ink/Stitch Print Preview" msgstr "" -#: print/templates/ui.html:4 +#: print/templates/ui.html:4 templates/print.inx:3 msgid "Print" msgstr "" @@ -1102,3 +1109,98 @@ msgstr "" msgid "Brother Stitch Format" msgstr "" +#: templates/convert_to_satin.inx:3 +msgid "Convert Line to Satin" +msgstr "" + +#: templates/convert_to_satin.inx:12 templates/embroider.inx:23 +#: templates/flip.inx:12 templates/install.inx:12 +#: templates/layer_commands.inx:16 templates/object_commands.inx:14 +#: templates/params.inx:12 templates/print.inx:12 templates/simulate.inx:12 +msgid "English" +msgstr "" + +#: templates/embroider.inx:3 +msgid "Embroider" +msgstr "" + +#: templates/embroider.inx:7 +msgid "Collapse length (mm)" +msgstr "" + +#: templates/embroider.inx:7 +msgid "Jump stitches smaller than this will be treated as normal stitches." +msgstr "" + +#: templates/embroider.inx:8 +msgid "Hide other layers" +msgstr "" + +#: templates/embroider.inx:8 +msgid "" +"Hide all other top-level layers when the embroidery layer is generated, " +"in order to make stitching discernible." +msgstr "" + +#: templates/embroider.inx:9 +msgid "Output file format" +msgstr "" + +#: templates/embroider.inx:14 +msgid "DEBUG" +msgstr "" + +#: templates/embroider.inx:17 +msgid "Directory" +msgstr "" + +#: templates/embroider.inx:17 +msgid "Leave empty to save the output in Inkscape's extension directory." +msgstr "" + +#: templates/flip.inx:3 +msgid "Flip Satin Columns" +msgstr "" + +#: templates/input.inx:11 +#, python-format +msgid "convert %(file_extension)s file to Ink/Stitch manual-stitch paths" +msgstr "" + +#: templates/install.inx:3 +msgid "Install add-ons for Inkscape" +msgstr "" + +#: templates/layer_commands.inx:3 +msgid "Add Layer Commands" +msgstr "" + +#: templates/layer_commands.inx:7 +msgid "Commands will be added to the currently-selected layer." +msgstr "" + +#: templates/object_commands.inx:3 +msgid "Attach Commands" +msgstr "" + +#: templates/output.inx:11 +#, python-format +msgid "Save design in %(file_extension)s format using Ink/Stitch" +msgstr "" + +#: templates/params.inx:3 +msgid "Params" +msgstr "" + +#: templates/simulate.inx:3 +msgid "Simulate" +msgstr "" + +#: templates/zip.inx:10 +msgid "Ink/Stitch: ZIP export multiple formats (.zip)" +msgstr "" + +#: templates/zip.inx:11 +msgid "Create a ZIP with multiple embroidery file formats using Ink/Stitch" +msgstr "" + diff --git a/templates/convert_to_satin.inx b/templates/convert_to_satin.inx index 446e4b5c5..50886d4a3 100644 --- a/templates/convert_to_satin.inx +++ b/templates/convert_to_satin.inx @@ -1,14 +1,16 @@ {% trans %}Convert Line to Satin{% endtrans %} - org.inkstitch.convert_to_satin + org.inkstitch.convert_to_satin.{{ locale }} inkstitch.py inkex.py convert_to_satin all - + + +