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
1=== modified file 'HACKING'
2--- HACKING 2015-02-23 17:00:11 +0000
3+++ HACKING 2015-02-24 13:18:01 +0000
4@@ -7,7 +7,8 @@
5
6 Before you get started, run:
7
8- sudo apt install python-pelican po4a make bzr ubuntu-html5-ui-toolkit
9+ sudo apt install python-pelican po4a make bzr \
10+ ubuntu-html5-ui-toolkit python3-polib
11
12
13 Editing
14
15=== modified file 'edit-here/generate-translations'
16--- edit-here/generate-translations 2015-02-13 14:31:20 +0000
17+++ edit-here/generate-translations 2015-02-24 13:18:01 +0000
18@@ -9,6 +9,7 @@
19
20 def main():
21 translations = Translations()
22+ translations.rewrite_links()
23 translations.generate_translations()
24
25
26
27=== modified file 'edit-here/po/de.po'
28--- edit-here/po/de.po 2015-02-24 09:19:32 +0000
29+++ edit-here/po/de.po 2015-02-24 13:18:01 +0000
30@@ -2,7 +2,7 @@
31 # Copyright (C) YEAR Free Software Foundation, Inc.
32 # This file is distributed under the same license as the PACKAGE package.
33 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
34-#
35+#
36 #, fuzzy
37 msgid ""
38 msgstr ""
39@@ -33,7 +33,7 @@
40 #. type: Bullet: ' * '
41 #: content/pages/faq.md:7
42 msgid "[Apps]({filename}apps.md)"
43-msgstr "[Apps]({filename}apps.md)"
44+msgstr "[Apps]({filename}lang-de-apps.md)"
45
46 #. type: Bullet: ' * '
47 #: content/pages/faq.md:7
48@@ -68,7 +68,8 @@
49 #: content/pages/index.md:9
50 msgid "links to get in touch with experts and other community members"
51 msgstr ""
52-"Links, um mit anderen Community-Mitgliedern und Experten in Kontakt zu kommen"
53+"Links, um mit anderen Community-Mitgliedern und Experten in Kontakt zu "
54+"kommen"
55
56 #. type: Plain text
57 #: content/pages/index.md:11
58@@ -80,14 +81,14 @@
59 #, fuzzy, no-wrap
60 #| msgid "[Take me to the FAQ!](faq.html) \n"
61 msgid "[Take me to the FAQ!]({filename}faq.md) \n"
62-msgstr "[Zu den Antworten!]({filename}faq.md) \n"
63+msgstr "[Zu den Antworten!]({filename}lang-de-faq.md) \n"
64
65 #. type: Plain text
66 #: content/pages/index.md:14
67 #, fuzzy
68 #| msgid "[Get in touch](get-in-touch.html)"
69 msgid "[Get in touch]({filename}get-in-touch.md)"
70-msgstr "[Kontakt]({filename}get-in-touch.md)"
71+msgstr "[Kontakt]({filename}lang-de-get-in-touch.md)"
72
73 #. type: Plain text
74 #: content/pages/apps.md:2
75
76=== modified file 'edit-here/translations.py'
77--- edit-here/translations.py 2015-02-24 13:18:00 +0000
78+++ edit-here/translations.py 2015-02-24 13:18:01 +0000
79@@ -2,6 +2,8 @@
80 import copy
81 import glob
82 import os
83+import polib
84+import re
85 import shutil
86 import subprocess
87 import tempfile
88@@ -59,7 +61,7 @@
89 args = []
90 for document in documents:
91 args += ['-m', document]
92- args += ['-p', os.path.join(self.translations_dir, lang+'.po')]
93+ args += ['-p', lang.file_name]
94 ret = self.run('po4a-updatepo', args)
95 if ret:
96 return False
97@@ -69,20 +71,42 @@
98 args = [
99 '-k', TRANSLATION_COMPLETION_PERCENTAGE,
100 '-m', doc,
101- '-p', 'po/%s.po' % lang,
102+ '-p', lang.file_name,
103 '-L', 'utf-8',
104 ]
105 return self.run('po4a-translate', args, with_output=True,
106 working_dir=os.path.join(PATH, '..'))
107
108
109+class Language(object):
110+ def __init__(self, po_file):
111+ self.file_name = po_file
112+ self.gettext_lang_code = os.path.basename(po_file).split('.po')[0]
113+
114+ def rewrite_links(self):
115+ po_file = polib.pofile(self.file_name)
116+ for entry_group in [po_file.untranslated_entries(),
117+ po_file.translated_entries(),
118+ po_file.fuzzy_entries()]:
119+ for entry in entry_group:
120+ if '{filename}' in entry.msgid:
121+ if not entry.msgstr:
122+ entry.msgstr = entry.msgid
123+ link = re.findall(r'\[.+?\]\(\{filename\}(.+?)\)',
124+ entry.msgid)[0]
125+ entry.msgstr = entry.msgstr.replace(
126+ link,
127+ 'lang-%s-%s' % (self.gettext_lang_code, link))
128+ po_file.save(self.file_name)
129+
130+
131 class Translations(object):
132 def __init__(self):
133 self._cleanup()
134 self.translations_dir = os.path.abspath(os.path.join(PATH, '../po'))
135- self.available_languages = \
136- [os.path.basename(a).split('.po')[0] for a
137- in glob.glob(self.translations_dir+'/*.po')]
138+ self.available_languages = []
139+ for po_filename in glob.glob(self.translations_dir+'/*.po'):
140+ self.available_languages += [Language(po_filename)]
141 self.documents = self._find_documents()
142 self.temp_dir = tempfile.mkdtemp()
143 self.po4a = PO4A(self.translations_dir, self.temp_dir)
144@@ -163,7 +187,11 @@
145 title_line = title_line.split(metadata_tag.capitalize())[0]
146 title_line = title_line.split(metadata_tag.upper())[0]
147 return "%s\nLang: %s\nDate:\n\n" % \
148- (title_line, lang)
149+ (title_line, lang.gettext_lang_code)
150+
151+ def rewrite_links(self):
152+ for lang in self.available_languages:
153+ lang.rewrite_links()
154
155 def generate_translations(self):
156 for lang in self.available_languages:
157@@ -172,7 +200,8 @@
158 self._call_po4a_translate(doc, lang)
159 new_path = os.path.join(PATH, 'pages',
160 'lang-%s-%s' %
161- (lang, os.path.basename(doc)))
162+ (lang.gettext_lang_code,
163+ os.path.basename(doc)))
164 text = self._new_header(lang, broken_title_line)
165 text += output
166 if os.path.exists(new_path):

Subscribers

People subscribed via source and target branches