diff options
-rw-r--r-- | setup.py | 1 | ||||
-rw-r--r-- | wikimini/templates/language.py | 41 |
2 files changed, 42 insertions, 0 deletions
@@ -11,5 +11,6 @@ setuptools.setup( "requests", "mwparserfromhell", "tabulate", + "pycountry", ], ) diff --git a/wikimini/templates/language.py b/wikimini/templates/language.py index 052b7f0..e8ab738 100644 --- a/wikimini/templates/language.py +++ b/wikimini/templates/language.py @@ -1,6 +1,8 @@ """Language related templates.""" from . import registry +import pycountry + def tmpl_ipa(wikimini, obj): """Renders the ``{{IPA|...}}`` template.""" @@ -17,3 +19,42 @@ def tmpl_lang(wikimini, obj): registry.insert("lang", tmpl_lang) registry.insert("script", tmpl_lang) + + +def tmpl_lang_code(language_name): + """Creates a template renderer for a ``{{lang-xx|...}}`` template.""" + def inner(wikimini, obj): + return "{}: {}".format( + language_name, wikimini._convert(obj.params[0].value) + ) + return inner + + +def tmpl_ipa_code(language_name): + """Creates a template renderer for a ``{{IPA-xx|...}}`` template.""" + def inner(wikimini, obj): + return "{} pronunciation: [{}]".format( + language_name, wikimini._convert(obj.params[0].value) + ) + return inner + + +for language in pycountry.languages: + if hasattr(language, "alpha_2"): + registry.insert( + f"lang-{language.alpha_2}", tmpl_lang_code(language.name) + ) + registry.insert( + f"IPA-{language.alpha_2}", tmpl_ipa_code(language.name) + ) + + +def tmpl_country_flag(country): + """Creates a template renderer for ``{{BRA}}`` country flags.""" + def inner(wikimini, obj): + return country + return inner + + +for country in pycountry.countries: + registry.insert(country.alpha_3, tmpl_country_flag(country.name)) |