Normalize recipe language codes to use underscore and capitals for country code

This commit is contained in:
Kovid Goyal 2023-07-12 20:42:01 +05:30
parent ca6709e99d
commit d594981038
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -43,14 +43,22 @@ def iterate_over_builtin_recipe_files():
yield rid, f yield rid, f
def normalize_language(x: str) -> str:
lang, sep, country = x.replace('-', '_').partition('_')
if sep == '_':
x = f'{lang}{sep}{country.upper()}'
return x
def serialize_recipe(urn, recipe_class): def serialize_recipe(urn, recipe_class):
from xml.sax.saxutils import quoteattr from xml.sax.saxutils import quoteattr
def attr(n, d):
def attr(n, d, normalize=lambda x: x):
ans = getattr(recipe_class, n, d) ans = getattr(recipe_class, n, d)
if isinstance(ans, bytes): if isinstance(ans, bytes):
ans = ans.decode('utf-8', 'replace') ans = ans.decode('utf-8', 'replace')
return quoteattr(ans) return quoteattr(normalize(ans))
default_author = _('You') if urn.startswith('custom:') else _('Unknown') default_author = _('You') if urn.startswith('custom:') else _('Unknown')
ns = getattr(recipe_class, 'needs_subscription', False) ns = getattr(recipe_class, 'needs_subscription', False)
@ -63,7 +71,7 @@ def serialize_recipe(urn, recipe_class):
'id' : quoteattr(str(urn)), 'id' : quoteattr(str(urn)),
'title' : attr('title', _('Unknown')), 'title' : attr('title', _('Unknown')),
'author' : attr('__author__', default_author), 'author' : attr('__author__', default_author),
'language' : attr('language', 'und'), 'language' : attr('language', 'und', normalize_language),
'needs_subscription' : quoteattr(ns), 'needs_subscription' : quoteattr(ns),
'description' : attr('description', '') 'description' : attr('description', '')
}) })