diff options
| author | Patrick Storz <eduard.braun2@gmx.de> | 2019-02-10 22:51:51 +0000 |
|---|---|---|
| committer | Patrick Storz <eduard.braun2@gmx.de> | 2019-02-16 16:56:17 +0000 |
| commit | a693c5a412381fdcab589a2cf8c2190a9e75ac9e (patch) | |
| tree | 4cacdd7782a3ad4589a8833186c3f6913bc5d483 | |
| parent | Fix preview for large file (diff) | |
| download | inkscape-a693c5a412381fdcab589a2cf8c2190a9e75ac9e.tar.gz inkscape-a693c5a412381fdcab589a2cf8c2190a9e75ac9e.zip | |
Optimize creation of localized default templates
- extract localized strings directly from .po/.mo files instead of
having them in a separate file
- drop dependency on perl
- avoid re-creating the templates on each build
| -rw-r--r-- | share/templates/CMakeLists.txt | 26 | ||||
| -rwxr-xr-x | share/templates/create_default_templates.py | 73 | ||||
| -rwxr-xr-x | share/templates/create_defaults.pl | 66 |
3 files changed, 91 insertions, 74 deletions
diff --git a/share/templates/CMakeLists.txt b/share/templates/CMakeLists.txt index aecdca6bb..563c55837 100644 --- a/share/templates/CMakeLists.txt +++ b/share/templates/CMakeLists.txt @@ -12,13 +12,23 @@ add_custom_target(templates_h ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/templates. install(FILES ${_FILES} "README" DESTINATION ${INKSCAPE_SHARE_INSTALL}/templates) -install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} DESTINATION ${INKSCAPE_SHARE_INSTALL} - FILES_MATCHING PATTERN "*.svg" PATTERN "CMakeFiles" EXCLUDE ) -add_custom_target(inkscape_default_svg ALL - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/create_defaults.pl ${CMAKE_CURRENT_SOURCE_DIR}/default.svg - #BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/default-* - COMMAND sh -c "${CMAKE_CURRENT_SOURCE_DIR}/create_defaults.pl" - COMMAND sh -c "mv default-* ${CMAKE_CURRENT_BINARY_DIR}" - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +# create localized versions of default.svg +file(GLOB POFILES ${CMAKE_SOURCE_DIR}/po/*.po) + foreach(pofile ${POFILES}) + string(REGEX REPLACE "(.+(\\\\|/))+" "${CMAKE_BINARY_DIR}/po/" pofile ${pofile}) + string(REGEX REPLACE "\\.po$" ".gmo" pofile ${pofile}) + list(APPEND GMOFILES "${pofile}") +endforeach(pofile) + +add_custom_command( + OUTPUT default_templates.timestamp + COMMAND ./create_default_templates.py ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/default.svg ${CMAKE_CURRENT_SOURCE_DIR}/create_default_templates.py ${GMOFILES} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Generating localized default templates" +) +add_custom_target(default_templates ALL DEPENDS default_templates.timestamp) +install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} DESTINATION ${INKSCAPE_SHARE_INSTALL} + FILES_MATCHING PATTERN "*.svg" PATTERN "CMakeFiles" EXCLUDE ) diff --git a/share/templates/create_default_templates.py b/share/templates/create_default_templates.py new file mode 100755 index 000000000..eb8de35ed --- /dev/null +++ b/share/templates/create_default_templates.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# +# Creates localized default templates +# (uses default.svg as base and reads localized strings directly from .po/.gmo files) +# + +from __future__ import print_function +from __future__ import unicode_literals # make all literals unicode strings by default (even in Python 2) + +import gettext +import glob +import os +import shutil +import sys +from io import open # needed for support of encoding parameter in Python 2 + + +LAYER_STRING = 'Layer' + + +if len(sys.argv) != 3: + sys.exit("Usage:\n %s ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}" % sys.argv[0]) + +source_dir = sys.argv[1] +binary_dir = sys.argv[2] + + +# get available languages (should match the already created .gmo files) +gmofiles = glob.glob(binary_dir + '/po/*.gmo') + +languages = gmofiles +languages = [language.split('/')[-1] for language in languages] # split filename from path +languages = [language.split('.')[-2] for language in languages] # split extension + + +# process each language sequentially +for language in languages: + # copy .gmo file into a location where gettext can find and use it + source = binary_dir + '/po/' + language + '.gmo' + destination_dir = binary_dir + '/po/locale/' + language + '/LC_MESSAGES/' + destination = destination_dir + 'inkscape.mo' + + if not os.path.isdir(destination_dir): + os.makedirs(destination_dir) + shutil.copy(source, destination) + + # get translation with help of gettext + translation = gettext.translation('inkscape', localedir=binary_dir + '/po/locale', languages=[language]) + translated_string = translation.gettext(LAYER_STRING) + + if type(translated_string) != type(LAYER_STRING): # python2 compat (make sure translation is a Unicode string) + translated_string = translated_string.decode('utf-8') + + # now create localized version of English template file (if we have a translation) + template_file = source_dir + '/share/templates/default.svg' + output_file = binary_dir + '/share/templates/default-' + language + '.svg' + + if os.path.isfile(output_file): + os.remove(output_file) + if translated_string != LAYER_STRING: + with open(template_file, 'r', encoding='utf-8', newline='\n') as file: + filedata = file.read() + filedata = filedata.replace('Layer', translated_string) + with open(output_file, 'w', encoding='utf-8', newline='\n') as file: + file.write(filedata) + + +# create timestamp file (indicates last sucessful creation for build system) +timestamp_file = binary_dir + '/share/templates/default_templates.timestamp' +if os.path.exists(timestamp_file): + os.utime(timestamp_file, None) +else: + open(timestamp_file, 'a').close() diff --git a/share/templates/create_defaults.pl b/share/templates/create_defaults.pl deleted file mode 100755 index 173be02f8..000000000 --- a/share/templates/create_defaults.pl +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/perl - -# Purpose: To create internationalized versions of default.svg. - -# Usage: create_defaults.pl - -use strict; -use warnings; -use utf8; - -my $count = 0; - -# Data base - -my @data = ( - ["be", "Пласт 1", "layer1"], - ["ca", "Capa 1", "capa1"], - ["cs", "Vrstva 1", "layer1"], - ["de", "Ebene 1", "layer1"], - ["eo", "Tavolo 1", "layer1"], - ["es", "Capa 1", "layer1"], - ["eu", "Capa 1", "layer1"], - ["fi", "Taso 1", "layer1"], - ["fr", "Calque 1", "layer1"], - ["hr", "Sloj 1", "layer1"], - ["hu", "1. réteg", "layer1"], - ["it", "Livello 1", "layer1"], - ["ja", "レイヤー 1", "layer1"], - ["lt", "Sluoksnis 1", "layer1"], - ["nl", "Laag 1", "layer1"], - ["pl", "Warstwa 1", "layer1"], - ["pt_BR", "Camada 1", "layer1"], - ["pt", "Camada 1", "layer1"], - ["ro", "Strat 1", "layer1"], - ["sk", "Vrstva 1", "layer1"], - ["zh_TW", "圖層 1", "layer1"] - ); - -foreach my $lang (@data ) { - - ++$count; - - my @values = @{$lang}; - print "$values[0]\n"; - - my $filename = "default-" . $values[0] . ".svg"; - open( OUTPUT, '>:encoding(UTF-8)', $filename ) or die "Cannot open $filename.\n"; - - # Open input again to return to top... - open (INPUT, '<:encoding(UTF-8)', "default.svg") or die 'Cannot open input\n'; - - while( my $line = <INPUT> ) { - - if( $line =~ /inkscape:label=/ ) { - $line =~ s/Layer 1/$values[1]/; - } - print OUTPUT $line; - - } - - close( INPUT ); - close( OUTPUT ); -} - - -print "Created $count files.\n"; |
