Skip to content

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
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@ doc/source/savefig/
# Pyodide/WASM related files #
##############################
/.pyodide-xbuildenv-*


# Web & Translations #
##############################
web/preview/
web/translations/
web/pandas/es/
web/pandas/pt/
web/pandas/fr/
82 changes: 80 additions & 2 deletions web/pandas/_templates/layout.html
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>
Expand All @@ -15,6 +15,82 @@
href="{{ base_url }}{{ stylesheet }}">
{% endfor %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<script type="text/javascript">
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 = [
{% for lang, name in translations["languages"].items() -%}
"{{ lang }}",
{% endfor -%}
]
var languageNames = {
{% for lang, name in translations["languages"].items() -%}
{{ lang }}: '{{ name }}',
{% endfor -%}
}

// Handle preview URLs on github
// If preview URL changes, this regex will need to be updated
var 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want to create it with javascript instead of having the html in the template?

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);
}
});
</script>
</head>
<body>
<header>
Expand All @@ -28,7 +104,7 @@

<div class="collapse navbar-collapse" id="nav-content">
<ul class="navbar-nav ms-auto">
{% for item in navbar %}
{% for item in navbar[selected_language] %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's just me, but I find selected_language a bit confusing. Just language, lang, or if you need to be more explicit page_language would be better. "Selected" sounds like the user selected it, which in most cases won't be the case.

{% if not item.has_subitems %}
<li class="nav-item">
<a class="nav-link" href="{% if not item.target.startswith("http") %}{{ base_url }}{% endif %}{{ item.target }}">{{ item.name }}</a>
Expand All @@ -50,6 +126,8 @@
</li>
{% endif %}
{% endfor %}
<!-- Language switcher -->
<div id="language-switcher-container"></div>
</ul>
</div>
</div>
Expand Down
44 changes: 11 additions & 33 deletions web/pandas/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,6 @@ static:
css:
- static/css/pandas.css
- static/css/codehilite.css
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
blog:
num_posts: 50
posts_path: community/blog
Expand Down Expand Up @@ -204,3 +171,14 @@ sponsors:
kind: partner
roadmap:
pdeps_path: pdeps
translations:
url: https://github.com/Scientific-Python-Translations/pandas-translations/archive/refs/heads/main.tar.gz
folder: translations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are extracting directly to the final directory, I guess this is not needed anymore.

source_path: pandas-translations-main/web/pandas/
default_language: 'en'
default_prefix: ''
Comment on lines +178 to +179
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to disagree, but English will always be the base language, I'd personally hardcode it where needed, and not have a setting for it.

Not sure what's the default prefix, is this something we'll ever change?

languages:
en: English
es: Español
pt: Português
# fr: Français
34 changes: 34 additions & 0 deletions web/pandas/navbar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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/
translated: false
- 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
Loading
Loading