|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # original source from https://github.com/guardianproject/info/blob/master/setup-pages-for-supported-languages.py |
| 3 | +"""Generate per-language placeholder copies of every content/*.md file. |
3 | 4 |
|
4 | | -import glob |
| 5 | +For each Hugo content page that doesn't yet have a localized sibling, copy |
| 6 | +the source file to content/<page>.<lang>.md for every language defined in |
| 7 | +config.yaml. This ensures Hugo emits localized URLs for every configured |
| 8 | +language, even when Weblate hasn't supplied a translation yet. |
| 9 | +
|
| 10 | +Run from the repo root. Used by the publish workflows; safe to run locally. |
| 11 | +""" |
5 | 12 | import os |
6 | | -import re |
7 | | -import shutil |
8 | | -import sys |
| 13 | + |
9 | 14 | import yaml |
10 | | -from yaml.loader import SafeLoader |
11 | 15 |
|
12 | 16 | with open('config.yaml') as fp: |
13 | | - #config = yaml.load(fp) |
14 | | - config = yaml.load(fp, Loader=SafeLoader) |
| 17 | + config = yaml.safe_load(fp) |
15 | 18 |
|
16 | | -rootfiles = dict() |
17 | | -languages = config['languages'].keys() |
18 | | -localized_pat = re.compile(r'.+\.([a-z][a-z](|-[A-Z][A-Z]|-Han[st]))$') |
| 19 | +# Sort longest-first so e.g. "pt-br" wins over "pt" when checking suffixes. |
| 20 | +languages = sorted(config['languages'].keys(), key=len, reverse=True) |
19 | 21 |
|
20 | | -for root, dirs, files in os.walk('content'): |
21 | | - #if root.startswith('content/blog'): |
22 | | - # continue # skip since it is not translated |
| 22 | +for root, _dirs, files in os.walk('content'): |
23 | 23 | for f in files: |
24 | 24 | fileroot, extension = os.path.splitext(f) |
25 | | - if extension == '.md': |
26 | | - m = localized_pat.match(fileroot) |
27 | | - if m: |
28 | | - lang = m.group(1) |
29 | | - if lang in languages: |
30 | | - #print(f, '\t', m.group(1)) |
31 | | - filename = os.path.join(root, f.replace('.' + lang + '.md', '.md')) |
32 | | - if not filename in rootfiles: |
33 | | - rootfiles[filename] = [] |
34 | | - rootfiles[filename].append(lang) |
35 | | - continue |
36 | | - for lang in languages: |
37 | | - lang_file = os.path.join(root, fileroot + '.' + lang + extension) |
38 | | - if not os.path.exists(lang_file): |
39 | | - with open(os.path.join(root, f)) as fp: |
40 | | - contents = fp.read() |
41 | | - # for some reason, index.*.md copies need the menu entries |
42 | | - if f != 'index.md': |
43 | | - contents = re.sub(r'\naliases:.*?\n([^ ])', r'\n\1', contents, flags=re.DOTALL) |
44 | | - contents = re.sub(r'\nmenu:.*?\n([^ ])', r'\n\1', contents, flags=re.DOTALL) |
45 | | - with open(lang_file, 'w') as fp: |
46 | | - fp.write(contents) |
| 25 | + if extension != '.md': |
| 26 | + continue |
| 27 | + |
| 28 | + # Skip files that are already localized variants (e.g. foo.de.md). |
| 29 | + if any(fileroot.endswith('.' + lang) for lang in languages): |
| 30 | + continue |
| 31 | + |
| 32 | + for lang in languages: |
| 33 | + lang_file = os.path.join(root, fileroot + '.' + lang + extension) |
| 34 | + if os.path.exists(lang_file): |
| 35 | + continue |
| 36 | + with open(os.path.join(root, f)) as src: |
| 37 | + contents = src.read() |
| 38 | + with open(lang_file, 'w') as dst: |
| 39 | + dst.write(contents) |
0 commit comments