From d594981038086187d53134e8cc83b7685c1228f5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 12 Jul 2023 20:42:01 +0530 Subject: [PATCH] Normalize recipe language codes to use underscore and capitals for country code --- src/calibre/web/feeds/recipes/collection.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/calibre/web/feeds/recipes/collection.py b/src/calibre/web/feeds/recipes/collection.py index 40a8ec84f6..f0757d1eda 100644 --- a/src/calibre/web/feeds/recipes/collection.py +++ b/src/calibre/web/feeds/recipes/collection.py @@ -43,14 +43,22 @@ def iterate_over_builtin_recipe_files(): 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): from xml.sax.saxutils import quoteattr - def attr(n, d): + + def attr(n, d, normalize=lambda x: x): ans = getattr(recipe_class, n, d) if isinstance(ans, bytes): ans = ans.decode('utf-8', 'replace') - return quoteattr(ans) + return quoteattr(normalize(ans)) default_author = _('You') if urn.startswith('custom:') else _('Unknown') ns = getattr(recipe_class, 'needs_subscription', False) @@ -63,7 +71,7 @@ def serialize_recipe(urn, recipe_class): 'id' : quoteattr(str(urn)), 'title' : attr('title', _('Unknown')), 'author' : attr('__author__', default_author), - 'language' : attr('language', 'und'), + 'language' : attr('language', 'und', normalize_language), 'needs_subscription' : quoteattr(ns), 'description' : attr('description', '') })