@teamleader/i18n
Advanced tools
Comparing version 1.0.2 to 2.0.2
109
lib/index.js
@@ -18,3 +18,3 @@ "use strict"; | ||
var _supportedLanguages = _interopRequireDefault(require("./supportedLanguages.json")); | ||
var _supportedLocales = _interopRequireDefault(require("./supportedLocales.json")); | ||
@@ -27,10 +27,4 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } | ||
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } | ||
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | ||
@@ -70,3 +64,3 @@ | ||
exports.Translation = Translation; | ||
var FALLBACK_LANGUAGE = 'en'; | ||
var FALLBACK_LOCALE = 'en-GB'; | ||
@@ -141,3 +135,3 @@ var withI18n = function withI18n(Component) { | ||
var namespace, language, localeData, translations, component; | ||
var locale, localeData, translations, component; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
@@ -147,10 +141,9 @@ while (1) { | ||
case 0: | ||
namespace = this.props.namespace; | ||
language = this.getUserLanguage(); | ||
locale = this.getUserLocale(); | ||
localeData = this.getAllLocaleData(); | ||
(0, _reactIntl.addLocaleData)(localeData); | ||
_context.next = 6; | ||
return this.fetchTranslations(language); | ||
_context.next = 5; | ||
return this.fetchTranslations(locale); | ||
case 6: | ||
case 5: | ||
translations = _context.sent; | ||
@@ -160,3 +153,3 @@ component = React.createElement(_reactIntl.IntlProvider, { | ||
children: React.createElement('div'), | ||
locale: language, | ||
locale: locale, | ||
ref: function ref(element) { | ||
@@ -228,3 +221,3 @@ if (!element) { | ||
case 9: | ||
case 8: | ||
case "end": | ||
@@ -242,11 +235,15 @@ return _context.stop(); | ||
}, { | ||
key: "getUserLanguage", | ||
value: function getUserLanguage() { | ||
var language = this.props.language || document.documentElement && document.documentElement.getAttribute('lang'); | ||
key: "getUserLocale", | ||
value: function getUserLocale() { | ||
var locale = this.props.locale || document.documentElement && document.documentElement.getAttribute('lang'); | ||
if (!locale) return FALLBACK_LOCALE; // we support both the language code and the language + locale. For example | ||
// both 'en' and 'en-GB' are valid though 'en' is considered 'en-US'. | ||
if (!language || !_supportedLanguages.default.includes(language)) { | ||
return FALLBACK_LANGUAGE; | ||
var supportedLanguages = _supportedLocales.default.map(this.localeToLanguage); | ||
if (_supportedLocales.default.includes(locale) || supportedLanguages.includes(locale)) { | ||
return locale; | ||
} | ||
return language; | ||
return FALLBACK_LOCALE; | ||
} | ||
@@ -258,4 +255,4 @@ }, { | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee2(language) { | ||
var path, response, json; | ||
regeneratorRuntime.mark(function _callee2(locale) { | ||
var path, response; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
@@ -265,3 +262,3 @@ while (1) { | ||
case 0: | ||
path = this.getTranslationsPath(language); | ||
path = this.getTranslationsPath(locale); | ||
_context2.prev = 1; | ||
@@ -277,13 +274,12 @@ _context2.next = 4; | ||
case 7: | ||
json = _context2.sent; | ||
return _context2.abrupt("return", json); | ||
return _context2.abrupt("return", _context2.sent); | ||
case 11: | ||
_context2.prev = 11; | ||
case 10: | ||
_context2.prev = 10; | ||
_context2.t0 = _context2["catch"](1); | ||
case 13: | ||
case 12: | ||
return _context2.abrupt("return", {}); | ||
case 14: | ||
case 13: | ||
case "end": | ||
@@ -293,3 +289,3 @@ return _context2.stop(); | ||
} | ||
}, _callee2, this, [[1, 11]]); | ||
}, _callee2, this, [[1, 10]]); | ||
})); | ||
@@ -303,12 +299,18 @@ | ||
key: "getTranslationsPath", | ||
value: function getTranslationsPath(language) { | ||
value: function getTranslationsPath(locale) { | ||
var path = this.props.path; | ||
if (typeof path === 'function') { | ||
return path(language); | ||
return path(locale); | ||
} | ||
return "".concat(path).concat(language, ".json"); | ||
return "".concat(path).concat(locale, ".json"); | ||
} | ||
}, { | ||
key: "localeToLanguage", | ||
value: function localeToLanguage(locale) { | ||
if (locale.match(/^tlh-/)) return 'en'; | ||
return locale.split('-')[0]; | ||
} | ||
}, { | ||
key: "getAllLocaleData", | ||
@@ -318,28 +320,17 @@ value: function getAllLocaleData() { | ||
return _supportedLanguages.default.reduce(function (currentLocaleData, language) { | ||
var languageCode = language.split('-')[0]; | ||
var localeData = _this3.getLocaleDataForLanguage(languageCode); | ||
return _toConsumableArray(currentLocaleData).concat(_toConsumableArray(localeData)); | ||
var languages = Object.keys(_supportedLocales.default.reduce(function (languages, locale) { | ||
return _objectSpread({}, languages, _defineProperty({}, _this3.localeToLanguage(locale), true)); | ||
}, {})); | ||
var localeData = languages.map(function (language) { | ||
return require("react-intl/locale-data/".concat(language)); | ||
}); | ||
localeData.push({ | ||
locale: 'tlh', | ||
parentLocale: 'en' | ||
}); | ||
return localeData.reduce(function (allLocaleData, localeData) { | ||
return allLocaleData.concat(localeData); | ||
}, []); | ||
} | ||
}, { | ||
key: "getLocaleDataForLanguage", | ||
value: function getLocaleDataForLanguage(languageCode) { | ||
// tlh doesn't exist, so we replace it with english (which we want anyway) | ||
var languageToLoad = languageCode === 'tlh' ? 'en' : languageCode; | ||
var localeData = require("react-intl/locale-data/".concat(languageToLoad)); | ||
if (languageCode === 'tlh') { | ||
localeData.push({ | ||
locale: 'tlh', | ||
parentLocale: 'en' | ||
}); | ||
} | ||
return localeData; | ||
} | ||
}, { | ||
key: "render", | ||
@@ -346,0 +337,0 @@ value: function render() { |
{ | ||
"name": "@teamleader/i18n", | ||
"version": "1.0.2", | ||
"version": "2.0.2", | ||
"description": "Teamleader i18n implementation", | ||
@@ -5,0 +5,0 @@ "author": "Teamleader <development@teamleader.eu>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
295
31867
13
2