-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Implement translations infrastructure #61380
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
Open
goanpeca
wants to merge
11
commits into
pandas-dev:main
Choose a base branch
from
goanpeca:translations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d7f0545
Add script to import translations
goanpeca 17063a7
Update scripts to handle tranlsations
goanpeca 8eec135
Merge branch 'main' of github.com:pandas-dev/pandas into translations
goanpeca 7666eb3
Move translations source path to config
goanpeca 30efbba
Fix language switcher to handle previews
goanpeca f8e6a02
Simplify scripts and logic
goanpeca de291cd
Process all languages in a single step
goanpeca 0d2bfff
Simplify code and move download and extract to pandas_web
goanpeca ac0b8f0
Create preprocessor and fix review comments
goanpeca 99e9635
Split preprocessor logic and more code review changes
goanpeca eadb5c9
Merge branch 'main' into translations
goanpeca 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
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,5 +1,5 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<html lang="{{ selected_language }}"> | ||
<head> | ||
<script defer data-domain="pandas.pydata.org" src="https://views.scientific-python.org/js/script.js"></script> | ||
<title>pandas - Python Data Analysis Library</title> | ||
|
@@ -15,6 +15,8 @@ | |
href="{{ base_url }}{{ stylesheet }}"> | ||
{% endfor %} | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css"> | ||
<meta id="languages" data-lang="{{ languages }}"> | ||
<script src="{{ base_url }}static/js/language_switcher.js"></script> | ||
</head> | ||
<body> | ||
<header> | ||
|
@@ -50,6 +52,8 @@ | |
</li> | ||
{% endif %} | ||
{% endfor %} | ||
<!-- Language switcher --> | ||
<div id="language-switcher-container"></div> | ||
</ul> | ||
</div> | ||
</div> | ||
|
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,33 @@ | ||
navbar: | ||
- name: "About us" | ||
target: | ||
- name: "About pandas" | ||
target: about/ | ||
- name: "Project roadmap" | ||
target: about/roadmap.html | ||
- name: "Governance" | ||
target: about/governance.html | ||
- name: "Team" | ||
target: about/team.html | ||
- name: "Sponsors" | ||
target: about/sponsors.html | ||
- name: "Citing and logo" | ||
target: about/citing.html | ||
- name: "Getting started" | ||
target: getting_started.html | ||
- name: "Documentation" | ||
target: docs/ | ||
- name: "Community" | ||
target: | ||
- name: "Blog" | ||
target: community/blog/ | ||
- name: "Ask a question (StackOverflow)" | ||
target: https://stackoverflow.com/questions/tagged/pandas | ||
- name: "Code of conduct" | ||
target: community/coc.html | ||
- name: "Ecosystem" | ||
target: community/ecosystem.html | ||
- name: "Benchmarks" | ||
target: community/benchmarks.html | ||
- name: "Contribute" | ||
target: contribute.html |
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,71 @@ | ||
window.addEventListener("DOMContentLoaded", function() { | ||
var absBaseUrl = document.baseURI; | ||
var baseUrl = location.protocol + "//" + location.hostname | ||
if (location.port) { | ||
baseUrl = baseUrl + ":" + location.port | ||
} | ||
var currentLanguage = document.documentElement.lang; | ||
var languages = JSON.parse(document.getElementById("languages").getAttribute('data-lang').replace(/'/g, '"')); | ||
const languageNames = { | ||
'en': 'English', | ||
'es': 'Español', | ||
'fr': 'Français', | ||
'pt': 'Português' | ||
} | ||
goanpeca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Handle preview URLs on github | ||
// If preview URL changes, this regex will need to be updated | ||
const re = /preview\/pandas-dev\/pandas\/(?<pr>[0-9]*)\//g; | ||
var previewUrl = ''; | ||
for (const match of absBaseUrl.matchAll(re)) { | ||
previewUrl = `/preview/pandas-dev/pandas/${match.groups.pr}`; | ||
} | ||
var pathName = location.pathname.replace(previewUrl, '') | ||
|
||
// Create dropdown menu | ||
function makeDropdown(options) { | ||
var dropdown = document.createElement("li"); | ||
dropdown.classList.add("nav-item"); | ||
dropdown.classList.add("dropdown"); | ||
|
||
var link = document.createElement("a"); | ||
link.classList.add("nav-link"); | ||
link.classList.add("dropdown-toggle"); | ||
link.setAttribute("data-bs-toggle", "dropdown"); | ||
link.setAttribute("href", "#"); | ||
link.setAttribute("role", "button"); | ||
link.setAttribute("aria-haspopup", "true"); | ||
link.setAttribute("aria-expanded", "false"); | ||
link.textContent = languageNames[currentLanguage]; | ||
|
||
var dropdownMenu = document.createElement("div"); | ||
dropdownMenu.classList.add("dropdown-menu"); | ||
|
||
options.forEach(function(i) { | ||
var dropdownItem = document.createElement("a"); | ||
dropdownItem.classList.add("dropdown-item"); | ||
dropdownItem.textContent = languageNames[i] || i.toUpperCase(); | ||
dropdownItem.setAttribute("href", "#"); | ||
dropdownItem.addEventListener("click", function() { | ||
var urlLanguage = ''; | ||
if (i !== 'en') { | ||
urlLanguage = '/' + i; | ||
} | ||
pathName = pathName.replace('/' + currentLanguage + '/', '/') | ||
var newUrl = baseUrl + previewUrl + urlLanguage + pathName | ||
window.location.href = newUrl; | ||
}); | ||
dropdownMenu.appendChild(dropdownItem); | ||
}); | ||
|
||
dropdown.appendChild(link); | ||
dropdown.appendChild(dropdownMenu); | ||
return dropdown; | ||
} | ||
|
||
var container = document.getElementById("language-switcher-container"); | ||
if (container) { | ||
var dropdown = makeDropdown(languages); | ||
container.appendChild(dropdown); | ||
} | ||
}); |
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,109 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Utilities to download and extract translations from the GitHub repository. | ||
goanpeca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
import io | ||
import os | ||
import shutil | ||
from subprocess import ( | ||
PIPE, | ||
Popen, | ||
) | ||
import sys | ||
import tarfile | ||
|
||
import requests | ||
import yaml | ||
|
||
|
||
def get_config(config_fname: str) -> dict: | ||
""" | ||
Load the config yaml file and return it as a dictionary. | ||
""" | ||
with open(config_fname, encoding="utf-8") as f: | ||
context = yaml.safe_load(f) | ||
return context | ||
|
||
|
||
def download_and_extract_translations(url: str, dir_name: str) -> None: | ||
""" | ||
Download the translations from the GitHub repository. | ||
""" | ||
shutil.rmtree(dir_name, ignore_errors=True) | ||
response = requests.get(url) | ||
if response.status_code == 200: | ||
doc = io.BytesIO(response.content) | ||
with tarfile.open(None, "r:gz", doc) as tar: | ||
tar.extractall(dir_name) | ||
else: | ||
raise Exception(f"Failed to download translations: {response.status_code}") | ||
|
||
|
||
def get_languages(source_path: str) -> list[str]: | ||
""" | ||
Get the list of languages available in the translations directory. | ||
""" | ||
en_path = f"{source_path}/en/" | ||
if os.path.exists(en_path): | ||
shutil.rmtree(en_path) | ||
|
||
paths = os.listdir(source_path) | ||
return [path for path in paths if os.path.isdir(f"{source_path}/{path}")] | ||
|
||
|
||
def remove_translations(source_path: str, languages: list[str]) -> None: | ||
""" | ||
Remove the translations from the source path. | ||
""" | ||
for language in languages: | ||
shutil.rmtree(os.path.join(source_path, language), ignore_errors=True) | ||
|
||
|
||
def copy_translations(source_path: str, target_path: str, languages: list[str]) -> None: | ||
""" | ||
Copy the translations to the appropriate directory. | ||
""" | ||
for lang in languages: | ||
dest = f"{target_path}/{lang}/" | ||
shutil.rmtree(dest, ignore_errors=True) | ||
cmds = [ | ||
"rsync", | ||
"-av", | ||
"--delete", | ||
f"{source_path}/{lang}/", | ||
dest, | ||
] | ||
p = Popen(cmds, stdout=PIPE, stderr=PIPE) | ||
stdout, stderr = p.communicate() | ||
sys.stderr.write(f"\nCopying: {lang}...\n\n") | ||
sys.stderr.write(stdout.decode()) | ||
sys.stderr.write(stderr.decode()) | ||
|
||
|
||
def process_translations( | ||
config_fname: str, source_path: str, process_translations: bool | ||
) -> tuple[list[str], list[str]]: | ||
""" | ||
Process the translations by downloading and extracting them from | ||
the GitHub repository. | ||
""" | ||
base_folder = os.path.dirname(__file__) | ||
config = get_config(os.path.join(source_path, config_fname)) | ||
translations_path = os.path.join(base_folder, f"{config['translations']['folder']}") | ||
translations_source_path = os.path.join( | ||
translations_path, config["translations"]["source_path"] | ||
) | ||
default_language = config["translations"]["default_language"] | ||
sys.stderr.write("\nDownloading and extracting translations...\n\n") | ||
download_and_extract_translations(config["translations"]["url"], translations_path) | ||
languages = [default_language] | ||
translated_languages = get_languages(translations_source_path) | ||
remove_translations(source_path, translated_languages) | ||
if process_translations: | ||
languages = [default_language] + translated_languages | ||
|
||
sys.stderr.write("\nCopying translations...\n") | ||
copy_translations(translations_source_path, source_path, translated_languages) | ||
|
||
return translated_languages, languages |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.