-
-
Notifications
You must be signed in to change notification settings - Fork 129
Add autotranslation of releases to build step #654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1a4e904
Autotranslate release sections for portuguese and japanese
steppi 80eb86c
Add some top level docstrings to autotranslate scripts
steppi 6303efd
Add removal of generated content to make clean
steppi ab08509
Add installation of pyyaml to build
steppi d7daa49
Add requirements.txt
steppi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| generated/ | ||
| public/ | ||
| resources/ | ||
| config.yaml | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| {{ $lang := .Page.Lang }} | ||
| {{ $path := printf "generated/releases/%s/releases.md" $lang }} | ||
| {{ readFile $path | markdownify }} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| PyYAML |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import os | ||
| import yaml | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| def get_languages(): | ||
| """Get languages that are currently active on website.""" | ||
| current_dir = Path(__file__) | ||
| toplevel_dir = current_dir.parent.parent.parent | ||
| config_path = toplevel_dir / "config.yaml" | ||
| with open(config_path) as f: | ||
| data = yaml.safe_load(f) | ||
| return list(data["languages"]) | ||
|
|
||
|
|
||
| def translate_file( | ||
| input_filepath: str, output_filepath: str, translation_func: callable | ||
| ) -> None: | ||
| """Apply translation function to text in a file and save to new location. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| input_filepath : str | ||
| Path to file to be translated | ||
| output_filepath : str | ||
| Path where translated file will be saved. | ||
| translation_func : callable | ||
| Function from str to str which translates input. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| with open(input_filepath, 'r') as input_file: | ||
| data = input_file.read() | ||
|
|
||
| transformed_data = translation_func(data) | ||
|
|
||
| with open(output_filepath, 'w') as output_file: | ||
| output_file.write(transformed_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import os | ||
| import yaml | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| def get_languages(): | ||
| """Get languages that are currently active on website.""" | ||
| current_dir = Path(__file__) | ||
| toplevel_dir = current_dir.parent.parent.parent | ||
| config_path = toplevel_dir / "config.yaml" | ||
| with open(config_path) as f: | ||
| data = yaml.safe_load(f) | ||
| return list(data["languages"]) | ||
|
|
||
|
|
||
| def translate_file( | ||
| input_filepath: str, output_filepath: str, translation_func: callable | ||
| ) -> None: | ||
| """Apply translation function each line in a file and save to new location. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| input_filepath : str | ||
| Path to file to be translated | ||
| output_filepath : str | ||
| Path where translated file will be saved. | ||
| translation_func : callable | ||
| Function from str to str which translates one line of input. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| with open(input_filepath, 'r') as input_file: | ||
| lines = input_file.readlines() | ||
| output = [] | ||
| for i, line in enumerate(lines): | ||
| line = line.strip() | ||
| if not line: | ||
| continue | ||
| try: | ||
| output.append(translation_func(line)) | ||
| except ValueError as e: | ||
| print(f"Problem with input line {i+t}, {line}") | ||
| output = "\n".join(output) + "\n" | ||
| with open(output_filepath, 'w') as output_file: | ||
| output_file.write(output) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| - NumPy 1.25.1 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.25.1)) -- _8 Jul 2023_. | ||
| - NumPy 1.24.4 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.24.4)) -- _26 Jun 2023_. | ||
| - NumPy 1.25.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.25.0)) -- _17 Jun 2023_. | ||
| - NumPy 1.24.3 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.24.3)) -- _22 Apr 2023_. | ||
| - NumPy 1.24.2 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.24.2)) -- _5 Feb 2023_. | ||
| - NumPy 1.24.1 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.24.1)) -- _26 Dec 2022_. | ||
| - NumPy 1.24.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.24.0)) -- _18 Dec 2022_. | ||
| - NumPy 1.23.5 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.23.5)) -- _19 Nov 2022_. | ||
| - NumPy 1.23.4 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.23.4)) -- _12 Oct 2022_. | ||
| - NumPy 1.23.3 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.23.3)) -- _9 Sep 2022_. | ||
| - NumPy 1.23.2 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.23.2)) -- _14 Aug 2022_. | ||
| - NumPy 1.23.1 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.23.1)) -- _8 Jul 2022_. | ||
| - NumPy 1.23.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.23.0)) -- _22 Jun 2022_. | ||
| - NumPy 1.22.4 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.22.4)) -- _20 May 2022_. | ||
| - NumPy 1.21.6 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.21.6)) -- _12 Apr 2022_. | ||
| - NumPy 1.22.3 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.22.3)) -- _7 Mar 2022_. | ||
| - NumPy 1.22.2 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.22.2)) -- _3 Feb 2022_. | ||
| - NumPy 1.22.1 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.22.1)) -- _14 Jan 2022_. | ||
| - NumPy 1.22.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.22.0)) -- _31 Dec 2021_. | ||
| - NumPy 1.21.5 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.21.5)) -- _19 Dec 2021_. | ||
| - NumPy 1.21.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.21.0)) -- _22 Jun 2021_. | ||
| - NumPy 1.20.3 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.20.3)) -- _10 May 2021_. | ||
| - NumPy 1.20.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.20.0)) -- _30 Jan 2021_. | ||
| - NumPy 1.19.5 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.19.5)) -- _5 Jan 2021_. | ||
| - NumPy 1.19.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.19.0)) -- _20 Jun 2020_. | ||
| - NumPy 1.18.4 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.18.4)) -- _3 May 2020_. | ||
| - NumPy 1.17.5 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.17.5)) -- _1 Jan 2020_. | ||
| - NumPy 1.18.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.18.0)) -- _22 Dec 2019_. | ||
| - NumPy 1.17.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.17.0)) -- _26 Jul 2019_. | ||
| - NumPy 1.16.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.16.0)) -- _14 Jan 2019_. | ||
| - NumPy 1.15.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.15.0)) -- _23 Jul 2018_. | ||
| - NumPy 1.14.0 ([release notes](https://github.com/numpy/numpy/releases/tag/v1.14.0)) -- _7 Jan 2018_. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| """Automatic translation of list of releases. | ||
|
|
||
| To add a new language: | ||
|
|
||
| First add a function that translates an English datestring of the form "%d %b | ||
| %Y" to the corresponding string in the target language to the dictionary | ||
| ``translate_date_funcs``, keyed by the associated two letter language code. | ||
|
|
||
| Second, add a translation for the English phrase "release notes" to the | ||
| ``release_notes`` dictionary, keyed by the associated two letter language | ||
| code. | ||
|
|
||
| At build time, the releases.md file at | ||
|
|
||
| ``"scripts/autotranslate/data/releases.md"`` | ||
|
|
||
| will be translated and translations will be added to | ||
|
|
||
| ``"generated/releases/xx/releases.md"`` | ||
|
|
||
| for language codes xx corresponding to languages for which translations will | ||
| be published. | ||
| """ | ||
|
|
||
|
|
||
| import functools | ||
| import os | ||
| import re | ||
| import yaml | ||
|
|
||
| from datetime import datetime | ||
| from pathlib import Path | ||
|
|
||
| from autotranslate_common import get_languages, translate_file | ||
|
|
||
|
|
||
| month_names_pt = [ | ||
| "janeiro", "fevereiro", "março", "abril", "maio", "junho", | ||
| "julho", "agosto", "setembro", "outubro", "novembro", "dezembro" | ||
| ] | ||
|
|
||
|
|
||
| def translate_date_en_ja(date_string: str) -> str: | ||
| date_object = datetime.strptime(date_string, "%d %b %Y") | ||
| output = date_object.strftime("%Y年%m月%d日") | ||
| # Remove zero padding if needed before return | ||
| return output.replace("年0", "年").replace("月0", "月") | ||
|
|
||
|
|
||
| def translate_date_en_pt(date_string: str) -> str: | ||
| date_object = datetime.strptime(date_string, "%d %b %Y") | ||
| return ( | ||
| f"{date_object.day} de {month_names_pt[date_object.month - 1]}" | ||
| f" de {date_object.year}" | ||
| ) | ||
|
|
||
|
|
||
| translate_date_funcs = { | ||
| "pt": translate_date_en_pt, | ||
| "ja": translate_date_en_ja, | ||
| } | ||
|
|
||
|
|
||
| def translate_date(date_string, language_code): | ||
| return translate_date_funcs[language_code](date_string) | ||
|
|
||
|
|
||
| release_notes = { | ||
| "pt": "notas de versão", | ||
| "ja": "リリースノート", | ||
| } | ||
|
|
||
|
|
||
| def _translate_release_line(release_line: str, language_code: str) -> str: | ||
| """Translate a single line from releases file.""" | ||
| if language_code not in release_notes: | ||
| # Fall back to no translation if language is unsupported. | ||
| return release_line | ||
| pattern = ( | ||
| r"- NumPy (?P<version>\d+\.\d+\.\d+) " | ||
| r"\(\[release notes\]\((?P<url>.+)\)\) " | ||
| r"-- _(?P<date>.+)_\." | ||
| ) | ||
| match = re.search(pattern, release_line) | ||
| if not match: | ||
| raise ValueError(f"Invalid input for release_line, {release_line}") | ||
| version = match.group("version") | ||
| url = match.group("url") | ||
| date = match.group("date") | ||
| return ( | ||
| f"- NumPy {version} ([{release_notes[language_code]}]({url})) --" | ||
| f" _{translate_date(date, language_code)}_." | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| current_dir = Path(__file__).parent | ||
| toplevel_dir = current_dir.parent.parent | ||
| releases_path = current_dir / "data" / "releases.md" | ||
| for language_code in get_languages(): | ||
| destination_dir = toplevel_dir / "generated" / "releases" / language_code | ||
| os.makedirs(destination_dir, exist_ok=True) | ||
| outpath = destination_dir / "releases.md" | ||
| translate_func = functools.partial( | ||
| _translate_release_line, language_code=language_code | ||
| ) | ||
| translate_file(releases_path, outpath, translate_func) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to use one of the existing shortcodes provided by the theme:
https://theme.scientific-python.org/shortcodes/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we could also just use the
include-mdshort-code and in thenews.mdfor each language, hard code the path to thereleases.mdfor that particular language. Figuring out the language in the shortcode and generating the path dynamically might be too clever for its own good. Do you think the first option I mentioned with justinclude-mdis better?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a simpler option is to generate a JSON data file, and to then use a language-specific template to generate the release lines:
https://gohugo.io/templates/data-templates/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, cool. Thanks @stefanv, I didn't know about this stuff! It looks like I've re-implemented something Hugo is already pretty good at. Data-templates does seem like the way to go.