Skip to content

Commit 963774f

Browse files
authored
add tutorial localizations (#214)
1 parent 2d41f5d commit 963774f

3 files changed

Lines changed: 100 additions & 21 deletions

File tree

_layouts/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{%- if page.layout == "manual" and page.language -%}
1+
{%- if page.language -%}
22
<!DOCTYPE html>
33
<html lang="{{ page.language }}">
44
{%- else -%}

_layouts/tutorial.html

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
<div style="display: none;" data-pagefind-meta="section">Tutorials</div>
77
{% include learnnav.html showmenu=true %}
88

9+
{%- assign page_language = page.language | default: "en" -%}
10+
{%- assign language = site.data.languageindex | where: "language", page_language | first -%}
11+
{%- assign github_link = page.github | default: "https://github.com/defold/doc" -%}
12+
{%- for tutorial in site.data.learnindex.navigation.tutorials -%}
13+
{%- if tutorial.path contains "/tutorials/" -%}
14+
{%- assign tutorial_path = tutorial.path | append: "/" -%}
15+
{%- assign tutorial_path_size = tutorial_path | size -%}
16+
{%- assign page_url_size = page.url | size -%}
17+
{%- assign start_index = page_url_size | minus: tutorial_path_size -%}
18+
{%- assign result = page.url | slice: start_index, tutorial_path_size -%}
19+
{%- if result == tutorial_path -%}
20+
{%- assign current_tutorial = tutorial -%}
21+
{%- endif -%}
22+
{%- endif -%}
23+
{%- endfor -%}
24+
925
<div class="section lightest dark:dark">
1026
<div class="container">
1127
<div class="row">
@@ -18,30 +34,76 @@
1834
{%- assign checked = "checked" -%}
1935
{%- endif -%}
2036
<li style="padding-left: 2rem;">
21-
<a href="{{ tutorial.path }}" style="text-decoration: none;">{{ tutorial.name }}</a>
37+
{%- if tutorial.path contains "/tutorials/" -%}
38+
{%- if page_language == "en" -%}
39+
<a href="{{ tutorial.path }}" style="text-decoration: none;">{{ tutorial.name }}</a>
40+
{%- elsif tutorial.languages contains page_language -%}
41+
<a href="/{{ language.urlprefix }}{{ tutorial.path }}" style="text-decoration: none;">{{ tutorial.name }}</a>
42+
{%- else -%}
43+
<a href="{{ tutorial.path }}" style="text-decoration: none;">{{ tutorial.name }} (English)</a>
44+
{%- endif -%}
45+
{%- else -%}
46+
<a href="{{ tutorial.path }}" style="text-decoration: none;">{{ tutorial.name }}</a>
47+
{%- endif -%}
2248
</li>
2349
{% endfor %}
2450
</ul>
2551
</div>
2652
<div class="dropdownmenu">
2753
<select class="dropdownmenu" onchange="location = this.value;">
2854
{% for tutorial in site.data.learnindex.navigation.tutorials %}
29-
<option value="{{ tutorial.path }}" {% if page.url contains tutorial.path %}selected{% endif %}> - {{ tutorial.name }}</option>
55+
{%- if tutorial.path contains "/tutorials/" -%}
56+
{%- if page_language == "en" -%}
57+
<option value="{{ tutorial.path }}" {% if page.url contains tutorial.path %}selected{% endif %}> - {{ tutorial.name }}</option>
58+
{%- elsif tutorial.languages contains page_language -%}
59+
<option value="/{{ language.urlprefix }}{{ tutorial.path }}" {% if page.url contains tutorial.path %}selected{% endif %}> - {{ tutorial.name }}</option>
60+
{%- else -%}
61+
<option value="{{ tutorial.path }}" {% if page.url contains tutorial.path %}selected{% endif %}> - {{ tutorial.name }} (English)</option>
62+
{%- endif -%}
63+
{%- else -%}
64+
<option value="{{ tutorial.path }}" {% if page.url contains tutorial.path %}selected{% endif %}> - {{ tutorial.name }}</option>
65+
{%- endif -%}
3066
{% endfor %}
3167
</select>
3268
</div>
3369
</div>
34-
<div class="columns eight tutorial" data-pagefind-body>
70+
<div class="columns eight tutorial">
71+
{%- if page_language != "en" and current_tutorial -%}
72+
{%- capture urlprefix -%}/{{ language.urlprefix }}/{%- endcapture -%}
73+
<div class="sidenote">
74+
<p>This translation is community contributed and may not be up to date. We only maintain the English version of the documentation. <a href="{{ page.url | replace: urlprefix, "/" }}">Read this tutorial in English</a></p>
75+
</div>
76+
{%- endif -%}
77+
<div data-pagefind-body>
3578
{%- if page.difficulty -%}
3679
<p style="margin-top: 0;"><span class="difficulty-tag">{{ page.difficulty }}</span></p>
3780
{%- endif -%}
3881
{% include anchor_headings.html html=content anchorClass="anchor-link" %}
82+
</div>
3983
</div>
4084
<div class="columns two">
85+
{%- if site.data.languageindex.size > 1 and current_tutorial -%}
86+
<input class="toggle" type="checkbox" checked/>
87+
<label class="togglelabel">Languages</label>
88+
<ul class="nobullet">
89+
{%- for l in site.data.languageindex -%}
90+
{%- if l.active and current_tutorial.languages contains l.language -%}
91+
<li style="padding-left: 2rem;">
92+
{%- if l.language == "en" -%}
93+
<a href='{{ current_tutorial.path }}' style="text-decoration: none;">{{ l.name }}</a>
94+
{%- elsif current_tutorial.languages contains l.language -%}
95+
<a href='/{{ l.urlprefix }}{{ current_tutorial.path }}' style="text-decoration: none;">{{ l.name }}</a>
96+
{%- endif -%}
97+
</li>
98+
{%- endif -%}
99+
{%- endfor -%}
100+
</ul>
101+
<hr/>
102+
{%- endif -%}
41103
<p>
42104
Did you spot an error or do you have a suggestion? Please let us know on GitHub!
43105
</p>
44-
{% include secondary_button.html text="GITHUB" link="https://github.com/defold/doc" %}
106+
{% include secondary_button.html text="GITHUB" link=github_link %}
45107
</div>
46108
</div>
47109
</div>

update.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,19 @@ def get_language_specific_dir(language, dir):
319319
dir = os.path.join(language, dir)
320320
return dir
321321

322+
323+
def get_index_item_languages(item, languages):
324+
item_languages = []
325+
if not item["path"].startswith("http"):
326+
path = item["path"][1:]
327+
# foo/bar/#anchor -> foo/bar
328+
# foo/bar#anchor -> foo/bar
329+
path = re.sub(r"/?\#.*", "", path)
330+
for language in languages["languages"].keys():
331+
if os.path.exists(get_language_specific_dir(language, path + ".md")):
332+
item_languages.append(language)
333+
return item_languages
334+
322335
def update_file_links_with_lang(filename, pattern, language):
323336
# Open the file and read its content
324337
with open(filename, 'r') as f:
@@ -485,13 +498,22 @@ def process_docs(download = False):
485498
for filename in find_files(shared_includes_dst_dir, "*.md"):
486499
process_doc_file(filename, language)
487500

488-
print("...tutorials")
489-
tutorials_src_dir = os.path.join(DOC_DIR, "docs", "en", "tutorials")
490-
tutorials_dst_dir = "tutorials"
491-
rmcopytree(tutorials_src_dir, tutorials_dst_dir)
492-
for filename in find_files(tutorials_dst_dir, "*.md"):
493-
process_doc_file(filename, "en")
494-
append_frontmatter(filename, { "layout": "tutorial" })
501+
for language in languages["languages"].keys():
502+
print("...tutorials ({})".format(language))
503+
tutorials_src_dir = os.path.join(DOC_DIR, "docs", language, "tutorials")
504+
if os.path.exists(tutorials_src_dir):
505+
tutorials_dst_dir = get_language_specific_dir(language, "tutorials")
506+
rmcopytree(tutorials_src_dir, tutorials_dst_dir)
507+
for filename in find_files(tutorials_dst_dir, "*.md"):
508+
process_doc_file(filename, language)
509+
append_frontmatter(filename, {
510+
"language": language,
511+
"layout": "tutorial",
512+
})
513+
if language != "en":
514+
update_file_links_with_lang(filename, r'/manuals/[^)#]+', language)
515+
update_file_links_with_lang(filename, r'/tutorials/[^)#]+', language)
516+
replace_in_file(filename, r"\.\.\/images\/", r"/tutorials/images/")
495517

496518
print("...courses")
497519
courses_src_dir = os.path.join(DOC_DIR, "docs", "en", "courses")
@@ -505,15 +527,10 @@ def process_docs(download = False):
505527
print("...index (incl. languages)")
506528
for section in index["navigation"]["manuals"]:
507529
for item in section["items"]:
508-
item["languages"] = []
509-
if not item["path"].startswith("http"):
510-
path = item["path"][1:]
511-
# foo/bar/#anchor -> foo/bar
512-
# foo/bar#anchor -> foo/bar
513-
path = re.sub(r"/?\#.*", "", path)
514-
for language in languages["languages"].keys():
515-
if os.path.exists(get_language_specific_dir(language, path + ".md")):
516-
item["languages"].append(language)
530+
item["languages"] = get_index_item_languages(item, languages)
531+
for item in index["navigation"]["tutorials"]:
532+
if not item["path"].startswith("http"):
533+
item["languages"] = get_index_item_languages(item, languages)
517534
write_as_json(index_file, index)
518535

519536
print("...shared images")

0 commit comments

Comments
 (0)