diff options
-rw-r--r-- | solenv/bin/desktop-translate.pl | 171 | ||||
-rw-r--r-- | solenv/bin/desktop-translate.py | 140 | ||||
-rw-r--r-- | solenv/gbuild/Gallery.mk | 8 | ||||
-rw-r--r-- | sysui/CustomTarget_share.mk | 14 |
4 files changed, 153 insertions, 180 deletions
diff --git a/solenv/bin/desktop-translate.pl b/solenv/bin/desktop-translate.pl deleted file mode 100644 index a888fc48ec71..000000000000 --- a/solenv/bin/desktop-translate.pl +++ /dev/null @@ -1,171 +0,0 @@ -: -eval 'exec perl -wS $0 ${1+"$@"}' - if 0; - -# -# This file is part of the LibreOffice project. -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# This file incorporates work covered by the following license notice: -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed -# with this work for additional information regarding copyright -# ownership. The ASF licenses this file to you under the Apache -# License, Version 2.0 (the "License"); you may not use this file -# except in compliance with the License. You may obtain a copy of -# the License at http://www.apache.org/licenses/LICENSE-2.0 . -# - -# -# Translates multiple .desktop files at once with strings from .ulf -# files; if you add new translatable .ulf files please add them to -# l10ntools/source/localize.cxx -# - -my ($prefix, $ext, $key); -my $productname = "LibreOffice"; -my $workdir = "."; -my $template_dir; - -while ($_ = $ARGV[0], /^-/) { - shift; - last if /^--$/; - if (/^-p/) { - $productname = $ARGV[0]; - shift; - } - if (/^-d/) { - $workdir = $ARGV[0]; - shift; - } - if (/^--key/) { - $key = $ARGV[0]; - shift; - } - if (/^--prefix/) { - $prefix = $ARGV[0]; - shift; - } - if (/^--ext/) { - $ext = $ARGV[0]; - shift; - } - if (/^--template-dir/) { - $template_dir = $ARGV[0]; - shift; - } -} - -if (!defined $template_dir) { - $template_dir = "$workdir/$prefix"; -} - -# hack for unity section -my $outkey = $key; -if ( $outkey eq "UnityQuickList" ) { - $outkey = "Name"; -} - -my %templates; - -# open input file -unless (open(SOURCE, $ARGV[0])) { - die "Can't open $ARGV[0] file: $!\n"; -} - -# currently read template -my $template; - -# read ulf file -while (<SOURCE>) { - my $line = $_; - - if ( "[" eq substr($line, 0, 1) ) { - $template = substr($line, 1, index($line,"]")-1); - my %entry; - # For every section in the specified ulf file there should exist - # a template file in $workdir .. - $entry{'outfile'} = "$template_dir$template.$ext"; - my %translations; - $entry{'translations'} = \%translations; - $templates{$template} = \%entry; - } else { - # split locale = "value" into 2 strings - my ($locale, $value) = split(' = ', $line); - - if ( $locale ne $line ) { - # replace en-US with en - $locale=~s/en-US/en/; - - # use just anything inside the "" - $value = substr($value, index($value, "\"") + 1, rindex($value, "\"") - 1); - - # replace resource placeholder - $value=~s/%PRODUCTNAME/$productname/g; - - $locale=~s/-/_/; - - $templates{$template}->{'translations'}->{$locale} = $value; - } - } -} - -close(SOURCE); - -my $processed = 0; -# process templates -foreach $template (keys %templates) { - my $outfile = $templates{$template}->{'outfile'}; - - # open the template file - ignore sections for which no - # templates exist - if (open(TEMPLATE, $outfile)) { - $processed++; - } elsif ($ext eq 'str') { # string files processed one by one - next; - } else { - die "Warning: No template found for item '$template' : '$outfile' : '$_': $!\n"; - } - - # open output file - unless (open(OUTFILE, "> $outfile.tmp")) { - print STDERR "Can't create output file $outfile.tmp: $!\n"; - exit -1; - } - - # emit the template to the output file - while (<TEMPLATE>) { - my $keyline = $_; - $keyline =~ s/^$key/$outkey/; - print OUTFILE $keyline; - if (/$key/) { - my $translations = $templates{$template}->{'translations'}; - foreach my $locale (sort (keys %{$translations})) { - my $value = $translations->{$locale}; - # print "locale is $locale\n"; - # print "value is $value\n"; - if ($value) { - if ($ext eq "desktop" || $ext eq "str") { - print OUTFILE "$outkey\[$locale\]=$value\n"; - } else { - print OUTFILE "\t\[$locale\]$outkey=$value\n"; - } - } - } - } - } - - close(TEMPLATE); - - if (close(OUTFILE)) { - system "mv -f $outfile.tmp $outfile\n"; - } -} - -if ($ext eq 'str' && $processed == 0) { - die "Warning: No matching templates processed"; -} diff --git a/solenv/bin/desktop-translate.py b/solenv/bin/desktop-translate.py new file mode 100644 index 000000000000..e0dbc3078082 --- /dev/null +++ b/solenv/bin/desktop-translate.py @@ -0,0 +1,140 @@ +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This file incorporates work covered by the following license notice: +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed +# with this work for additional information regarding copyright +# ownership. The ASF licenses this file to you under the Apache +# License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.apache.org/licenses/LICENSE-2.0 . +# + +# +# Translates multiple .desktop files at once with strings from .ulf +# files; if you add new translatable .ulf files please add them to +# l10ntools/source/localize.cxx +# + +import os, sys, argparse + +parser = argparse.ArgumentParser() +parser.add_argument('-p', dest='productname', default='LibreOffice') +parser.add_argument('-d', dest='workdir', default='.') +parser.add_argument('--key', dest='key') +parser.add_argument('--prefix', dest='prefix', default='') +parser.add_argument('--ext', dest='ext') +parser.add_argument('--template-dir', dest='template_dir', default=None) +parser.add_argument('ifile') + +o = parser.parse_args() + +if o.template_dir is None: + template_dir = '{}/{}'.format(o.workdir, o.prefix) +else: + template_dir = o.template_dir + +# hack for unity section +if o.key == "UnityQuickList": + outkey = "Name" +else: + outkey = o.key + + +templates = {} + +# open input file +source = open(o.ifile) + +template = None + +# read ulf file +for line in source: + if line.strip() == '': + continue + if line[0] == "[": + template = line.split(']', 1)[0][1:] + entry = {} + # For every section in the specified ulf file there should exist + # a template file in $workdir .. + entry['outfile'] = "{}{}.{}".format(template_dir, template, o.ext) + entry['translations'] = {} + templates[template] = entry + else: + # split locale = "value" into 2 strings + if ' = ' not in line: + continue + locale, value = line.split(' = ') + + if locale != line: + # replace en-US with en + locale.replace('en-US', 'en') + + # use just anything inside the "" + value = value.strip() + assert(value[0] == '"') + assert(value[-1] == '"') + value = value[1:-1] + + # replace resource placeholder + value = value.replace('%PRODUCTNAME', o.productname) + + locale = locale.replace('-', '_') + + templates[template]['translations'][locale] = value + +source.close() + +processed = 0 +# process templates +for template in templates: + outfilename = templates[template]['outfile'] + + # open the template file - ignore sections for which no + # templates exist + try: + template_file = open(outfilename) + except Exception: + # string files processed one by one + if o.ext == 'str': + continue + sys.exit("Warning: No template found for item '{}' : '{}' : '{}': $!\n".format(template, outfile, line)) + processed += 1 + + # open output file + tmpfilename = '{}.tmp'.format(outfilename) + outfile = open(tmpfilename, 'w') + + # emit the template to the output file + for line in template_file: + keyline = line + if keyline.startswith(o.key): + keyline = keyline[len(o.key):] + outkey + outfile.write(keyline) + if o.key in line: + translations = templates[template]['translations'] + for locale in sorted (translations.keys()): + value = translations.get(locale, None) + # print "locale is $locale\n"; + # print "value is $value\n"; + if value: + if o.ext == "desktop" or o.ext == "str": + outfile.write("""{}[{}]={}\n""".format(outkey, locale, value)) + else: + outfile.write("""\t[{}]{}={}\n""".format(locale, outkey, value)) + + template_file.close() + + outfile.close() + if os.path.exists(outfilename): + os.unlink(outfilename) + os.rename(tmpfilename, outfilename) + +if o.ext == 'str' and processed == 0: + sys.exit("Warning: No matching templates processed") diff --git a/solenv/gbuild/Gallery.mk b/solenv/gbuild/Gallery.mk index 0869750683b9..d4c2765ae2dd 100644 --- a/solenv/gbuild/Gallery.mk +++ b/solenv/gbuild/Gallery.mk @@ -11,7 +11,7 @@ # Handles creation of image galleries. -gb_Gallery_TRANSLATE := $(SRCDIR)/solenv/bin/desktop-translate.pl +gb_Gallery_TRANSLATE := $(SRCDIR)/solenv/bin/desktop-translate.py gb_Gallery_INSTDIR := $(LIBO_SHARE_FOLDER)/gallery define gb_Gallery__command @@ -34,7 +34,8 @@ endef define gb_Gallery__command_str cp -f $(GALLERY_STRFILE) $@ && \ -$(PERL) $(gb_Gallery_TRANSLATE) \ +$(call gb_ExternalExecutable_get_command,python) \ + $(gb_Gallery_TRANSLATE) \ --ext "str" --key "name" \ -d $(GALLERY_WORKDIR) \ $(GALLERY_ULFFILE) @@ -65,7 +66,8 @@ $(call gb_Gallery_get_workdir,%).ulf : \ $(foreach lang,$(gb_TRANS_LANGS),\ $(gb_POLOCATION)/$(lang)/extras/source/gallery/share.po)) -$(call gb_Gallery_get_workdir,%).str : $(gb_Gallery_TRANSLATE) +$(call gb_Gallery_get_workdir,%).str : $(gb_Gallery_TRANSLATE) \ + $(call gb_ExternalExecutable_get_dependencies,python) $(call gb_Output_announce,$*,$(true),STR,1) $(call gb_Trace_StartRange,$*,STR) $(call gb_Gallery__command_str,$@,$*) diff --git a/sysui/CustomTarget_share.mk b/sysui/CustomTarget_share.mk index cd06c141e096..b613c56cda3c 100644 --- a/sysui/CustomTarget_share.mk +++ b/sysui/CustomTarget_share.mk @@ -21,7 +21,7 @@ endif share_WORKDIR := $(call gb_CustomTarget_get_workdir,sysui/share) share_SRCDIR := $(SRCDIR)/sysui/desktop -share_TRANSLATE := $(SRCDIR)/solenv/bin/desktop-translate.pl +share_TRANSLATE := $(SRCDIR)/solenv/bin/desktop-translate.py LAUNCHERLIST_APPS := writer calc draw impress math base startcenter LAUNCHERLIST := $(LAUNCHERLIST_APPS) xsltfilter @@ -142,13 +142,15 @@ $(share_WORKDIR)/%/openoffice.org.xml: $(share_WORKDIR)/documents.ulf $(MIMEDESK $(share_WORKDIR)/%/openoffice.keys: \ $(share_SRCDIR)/mimetypes/openoffice.mime $(MIMEKEYS) $(share_SRCDIR)/share/brand.pl \ - $(share_TRANSLATE) $(share_WORKDIR)/documents.ulf + $(share_TRANSLATE) $(share_WORKDIR)/documents.ulf \ + $(call gb_ExternalExecutable_get_dependencies,python) mkdir -p $(dir $@) $(call gb_Output_announce,$(subst $(WORKDIR)/,,$@),$(true),PRL,1) $(call gb_Trace_StartRange,$(subst $(WORKDIR)/,,$@),PRL) $(PERL) $(share_SRCDIR)/share/brand.pl -p $* -u $(UNIXFILENAME.$*) \ --iconprefix $(UNIXFILENAME.$*)- $^ $(share_WORKDIR)/$* - $(PERL) $(share_TRANSLATE) -p $* -d $(share_WORKDIR)/$* \ + $(call gb_ExternalExecutable_get_command,python) $(share_TRANSLATE) \ + -p $* -d $(share_WORKDIR)/$* \ --ext "keys" --key "description" $(share_WORKDIR)/documents.ulf cat $(MIMEKEYS) > $@ $(call gb_Trace_EndRange,$(subst $(WORKDIR)/,,$@),PRL) @@ -242,11 +244,11 @@ $(share_WORKDIR)/%/build.flag: $(share_SRCDIR)/share/brand.pl $(LAUNCHERS) \ $(PERL) $(share_SRCDIR)/share/brand.pl -p '$${PRODUCTNAME} $${PRODUCTVERSION}' -u $(UNIXFILENAME.$*) \ $(brand_URIPARAM) \ --iconprefix '$${UNIXBASISROOTNAME}-' $^ $(share_WORKDIR)/$* - $(PERL) $(share_TRANSLATE) -p $(PRODUCTNAME.$*)$(PRODUCTVERSION) -d $(share_WORKDIR)/$* \ + $(call gb_ExternalExecutable_get_command,python) $(share_TRANSLATE) -p $(PRODUCTNAME.$*)$(PRODUCTVERSION) -d $(share_WORKDIR)/$* \ --ext "desktop" --key "Comment" $(share_WORKDIR)/launcher_comment.ulf - $(PERL) $(share_TRANSLATE) -p $(PRODUCTNAME.$*)$(PRODUCTVERSION) -d $(share_WORKDIR)/$* \ + $(call gb_ExternalExecutable_get_command,python) $(share_TRANSLATE) -p $(PRODUCTNAME.$*)$(PRODUCTVERSION) -d $(share_WORKDIR)/$* \ --ext "desktop" --key "GenericName" $(share_WORKDIR)/launcher_genericname.ulf - $(PERL) $(share_TRANSLATE) -p $(PRODUCTNAME.$*)$(PRODUCTVERSION) -d $(share_WORKDIR)/$* \ + $(call gb_ExternalExecutable_get_command,python) $(share_TRANSLATE) -p $(PRODUCTNAME.$*)$(PRODUCTVERSION) -d $(share_WORKDIR)/$* \ --ext "desktop" --key "UnityQuickList" $(share_WORKDIR)/launcher_unityquicklist.ulf touch $@ $(call gb_Trace_EndRange,$(subst $(WORKDIR)/,,$@),PRL) |