Merge lp:~dholbach/help-app/rewrite-links into lp:~ubuntu-touch-coreapps-drivers/help-app/trunk

Proposed by Daniel Holbach
Status: Merged
Merged at revision: 53
Proposed branch: lp:~dholbach/help-app/rewrite-links
Merge into: lp:~ubuntu-touch-coreapps-drivers/help-app/trunk
Prerequisite: lp:~dholbach/help-app/break-out-po4a-operations
Diff against target: 166 lines (+45/-13)
4 files modified
HACKING (+2/-1)
edit-here/generate-translations (+1/-0)
edit-here/po/de.po (+6/-5)
edit-here/translations.py (+36/-7)
To merge this branch: bzr merge lp:~dholbach/help-app/rewrite-links
Reviewer Review Type Date Requested Status
David Planella Approve
Review via email: mp+250743@code.launchpad.net

Commit message

This piece of code will make sure we end up with translations like this:

msgid "[Take me to the FAQ!]({filename}faq.md) \n"
msgstr "[Zu den Antworten!]({filename}lang-de-faq.md) \n"

It'll ensure that the link in English (faq.md) will become lang-XX-faq.md in the translated version.

To post a comment you must log in.
Revision history for this message
Daniel Holbach (dholbach) wrote :

.... on second thoughts, we might not need this. We could merge this though and use it until bug 1425010 is fixed. The code could still be useful in checking links at some stage (rogue translations breaking links).

Revision history for this message
Daniel Holbach (dholbach) wrote :

It should be useful though, if we use the following naming scheme:

 - index.en-us.html
 - index.de.html
 - get-in-touch.en-us.html (*)
 - get-in-touch.de.html

Revision history for this message
David Planella (dpm) wrote :

