Fix i18n: handle both single and double brace placeholders from f-strings

This commit is contained in:
Russell Ballestrini 2025-12-31 19:21:15 -05:00
parent 65a6aad3ab
commit 54975667f3

15
serp.py
View file

@ -368,12 +368,17 @@ function setLang(code) {
}
'''
html = html.replace('<script>', f'<script>\nconst T={json.dumps(trans)};\n{lang_js}', 1)
# Insert language selector before </div> in nav (after About link)
html = html.replace('<a href="/about">{{about}}</a>\n </div>',
f'<a href="/about">{{{{about}}}}</a>\n {lang_selector}\n </div>')
# Replace all {{key}} placeholders (double braces for static text)
# Insert language selector in nav (handle both {{about}} and {about} from f-strings)
for pattern in ['{{about}}</a>\n </div>', '{about}</a>\n </div>',
'{{about}}</a>\n </div>', '{about}</a>\n </div>']:
if pattern in html:
html = html.replace(f'<a href="/about">{pattern}',
f'<a href="/about">{pattern.split("</a>")[0]}</a>\n {lang_selector}\n </div>')
break
# Replace all {key} and {{key}} placeholders
for key, value in trans.items():
html = html.replace('{{' + key + '}}', value)
html = html.replace('{{' + key + '}}', value) # Double braces (static templates)
html = html.replace('{' + key + '}', value) # Single braces (after f-string)
return html
app = FastAPI(title="neopig", description="Media crawler + SERP + Screenshot service")