From d02c038d77bc4deb805aa1cb6cace3bc577f3a41 Mon Sep 17 00:00:00 2001 From: Rafael Fontenelle Date: Sun, 28 Apr 2024 16:28:20 -0300 Subject: [PATCH 1/5] Add convert_language_tag.py --- .readthedocs.yml | 3 ++- convert_language_tag.py | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100755 convert_language_tag.py diff --git a/.readthedocs.yml b/.readthedocs.yml index 915a7cd132..fc5dfd52a1 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -5,6 +5,7 @@ build: tools: python: "3" commands: + - export LANGUAGE=$(./convert_language_tag.py $READTHEDOCS_LANGUAGE) # install requirements - pip install -r requirements.txt # initialise submodule (https://github.com/sphinx-doc/sphinx.git) @@ -15,5 +16,5 @@ build: sphinx-build ./sphinx/doc/ ./_readthedocs/html -b html -D locale_dirs=$(realpath locale) - -D language=$READTHEDOCS_LANGUAGE + -D language=$LANGUAGE -D gettext_compact=0 diff --git a/convert_language_tag.py b/convert_language_tag.py new file mode 100755 index 0000000000..95838a4abf --- /dev/null +++ b/convert_language_tag.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +""" +Convert a lowercased IETF language tag (ll-cc) to Gettext's ISO 639 (ll_CC), +and print the result. + +This script will return the same input value if the language tag doesn't have +the country part (ll) or it is already a Gettext's ISO 639 (ll_CC). + +Exit with error in case the input is not a valid language tag +(i.e. not ll-cc or ll_CC) +""" + +import argparse +import re +import sys + +IETF_LANG_TAG_PATTERN = r"^[a-z]{2}-[a-z]{2}$" +A_LANG_TAG_PATTERN = r"^[a-z]{2}(-[a-z]{2}|_[A-Z]{2})?$" + + +def is_valid_language_tag(input_str: str, pattern: str) -> bool: + """Check if the input string matches the language tag pattern.""" + return bool(re.match(pattern, input_str)) + + +def convert_to_iso639_gettext(input_str: str) -> str: + """Convert a ll-cc to ll_CC language code.""" + first_part, _, last_part = input_str.partition("-") + last_part_upper = last_part.upper() + return f"{first_part}_{last_part_upper}" + + +def main(): + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawTextHelpFormatter + ) + parser.add_argument("language") + arg = parser.parse_args() + + if not is_valid_language_tag(arg.language, A_LANG_TAG_PATTERN): + sys.exit( + f"Error: Expected a language code like pt_BR or pt-br. " + f"Invalid language code given: {arg.language}" + ) + + output_language = ( + convert_to_iso639_gettext(arg.language) + if is_valid_language_tag(arg.language, IETF_LANG_TAG_PATTERN) + else arg.language + ) + print(output_language) + + +if __name__ == "__main__": + main() From 9845b5b6a58b407ca9e0fd29cb707653a4e5319e Mon Sep 17 00:00:00 2001 From: Rafael Fontenelle Date: Mon, 29 Apr 2024 05:20:50 -0300 Subject: [PATCH 2/5] Use python explicitly for calling the script Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- .readthedocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.readthedocs.yml b/.readthedocs.yml index fc5dfd52a1..cb5ebe28fd 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -5,7 +5,7 @@ build: tools: python: "3" commands: - - export LANGUAGE=$(./convert_language_tag.py $READTHEDOCS_LANGUAGE) + - export LANGUAGE=$(python convert_language_tag.py $READTHEDOCS_LANGUAGE) # install requirements - pip install -r requirements.txt # initialise submodule (https://github.com/sphinx-doc/sphinx.git) From 4b5b400033536ae5247dfedc88cf09afd00b18f4 Mon Sep 17 00:00:00 2001 From: Rafael Fontenelle Date: Mon, 29 Apr 2024 05:23:25 -0300 Subject: [PATCH 3/5] Use upper() directly instead of storing it Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- convert_language_tag.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/convert_language_tag.py b/convert_language_tag.py index 95838a4abf..d8f3ca96f9 100755 --- a/convert_language_tag.py +++ b/convert_language_tag.py @@ -26,8 +26,7 @@ def is_valid_language_tag(input_str: str, pattern: str) -> bool: def convert_to_iso639_gettext(input_str: str) -> str: """Convert a ll-cc to ll_CC language code.""" first_part, _, last_part = input_str.partition("-") - last_part_upper = last_part.upper() - return f"{first_part}_{last_part_upper}" + return f"{first_part}_{last_part.upper()}" def main(): From 4b16c8f1470440e1fdc5242330e1e842c64bb5b8 Mon Sep 17 00:00:00 2001 From: Rafael Fontenelle Date: Tue, 7 May 2024 16:58:23 -0300 Subject: [PATCH 4/5] Make convert_language_tag.py read from READTHEDOCS_LANGUAGE Read from READTHEDOCS_LANGUAGE instead of from argument --- convert_language_tag.py | 44 ++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/convert_language_tag.py b/convert_language_tag.py index d8f3ca96f9..9324ea7c9a 100755 --- a/convert_language_tag.py +++ b/convert_language_tag.py @@ -1,16 +1,19 @@ #!/usr/bin/env python3 """ -Convert a lowercased IETF language tag (ll-cc) to Gettext's ISO 639 (ll_CC), +Convert a READTHEDOCS_LANGUAGE (ll-cc) to Sphinx-compatible format (ll_CC), and print the result. -This script will return the same input value if the language tag doesn't have -the country part (ll) or it is already a Gettext's ISO 639 (ll_CC). +This script reads Read the Docs's READTHEDOCS_LANGUAGE environment variable, +and convert ll-cc into ll_CC. -Exit with error in case the input is not a valid language tag -(i.e. not ll-cc or ll_CC) +The output is the same as input if the input language has only language +without country part ('ll') or is already Gettext's ISO 639 (ll_CC). + +Exit with error in case READTHEDOCS_LANGUAGE is not set or th input is not +one of the valid language formats (ll, ll-cc or ll_CC) """ -import argparse +import os import re import sys @@ -18,34 +21,35 @@ A_LANG_TAG_PATTERN = r"^[a-z]{2}(-[a-z]{2}|_[A-Z]{2})?$" -def is_valid_language_tag(input_str: str, pattern: str) -> bool: - """Check if the input string matches the language tag pattern.""" +def match_language_tag(input_str: str, pattern: str) -> bool: + """Match the *input_str* string with the language tag *pattern*.""" return bool(re.match(pattern, input_str)) def convert_to_iso639_gettext(input_str: str) -> str: - """Convert a ll-cc to ll_CC language code.""" + """Convert *input_str* from ll-cc to ll_CC language format.""" first_part, _, last_part = input_str.partition("-") return f"{first_part}_{last_part.upper()}" def main(): - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawTextHelpFormatter - ) - parser.add_argument("language") - arg = parser.parse_args() + rtd_language = os.getenv("READTHEDOCS_LANGUAGE") + if not rtd_language: + sys.exit( + "Error: READTHEDOCS_LANGUAGE not set, " + + "is this being run in readthedocs build env?" + ) - if not is_valid_language_tag(arg.language, A_LANG_TAG_PATTERN): + if not match_language_tag(rtd_language, A_LANG_TAG_PATTERN): sys.exit( - f"Error: Expected a language code like pt_BR or pt-br. " - f"Invalid language code given: {arg.language}" + "Error: Expected a language code like pt, pt_BR or pt-br. " + f"Invalid language code given: {rtd_language}" ) output_language = ( - convert_to_iso639_gettext(arg.language) - if is_valid_language_tag(arg.language, IETF_LANG_TAG_PATTERN) - else arg.language + convert_to_iso639_gettext(rtd_language) + if match_language_tag(rtd_language, IETF_LANG_TAG_PATTERN) + else rtd_language ) print(output_language) From a221648df89f7be75405094e583197b05bfa3c81 Mon Sep 17 00:00:00 2001 From: Rafael Fontenelle Date: Tue, 7 May 2024 16:59:18 -0300 Subject: [PATCH 5/5] Call convert_language_tag.py from sphinx-build --- .readthedocs.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.readthedocs.yml b/.readthedocs.yml index cb5ebe28fd..14b4364905 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -5,7 +5,6 @@ build: tools: python: "3" commands: - - export LANGUAGE=$(python convert_language_tag.py $READTHEDOCS_LANGUAGE) # install requirements - pip install -r requirements.txt # initialise submodule (https://github.com/sphinx-doc/sphinx.git) @@ -16,5 +15,5 @@ build: sphinx-build ./sphinx/doc/ ./_readthedocs/html -b html -D locale_dirs=$(realpath locale) - -D language=$LANGUAGE + -D language=$(python convert_language_tag.py) -D gettext_compact=0