Skip to content

Commit ea0e226

Browse files
committed
Define supported locales instead of just languages
1 parent 2a4c0e9 commit ea0e226

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

TRANSLATION.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@ At runtime, the proper translation will be picked based on the browser language.
2121

2222
You need to add the new language in 4 places:
2323

24-
1. Create `lib/challenges/locales/<lang>` and copy the whole `worlds` directory and `index.json`
24+
1. Create `lib/challenges/locales/<locale>` and copy the whole `worlds` directory and `index.json`
2525
These are the translations of the challenges, the main content of Make-Art.
2626

27-
2. Create `locales/<lang>` and copy the content of `locales/en`
27+
2. Create `locales/<locale>` and copy the content of `locales/en`
2828
These are the translations of the views
2929

3030
3. Directly edit `content/docs.json` and add your language to the map (at the top level)
3131

32-
4. Add your language to `SUPPORTED_LANGUAGES` array in `lib/i18n.js`
33-
34-
Note that languages are identified by their 2 letter abbreviation (ignoring the country), so for example fr-FR and fr-CA will share the same translation `fr`.
32+
4. Add your language to `SUPPORTED_LOCALES` array in `lib/i18n.js`
3533

3634
## How to make sure your code is i18n-aware
3735

lib/i18n.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
var url = require('url');
22

3-
var SUPPORTED_LANGUAGES = [
3+
var SUPPORTED_LOCALES = [
44
'ja',
55
'en',
6-
'es',
6+
'es-AR',
77
];
88

99
function getLanguage() {
1010
var parsedUrl = url.parse(window.location.href, true);
1111

1212
if (parsedUrl.query['lang'] !== undefined) {
13-
var lang = parsedUrl.query['lang'].toLowerCase();
14-
if (SUPPORTED_LANGUAGES.indexOf(lang) > -1) {
13+
var lang = parsedUrl.query['lang'];
14+
if (SUPPORTED_LOCALES.indexOf(lang) > -1) {
1515
return lang;
1616
}
1717
// not in supported languages so fall back to normal detection
@@ -20,14 +20,21 @@ function getLanguage() {
2020
var language = window.navigator.languages ? window.navigator.languages[0] :
2121
window.navigator.userLanguage || window.navigator.language || 'en',
2222
p = language.indexOf('-');
23-
language = (p > -1) ? language.substr(0, p) : language;
23+
24+
// check if full locale is supported
25+
if (SUPPORTED_LOCALES.indexOf(language) > -1) {
26+
return language;
27+
}
28+
2429
language = language.toLowerCase();
2530

26-
if (SUPPORTED_LANGUAGES.indexOf(language) === -1) {
27-
// language is not supported so default to 'en'
28-
return 'en';
31+
// otherwise check that if just language is supported
32+
language = (p > -1) ? language.substr(0, p) : language;
33+
if (SUPPORTED_LOCALES.indexOf(language) > -1) {
34+
return language;
2935
}
30-
return language;
36+
// language is not supported so default to 'en'
37+
return 'en';
3138
}
3239

3340
function getChallengeLocalePath () {

test/unit/i18n-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("i18n", function() {
3131
global.window.navigator.language = 'es-AR';
3232

3333
var challengePath = i18n.getChallengeLocalePath();
34-
assert.equal(challengePath, '/locales/es');
34+
assert.equal(challengePath, '/locales/es-AR');
3535
});
3636

3737
it("should return correct html locale path", function() {
@@ -43,7 +43,7 @@ describe("i18n", function() {
4343
global.window.navigator.language = 'es-AR';
4444

4545
var challengePath = i18n.getHtmlLocalePath();
46-
assert.equal(challengePath, '/locales/es');
46+
assert.equal(challengePath, '/locales/es-AR');
4747
});
4848

4949
it("should allow language override from url param", function() {

0 commit comments

Comments
 (0)