Skip to content

Commit 1cbf0e2

Browse files
authored
Merge pull request #354 from github/i18n-switcher
Locale switching
2 parents 9121deb + 50432e5 commit 1cbf0e2

File tree

5 files changed

+75
-16
lines changed

5 files changed

+75
-16
lines changed

_config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ description: A community guide for open source creators.
44
# See: docs/translations.md
55
locale: en-US
66
translations:
7-
- locale: en-US
7+
en-US:
88
name: English (US)
9-
repository: github/open-source-guide
9+
url: https://opensource.guide
1010

1111
exclude:
1212
- bin

_includes/footer.html

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,30 @@ <h3 class="alt-h3 mb-3">{{ site.data.locale[site.locale].footer.subscribe.headin
4141
</div>
4242

4343
<div class="border-top text-gray py-5">
44-
{% capture code %}
45-
{% assign code_label = site.data.locale[site.locale].footer.byline.code_label %}
46-
<svg height="20" class="octicon octicon-code v-align-middle fill-gray mr-1" aria-label="{{ code_label }}" viewBox="0 0 14 16" version="1.1" width="17" role="img"><path d="M9.5 3L8 4.5 11.5 8 8 11.5 9.5 13 14 8 9.5 3zm-5 0L0 8l4.5 5L6 11.5 2.5 8 6 4.5 4.5 3z"></path></svg>
47-
{% endcapture %}
48-
{% capture love %}
49-
{% assign love_label = site.data.locale[site.locale].footer.byline.love_label %}
50-
<svg height="20" class="octicon octicon-heart v-align-middle fill-gray mx-1" aria-label="{{love_label}}" viewBox="0 0 12 16" version="1.1" width="15" role="img"><path d="M11.2 3c-.52-.63-1.25-.95-2.2-1-.97 0-1.69.42-2.2 1-.51.58-.78.92-.8 1-.02-.08-.28-.42-.8-1-.52-.58-1.17-1-2.2-1-.95.05-1.69.38-2.2 1-.52.61-.78 1.28-.8 2 0 .52.09 1.52.67 2.67C1.25 8.82 3.01 10.61 6 13c2.98-2.39 4.77-4.17 5.34-5.33C11.91 6.51 12 5.5 12 5c-.02-.72-.28-1.39-.8-2.02V3z"></path></svg>
51-
{% endcapture %}
52-
{% capture github %}
53-
<svg height="20" class="octicon octicon-mark-github v-align-middle fill-gray mx-1" aria-label="GitHub" viewBox="0 0 16 16" version="1.1" width="20" role="img"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg>
54-
{% endcapture %}
44+
<div>
45+
{% capture code %}
46+
{% assign code_label = site.data.locale[site.locale].footer.byline.code_label %}
47+
<svg height="20" class="octicon octicon-code v-align-middle fill-gray mr-1" aria-label="{{ code_label }}" viewBox="0 0 14 16" version="1.1" width="17" role="img"><path d="M9.5 3L8 4.5 11.5 8 8 11.5 9.5 13 14 8 9.5 3zm-5 0L0 8l4.5 5L6 11.5 2.5 8 6 4.5 4.5 3z"></path></svg>
48+
{% endcapture %}
49+
{% capture love %}
50+
{% assign love_label = site.data.locale[site.locale].footer.byline.love_label %}
51+
<svg height="20" class="octicon octicon-heart v-align-middle fill-gray mx-1" aria-label="{{love_label}}" viewBox="0 0 12 16" version="1.1" width="15" role="img"><path d="M11.2 3c-.52-.63-1.25-.95-2.2-1-.97 0-1.69.42-2.2 1-.51.58-.78.92-.8 1-.02-.08-.28-.42-.8-1-.52-.58-1.17-1-2.2-1-.95.05-1.69.38-2.2 1-.52.61-.78 1.28-.8 2 0 .52.09 1.52.67 2.67C1.25 8.82 3.01 10.61 6 13c2.98-2.39 4.77-4.17 5.34-5.33C11.91 6.51 12 5.5 12 5c-.02-.72-.28-1.39-.8-2.02V3z"></path></svg>
52+
{% endcapture %}
53+
{% capture github %}
54+
<svg height="20" class="octicon octicon-mark-github v-align-middle fill-gray mx-1" aria-label="GitHub" viewBox="0 0 16 16" version="1.1" width="20" role="img"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg>
55+
{% endcapture %}
5556

56-
{% assign byline = site.data.locale[site.locale].footer.byline.format %}
57-
{{ byline | replace: "[code]", code | replace: "[love]", love | replace: "[github]", github }}
57+
{% assign byline = site.data.locale[site.locale].footer.byline.format %}
58+
{{ byline | replace: "[code]", code | replace: "[love]", love | replace: "[github]", github }}
59+
</div>
60+
61+
<div class="text-small my-6">
62+
{% for translation in site.translations %}
63+
<a class="locale-chooser text-gray-light d-inline-block px-1"
64+
lang="{{ translation[0] }}"
65+
href="{{ page.url | prepend: translation[1].url }}?l={{ translation[0] }}">{{ translation[1].name }}</a>
66+
{% endfor %}
67+
</div>
5868
</div>
5969
</div>
6070
</footer>

_includes/head.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<link rel="icon" type="image/x-icon" href="https://assets-cdn.github.com/favicon.ico">
77
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
88
<script src="{{ "/js/script.js" | relative_url }}"></script>
9+
<script src="{{ "/js/locale.js" | relative_url }}"></script>
910
<script>
1011
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
1112
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

_layouts/landing.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html lang="{{ site.locale }}">
33
{% include head.html %}
44
<body class="">
55
<div class="">

js/locale.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
---
3+
4+
$(document).ready(function() {
5+
var currentLocale = $('html').attr('lang');
6+
7+
// All available translations
8+
var translations = $('a.locale-chooser');
9+
10+
// Get the preferred locale
11+
var locale = getLocaleFromQuery()
12+
|| getLocaleFromCookie()
13+
|| getLocaleFromBrowser(translations);
14+
15+
// If preferred locale is not the same as the current locale, then set it.
16+
if (locale && locale != currentLocale) {
17+
translations.filter("[lang=" + locale + "]").click();
18+
}
19+
20+
// Set locale when clicking on locale link
21+
translations.on('click', function(event) {
22+
setLocale($(event.target).attr('lang'));
23+
});
24+
});
25+
26+
// Save the preferred locale in a cookie, which will be set on any subdomain.
27+
function setLocale(locale) {
28+
document.cookie = 'locale=' + locale;
29+
}
30+
31+
// Get locale from the `l` parameter of the query string
32+
function getLocaleFromQuery() {
33+
return window.location.search.replace(/.*[?&]l=([^&$]+).*/, '$1');
34+
}
35+
36+
function getLocaleFromCookie() {
37+
return document.cookie.replace(/(?:(?:^|.*;\s*)locale\s*\=\s*([^;]*).*$)|^.*$/, '$1');
38+
}
39+
40+
// Use locale that matches browser's preferred locales
41+
function getLocaleFromBrowser(translations) {
42+
var browserLocales = [].concat(navigator.languages || navigator.userLanguage || navigator.language);
43+
for(var i = 0; i < browserLocales.length; i++) {
44+
if (translations.filter('[lang=' + browserLocales[i] + ']').length) {
45+
return browserLocales[i];
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)