Skip to content

Commit

Permalink
Add convert_language_tag.py (#37)
Browse files Browse the repository at this point in the history
* Add convert_language_tag.py

* Use python explicitly for calling the script

Co-authored-by: Adam Turner <[email protected]>

* Use upper() directly instead of storing it

Co-authored-by: Adam Turner <[email protected]>

* Make  convert_language_tag.py read from READTHEDOCS_LANGUAGE

Read from READTHEDOCS_LANGUAGE instead of from argument

* Call convert_language_tag.py from sphinx-build

---------

Co-authored-by: Adam Turner <[email protected]>
  • Loading branch information
rffontenelle and AA-Turner authored May 7, 2024
1 parent bbd168f commit ac68470
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ build:
sphinx-build ./sphinx/doc/ ./_readthedocs/html
-b html
-D locale_dirs=$(realpath locale)
-D language=$READTHEDOCS_LANGUAGE
-D language=$(python convert_language_tag.py)
-D gettext_compact=0
58 changes: 58 additions & 0 deletions convert_language_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""
Convert a READTHEDOCS_LANGUAGE (ll-cc) to Sphinx-compatible format (ll_CC),
and print the result.
This script reads Read the Docs's READTHEDOCS_LANGUAGE environment variable,
and convert ll-cc into 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 os
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 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 *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():
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 match_language_tag(rtd_language, A_LANG_TAG_PATTERN):
sys.exit(
"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(rtd_language)
if match_language_tag(rtd_language, IETF_LANG_TAG_PATTERN)
else rtd_language
)
print(output_language)


if __name__ == "__main__":
main()

0 comments on commit ac68470

Please sign in to comment.