Skip to content
Merged

I18n #27

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions App/Locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import I18n from './Locales/boot';

var Manager = function(object) {
this.key = object;
};

Manager.prototype.en = function(hash) {
if (I18n.translations['en'][this.key] && !process.env.TEST_UNIT) {
console.warn(`Locale (en) key already exists: ${this.key}`);
}
I18n.translations['en'][this.key] = hash;
return this;
};

Manager.prototype.t = function(scope, options) {
if (this.key) {
scope = this.key + '.' + scope; // prepend our key
}
return I18n.t(scope, options);
}

var Locale = {
key: function(object, enHash) {
var manager = new Manager(object);
// common case is passing en
if (enHash) {
return manager.en(enHash);
}
else {
return manager;
}
},

global: function() {
return new Manager(null);
},

lib: function() {
return I18n;
},

formatMoneyWithCountryIsoCode: function(cents, countryIsoCode, options = {}) {
const locale = I18n.getLocaleFromCountryIsoCode(countryIsoCode);
return Locale.lib().l('currency', cents, {locale, ...options});
},

formatMoneyWithCurrency: function(cents, currency, options = {}) {
const locale = I18n.getLocaleFromCurrency(currency);
return Locale.lib().l('currency', cents, {locale, ...options});
},
};

export default Locale;
5 changes: 5 additions & 0 deletions App/Locales/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// these do not get translated, mostly for values or something

export default {

};
42 changes: 42 additions & 0 deletions App/Locales/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import I18n from '../Locales/i18n';

I18n.translations['base'] = require('../Locales/base.js').default; // defaults, not translated - likely for set values
I18n.translations['en'] = require('../Locales/en.js').default; // shared for english countries, generally loaded through Locale.register

I18n.translations['en-US'] = require('../Locales/en-US.js').default; // known ones for US

I18n.translations['en-GB'] = require('../Locales/en-GB.js').default; // known ones for GB
I18n.translations['en-GB-xtra'] = require('../Locales/en-GB-xtra.js').default; // programmatic - ideally empty and in translated


I18n.default_locale = "en-US";
I18n.locale = "en-US";

I18n.fallbacks = {
'en-US': ['en', 'base'],
'en-GB': ['en-GB-xtra', 'en', 'base']
};

const countryIsoCodesToLocale = {
'US': 'en-US',
'GB': 'en-GB',
};

const currenciesToLocale = {
'USD': 'en-US',
'GBP': 'en-GB',
};

I18n.register = function(component, enHash) {
I18n.translations['en'][component] = enHash;
};

I18n.getLocaleFromCountryIsoCode = (countryIsoCode) => {
return countryIsoCodesToLocale[countryIsoCode] || countryIsoCodesToLocale['US'];
};

I18n.getLocaleFromCurrency = (countryIsoCode) => {
return currenciesToLocale[countryIsoCode] || currenciesToLocale['USD'];
};

export default I18n;
3 changes: 3 additions & 0 deletions App/Locales/en-GB-xtra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {

};
29 changes: 29 additions & 0 deletions App/Locales/en-GB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Known GB Translations - generally matches US ones

export default {
datetime: {
day_and_time: '%a, %b %-d, %-H:%M',
day_header: '%+A - %B %d',
short_day: '%a %b %-d',
short_time: '%-H:%M',
month_day: '%B %-d',
month_day_year: '%e %b %Y',
month_day_year_at_time: '%-d %B %Y at %-H:%M',
short_weekday_name: '%a',
long_day_of_month: '%d',
time_zone: '%Z',
},
number: {
currency: {
format: {
delimiter: ',',
format: '%u%n',
precision: 2,
separator: '.',
significant: false,
strip_insignificant_zeros: false,
unit: '£',
},
},
},
};
29 changes: 29 additions & 0 deletions App/Locales/en-US.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// All US Translations, more or less should match the GB one

export default {
datetime: {
day_and_time: "%a, %b %-d, %-I:%M %p",
day_header: "%+A - %B %d",
short_day: "%a %b %-d",
short_time: "%-I:%M %p",
month_day: "%B %-d",
month_day_year: "%b %e, %Y",
month_day_year_at_time: "%B %-d, %Y at %-I:%M %p",
short_weekday_name: "%a",
long_day_of_month: "%d",
time_zone: "%Z"
},
number: {
currency: {
format: {
delimiter: ',',
format: '%u%n',
precision: 2,
separator: '.',
significant: false,
strip_insignificant_zeros: false,
unit: '$',
},
},
},
};
5 changes: 5 additions & 0 deletions App/Locales/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// base translations that get translated

export default {

};
Loading