Good call on using polib! Looks good to me, I'd suggest going for the .$LANG.html change straight away, but if you think it makes sense to get this merged already first, sounds good to me too.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'HACKING'
--- HACKING 2015-02-23 17:00:11 +0000
+++ HACKING 2015-02-24 13:18:01 +0000
@@ -7,7 +7,8 @@
77
8Before you get started, run:8Before you get started, run:
99
10 sudo apt install python-pelican po4a make bzr ubuntu-html5-ui-toolkit10 sudo apt install python-pelican po4a make bzr \
11 ubuntu-html5-ui-toolkit python3-polib
1112
1213
13Editing14Editing
1415
=== modified file 'edit-here/generate-translations'
--- edit-here/generate-translations 2015-02-13 14:31:20 +0000
+++ edit-here/generate-translations 2015-02-24 13:18:01 +0000
@@ -9,6 +9,7 @@
99
10def main():10def main():
11 translations = Translations()11 translations = Translations()
12 translations.rewrite_links()
12 translations.generate_translations()13 translations.generate_translations()
1314
1415
1516
=== modified file 'edit-here/po/de.po'
--- edit-here/po/de.po 2015-02-24 09:19:32 +0000
+++ edit-here/po/de.po 2015-02-24 13:18:01 +0000
@@ -2,7 +2,7 @@
2# Copyright (C) YEAR Free Software Foundation, Inc.2# Copyright (C) YEAR Free Software Foundation, Inc.
3# This file is distributed under the same license as the PACKAGE package.3# This file is distributed under the same license as the PACKAGE package.
4# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.4# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5#5#
6#, fuzzy6#, fuzzy
7msgid ""7msgid ""
8msgstr ""8msgstr ""
@@ -33,7 +33,7 @@
33#. type: Bullet: ' * '33#. type: Bullet: ' * '
34#: content/pages/faq.md:734#: content/pages/faq.md:7
35msgid "[Apps]({filename}apps.md)"35msgid "[Apps]({filename}apps.md)"
36msgstr "[Apps]({filename}apps.md)"36msgstr "[Apps]({filename}lang-de-apps.md)"
3737
38#. type: Bullet: ' * '38#. type: Bullet: ' * '
39#: content/pages/faq.md:739#: content/pages/faq.md:7
@@ -68,7 +68,8 @@
68#: content/pages/index.md:968#: content/pages/index.md:9
69msgid "links to get in touch with experts and other community members"69msgid "links to get in touch with experts and other community members"
70msgstr ""70msgstr ""
71"Links, um mit anderen Community-Mitgliedern und Experten in Kontakt zu kommen"71"Links, um mit anderen Community-Mitgliedern und Experten in Kontakt zu "
72"kommen"
7273
73#. type: Plain text74#. type: Plain text
74#: content/pages/index.md:1175#: content/pages/index.md:11
@@ -80,14 +81,14 @@
80#, fuzzy, no-wrap81#, fuzzy, no-wrap
81#| msgid "[Take me to the FAQ!](faq.html) \n"82#| msgid "[Take me to the FAQ!](faq.html) \n"
82msgid "[Take me to the FAQ!]({filename}faq.md) \n"83msgid "[Take me to the FAQ!]({filename}faq.md) \n"
83msgstr "[Zu den Antworten!]({filename}faq.md) \n"84msgstr "[Zu den Antworten!]({filename}lang-de-faq.md) \n"
8485
85#. type: Plain text86#. type: Plain text
86#: content/pages/index.md:1487#: content/pages/index.md:14
87#, fuzzy88#, fuzzy
88#| msgid "[Get in touch](get-in-touch.html)"89#| msgid "[Get in touch](get-in-touch.html)"
89msgid "[Get in touch]({filename}get-in-touch.md)"90msgid "[Get in touch]({filename}get-in-touch.md)"
90msgstr "[Kontakt]({filename}get-in-touch.md)"91msgstr "[Kontakt]({filename}lang-de-get-in-touch.md)"
9192
92#. type: Plain text93#. type: Plain text
93#: content/pages/apps.md:294#: content/pages/apps.md:2
9495
=== modified file 'edit-here/translations.py'
--- edit-here/translations.py 2015-02-24 13:18:00 +0000
+++ edit-here/translations.py 2015-02-24 13:18:01 +0000
@@ -2,6 +2,8 @@
2import copy2import copy
3import glob3import glob
4import os4import os
5import polib
6import re
5import shutil7import shutil
6import subprocess8import subprocess
7import tempfile9import tempfile
@@ -59,7 +61,7 @@
59 args = []61 args = []
60 for document in documents:62 for document in documents:
61 args += ['-m', document]63 args += ['-m', document]
62 args += ['-p', os.path.join(self.translations_dir, lang+'.po')]64 args += ['-p', lang.file_name]
63 ret = self.run('po4a-updatepo', args)65 ret = self.run('po4a-updatepo', args)
64 if ret:66 if ret:
65 return False67 return False
@@ -69,20 +71,42 @@
69 args = [71 args = [
70 '-k', TRANSLATION_COMPLETION_PERCENTAGE,72 '-k', TRANSLATION_COMPLETION_PERCENTAGE,
71 '-m', doc,73 '-m', doc,
72 '-p', 'po/%s.po' % lang,74 '-p', lang.file_name,
73 '-L', 'utf-8',75 '-L', 'utf-8',
74 ]76 ]
75 return self.run('po4a-translate', args, with_output=True,77 return self.run('po4a-translate', args, with_output=True,
76 working_dir=os.path.join(PATH, '..'))78 working_dir=os.path.join(PATH, '..'))
7779
7880
81class Language(object):
82 def __init__(self, po_file):
83 self.file_name = po_file
84 self.gettext_lang_code = os.path.basename(po_file).split('.po')[0]
85
86 def rewrite_links(self):
87 po_file = polib.pofile(self.file_name)
88 for entry_group in [po_file.untranslated_entries(),
89 po_file.translated_entries(),
90 po_file.fuzzy_entries()]:
91 for entry in entry_group:
92 if '{filename}' in entry.msgid:
93 if not entry.msgstr:
94 entry.msgstr = entry.msgid
95 link = re.findall(r'\[.+?\]\(\{filename\}(.+?)\)',
96 entry.msgid)[0]
97 entry.msgstr = entry.msgstr.replace(
98 link,
99 'lang-%s-%s' % (self.gettext_lang_code, link))
100 po_file.save(self.file_name)
101
102
79class Translations(object):103class Translations(object):
80 def __init__(self):104 def __init__(self):
81 self._cleanup()105 self._cleanup()
82 self.translations_dir = os.path.abspath(os.path.join(PATH, '../po'))106 self.translations_dir = os.path.abspath(os.path.join(PATH, '../po'))
83 self.available_languages = \107 self.available_languages = []
84 [os.path.basename(a).split('.po')[0] for a108 for po_filename in glob.glob(self.translations_dir+'/*.po'):
85 in glob.glob(self.translations_dir+'/*.po')]109 self.available_languages += [Language(po_filename)]
86 self.documents = self._find_documents()110 self.documents = self._find_documents()
87 self.temp_dir = tempfile.mkdtemp()111 self.temp_dir = tempfile.mkdtemp()
88 self.po4a = PO4A(self.translations_dir, self.temp_dir)112 self.po4a = PO4A(self.translations_dir, self.temp_dir)
@@ -163,7 +187,11 @@
163 title_line = title_line.split(metadata_tag.capitalize())[0]187 title_line = title_line.split(metadata_tag.capitalize())[0]
164 title_line = title_line.split(metadata_tag.upper())[0]188 title_line = title_line.split(metadata_tag.upper())[0]
165 return "%s\nLang: %s\nDate:\n\n" % \189 return "%s\nLang: %s\nDate:\n\n" % \
166 (title_line, lang)190 (title_line, lang.gettext_lang_code)
191
192 def rewrite_links(self):
193 for lang in self.available_languages:
194 lang.rewrite_links()
167195
168 def generate_translations(self):196 def generate_translations(self):
169 for lang in self.available_languages:197 for lang in self.available_languages:
@@ -172,7 +200,8 @@
172 self._call_po4a_translate(doc, lang)200 self._call_po4a_translate(doc, lang)
173 new_path = os.path.join(PATH, 'pages',201 new_path = os.path.join(PATH, 'pages',
174 'lang-%s-%s' %202 'lang-%s-%s' %
175 (lang, os.path.basename(doc)))203 (lang.gettext_lang_code,
204 os.path.basename(doc)))
176 text = self._new_header(lang, broken_title_line)205 text = self._new_header(lang, broken_title_line)
177 text += output206 text += output
178 if os.path.exists(new_path):207 if os.path.exists(new_path):

Subscribers

People subscribed via source and target branches