Skip to content

Commit 3c497ac

Browse files
authored
Merge pull request taskrabbit#27 from taskrabbit/i18n
I18n
2 parents 3b9cc0d + 3c85317 commit 3c497ac

File tree

16 files changed

+1136
-22
lines changed

16 files changed

+1136
-22
lines changed

App/Locale.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import I18n from './Locales/boot';
2+
3+
var Manager = function(object) {
4+
this.key = object;
5+
};
6+
7+
Manager.prototype.en = function(hash) {
8+
if (I18n.translations['en'][this.key] && !process.env.TEST_UNIT) {
9+
console.warn(`Locale (en) key already exists: ${this.key}`);
10+
}
11+
I18n.translations['en'][this.key] = hash;
12+
return this;
13+
};
14+
15+
Manager.prototype.t = function(scope, options) {
16+
if (this.key) {
17+
scope = this.key + '.' + scope; // prepend our key
18+
}
19+
return I18n.t(scope, options);
20+
}
21+
22+
var Locale = {
23+
key: function(object, enHash) {
24+
var manager = new Manager(object);
25+
// common case is passing en
26+
if (enHash) {
27+
return manager.en(enHash);
28+
}
29+
else {
30+
return manager;
31+
}
32+
},
33+
34+
global: function() {
35+
return new Manager(null);
36+
},
37+
38+
lib: function() {
39+
return I18n;
40+
},
41+
42+
formatMoneyWithCountryIsoCode: function(cents, countryIsoCode, options = {}) {
43+
const locale = I18n.getLocaleFromCountryIsoCode(countryIsoCode);
44+
return Locale.lib().l('currency', cents, {locale, ...options});
45+
},
46+
47+
formatMoneyWithCurrency: function(cents, currency, options = {}) {
48+
const locale = I18n.getLocaleFromCurrency(currency);
49+
return Locale.lib().l('currency', cents, {locale, ...options});
50+
},
51+
};
52+
53+
export default Locale;

App/Locales/base.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// these do not get translated, mostly for values or something
2+
3+
export default {
4+
5+
};

App/Locales/boot.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import I18n from '../Locales/i18n';
2+
3+
I18n.translations['base'] = require('../Locales/base.js').default; // defaults, not translated - likely for set values
4+
I18n.translations['en'] = require('../Locales/en.js').default; // shared for english countries, generally loaded through Locale.register
5+
6+
I18n.translations['en-US'] = require('../Locales/en-US.js').default; // known ones for US
7+
8+
I18n.translations['en-GB'] = require('../Locales/en-GB.js').default; // known ones for GB
9+
I18n.translations['en-GB-xtra'] = require('../Locales/en-GB-xtra.js').default; // programmatic - ideally empty and in translated
10+
11+
12+
I18n.default_locale = "en-US";
13+
I18n.locale = "en-US";
14+
15+
I18n.fallbacks = {
16+
'en-US': ['en', 'base'],
17+
'en-GB': ['en-GB-xtra', 'en', 'base']
18+
};
19+
20+
const countryIsoCodesToLocale = {
21+
'US': 'en-US',
22+
'GB': 'en-GB',
23+
};
24+
25+
const currenciesToLocale = {
26+
'USD': 'en-US',
27+
'GBP': 'en-GB',
28+
};
29+
30+
I18n.register = function(component, enHash) {
31+
I18n.translations['en'][component] = enHash;
32+
};
33+
34+
I18n.getLocaleFromCountryIsoCode = (countryIsoCode) => {
35+
return countryIsoCodesToLocale[countryIsoCode] || countryIsoCodesToLocale['US'];
36+
};
37+
38+
I18n.getLocaleFromCurrency = (countryIsoCode) => {
39+
return currenciesToLocale[countryIsoCode] || currenciesToLocale['USD'];
40+
};
41+
42+
export default I18n;

App/Locales/en-GB-xtra.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
3+
};

App/Locales/en-GB.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Known GB Translations - generally matches US ones
2+
3+
export default {
4+
datetime: {
5+
day_and_time: '%a, %b %-d, %-H:%M',
6+
day_header: '%+A - %B %d',
7+
short_day: '%a %b %-d',
8+
short_time: '%-H:%M',
9+
month_day: '%B %-d',
10+
month_day_year: '%e %b %Y',
11+
month_day_year_at_time: '%-d %B %Y at %-H:%M',
12+
short_weekday_name: '%a',
13+
long_day_of_month: '%d',
14+
time_zone: '%Z',
15+
},
16+
number: {
17+
currency: {
18+
format: {
19+
delimiter: ',',
20+
format: '%u%n',
21+
precision: 2,
22+
separator: '.',
23+
significant: false,
24+
strip_insignificant_zeros: false,
25+
unit: '£',
26+
},
27+
},
28+
},
29+
};

App/Locales/en-US.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// All US Translations, more or less should match the GB one
2+
3+
export default {
4+
datetime: {
5+
day_and_time: "%a, %b %-d, %-I:%M %p",
6+
day_header: "%+A - %B %d",
7+
short_day: "%a %b %-d",
8+
short_time: "%-I:%M %p",
9+
month_day: "%B %-d",
10+
month_day_year: "%b %e, %Y",
11+
month_day_year_at_time: "%B %-d, %Y at %-I:%M %p",
12+
short_weekday_name: "%a",
13+
long_day_of_month: "%d",
14+
time_zone: "%Z"
15+
},
16+
number: {
17+
currency: {
18+
format: {
19+
delimiter: ',',
20+
format: '%u%n',
21+
precision: 2,
22+
separator: '.',
23+
significant: false,
24+
strip_insignificant_zeros: false,
25+
unit: '$',
26+
},
27+
},
28+
},
29+
};

App/Locales/en.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// base translations that get translated
2+
3+
export default {
4+
5+
};

0 commit comments

Comments
 (0)