Skip to content

Commit 9ff61c5

Browse files
committed
fixing the setup-pages-for-supported-languages script
* lang detection now uses the actual codes from config.yaml so pt-br and zh-hans get recognized properly instead of trying to re-localize them on every run * removed the menu/aliases regex stripping since nothing in content uses those keys anymore, which also kills a latent bug where _index.md copies were getting stripped wrongly * dropped the unused imports and the rootfiles dict that was never read
1 parent c054c4e commit 9ff61c5

1 file changed

Lines changed: 28 additions & 35 deletions

File tree

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,39 @@
11
#!/usr/bin/env python3
22
# 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.
34
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+
"""
512
import os
6-
import re
7-
import shutil
8-
import sys
13+
914
import yaml
10-
from yaml.loader import SafeLoader
1115

1216
with open('config.yaml') as fp:
13-
#config = yaml.load(fp)
14-
config = yaml.load(fp, Loader=SafeLoader)
17+
config = yaml.safe_load(fp)
1518

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)
1921

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'):
2323
for f in files:
2424
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

Comments
 (0)