Skip to content

Commit 50432e5

Browse files
committed
Implement locale switching
1 parent a6347aa commit 50432e5

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ <h3 class="alt-h3 mb-3">{{ site.data.locale[site.locale].footer.subscribe.headin
6060

6161
<div class="text-small my-6">
6262
{% for translation in site.translations %}
63-
<a class="locale-chooser text-gray-light d-inline-block px-1" data-locale="{{ translation.locale }}" href="{{ translation.url }}?l={{ translation.locale }}">{{ translation.name }}</a>
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>
6466
{% endfor %}
6567
</div>
6668
</div>

_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)