##// END OF EJS Templates
scripts/i18n: introduce --merge-pot-file to control normalization...
Mads Kiilerich -
r8186:5553ecc9 default
parent child Browse files
Show More
@@ -44,27 +44,44 b' def cli(debug):'
44
44
45 @cli.command()
45 @cli.command()
46 @click.argument('po_files', nargs=-1)
46 @click.argument('po_files', nargs=-1)
47 def normalize_po_files(po_files):
47 @click.option('--merge-pot-file', default=None)
48 @click.option('--strip/--no-strip', default=False)
49 def normalize_po_files(po_files, merge_pot_file, strip):
48 """Normalize the specified .po and .pot files.
50 """Normalize the specified .po and .pot files.
49
51
50 Only actual translations and essential headers will be preserved.
52 By default, only actual translations and essential headers will be
53 preserved, just as we want it in the main branches with minimal noise.
54
55 If a .pot file is specified, the po files will instead be updated by
56 running GNU msgmerge with this .pot file, thus updating source code
57 references and preserving comments and outdated translations.
51 """
58 """
52 for po_file in po_files:
59 for po_file in po_files:
53 i18n_utils._normalize_po_file(po_file, strip=True)
60 i18n_utils._normalize_po_file(po_file, merge_pot_file=merge_pot_file, strip=strip)
54
61
55 @cli.command()
62 @cli.command()
56 @click.argument('local')
63 @click.argument('local')
57 @click.argument('base')
64 @click.argument('base')
58 @click.argument('other')
65 @click.argument('other')
59 @click.argument('output')
66 @click.argument('output')
60 def normalized_merge(local, base, other, output):
67 @click.option('--merge-pot-file', default=None)
68 @click.option('--strip/--no-strip', default=False)
69 def normalized_merge(local, base, other, output, merge_pot_file, strip):
61 """Merge tool for use with 'hg merge/rebase/graft --tool'
70 """Merge tool for use with 'hg merge/rebase/graft --tool'
62
71
63 Merging i18n files with a standard merge tool could yield merge conflicts
72 i18n files are partially manually editored original source of content, and
64 when one side is normalized and the other is not. In such cases, it may be
73 partially automatically generated and updated. That create a lot of churn
65 better to first normalize all sides, then proceed with a standard merge.
74 and often cause a lot of merge conflicts.
66 This command does exactly that, and can be used as 'merge-tool' in
75
67 Mercurial commands like merge, rebase and graft.
76 To avoid that, this merge tool wrapper will normalize .po content before
77 running the merge tool.
78
79 By default, only actual translations and essential headers will be
80 preserved, just as we want it in the main branches with minimal noise.
81
82 If a .pot file is specified, the po files will instead be updated by
83 running GNU msgmerge with this .pot file, thus updating source code
84 references and preserving comments and outdated translations.
68
85
69 Add the following to your user or repository-specific .hgrc file to use it:
86 Add the following to your user or repository-specific .hgrc file to use it:
70 [merge-tools]
87 [merge-tools]
@@ -81,10 +98,10 b' def normalized_merge(local, base, other,'
81
98
82 print('i18n normalized-merge: merging file %s' % output)
99 print('i18n normalized-merge: merging file %s' % output)
83
100
84 i18n_utils._normalize_po_file(local, strip=True)
101 i18n_utils._normalize_po_file(local, merge_pot_file=merge_pot_file, strip=strip)
85 i18n_utils._normalize_po_file(base, strip=True)
102 i18n_utils._normalize_po_file(base, merge_pot_file=merge_pot_file, strip=strip)
86 i18n_utils._normalize_po_file(other, strip=True)
103 i18n_utils._normalize_po_file(other, merge_pot_file=merge_pot_file, strip=strip)
87 i18n_utils._normalize_po_file(output, strip=True)
104 i18n_utils._normalize_po_file(output, merge_pot_file=merge_pot_file, strip=strip)
88
105
89 # simplemerge will write markers to 'local' if it fails, keep a copy without markers
106 # simplemerge will write markers to 'local' if it fails, keep a copy without markers
90 localkeep = local + '.keep'
107 localkeep = local + '.keep'
@@ -110,9 +127,11 b' def normalized_merge(local, base, other,'
110 @cli.command()
127 @cli.command()
111 @click.argument('file1')
128 @click.argument('file1')
112 @click.argument('file2')
129 @click.argument('file2')
113 def normalized_diff(file1, file2):
130 @click.option('--merge-pot-file', default=None)
131 @click.option('--strip/--no-strip', default=False)
132 def normalized_diff(file1, file2, merge_pot_file, strip):
114 """Compare two files while transparently normalizing them."""
133 """Compare two files while transparently normalizing them."""
115 sys.exit(i18n_utils._normalized_diff(file1, file2, strip=True))
134 sys.exit(i18n_utils._normalized_diff(file1, file2, merge_pot_file=merge_pot_file, strip=strip))
116
135
117 if __name__ == '__main__':
136 if __name__ == '__main__':
118 cli()
137 cli()
@@ -159,7 +159,10 b' def _normalize_po(raw_content):'
159 chunks.append('\n'.join(chunk_lines) + '\n')
159 chunks.append('\n'.join(chunk_lines) + '\n')
160 return '\n'.join(chunks)
160 return '\n'.join(chunks)
161
161
162 def _normalize_po_file(po_file, strip=False):
162 def _normalize_po_file(po_file, merge_pot_file=None, strip=False):
163 if merge_pot_file:
164 runcmd(['msgmerge', '--width=76', '--backup=none', '--previous',
165 '--update', po_file, '-q', merge_pot_file])
163 if strip:
166 if strip:
164 po_tmp = po_file + '.tmp'
167 po_tmp = po_file + '.tmp'
165 with open(po_file, 'r') as src, open(po_tmp, 'w') as dest:
168 with open(po_file, 'r') as src, open(po_tmp, 'w') as dest:
@@ -168,7 +171,7 b' def _normalize_po_file(po_file, strip=Fa'
168 dest.write(normalized_content)
171 dest.write(normalized_content)
169 os.rename(po_tmp, po_file)
172 os.rename(po_tmp, po_file)
170
173
171 def _normalized_diff(file1, file2, strip=False):
174 def _normalized_diff(file1, file2, merge_pot_file=None, strip=False):
172 # Create temporary copies of both files
175 # Create temporary copies of both files
173 temp1 = tempfile.NamedTemporaryFile(prefix=os.path.basename(file1))
176 temp1 = tempfile.NamedTemporaryFile(prefix=os.path.basename(file1))
174 temp2 = tempfile.NamedTemporaryFile(prefix=os.path.basename(file2))
177 temp2 = tempfile.NamedTemporaryFile(prefix=os.path.basename(file2))
@@ -176,8 +179,8 b' def _normalized_diff(file1, file2, strip'
176 shutil.copyfile(file1, temp1.name)
179 shutil.copyfile(file1, temp1.name)
177 shutil.copyfile(file2, temp2.name)
180 shutil.copyfile(file2, temp2.name)
178 # Normalize them in place
181 # Normalize them in place
179 _normalize_po_file(temp1.name, strip=strip)
182 _normalize_po_file(temp1.name, merge_pot_file=merge_pot_file, strip=strip)
180 _normalize_po_file(temp2.name, strip=strip)
183 _normalize_po_file(temp2.name, merge_pot_file=merge_pot_file, strip=strip)
181 # Now compare
184 # Now compare
182 try:
185 try:
183 runcmd(['diff', '-u', temp1.name, temp2.name])
186 runcmd(['diff', '-u', temp1.name, temp2.name])
General Comments 0
You need to be logged in to leave comments. Login now