@boost/translate
Advanced tools
Comparing version 2.2.4 to 2.2.5
@@ -6,2 +6,15 @@ # Change Log | ||
### 2.2.5 - 2021-03-26 | ||
#### 📦 Dependencies | ||
- **[i18next]** Update to v20.0. ([fdfdd18](https://github.com/milesj/boost/commit/fdfdd18)) | ||
- **[packemon]** Update to v0.14. (#145) ([9897e2a](https://github.com/milesj/boost/commit/9897e2a)), closes [#145](https://github.com/milesj/boost/issues/145) | ||
**Note:** Version bump only for package @boost/translate | ||
### 2.2.4 - 2021-02-21 | ||
@@ -8,0 +21,0 @@ |
226
lib/index.js
@@ -9,226 +9,8 @@ // Generated with Packemon: https://packemon.dev | ||
var i18next = require('i18next'); | ||
var createTranslator = require('./createTranslator.js'); | ||
var common = require('@boost/common'); | ||
var TranslateError = require('./TranslateError.js'); | ||
var internal = require('@boost/internal'); | ||
var osLocale = require('os-locale'); | ||
function _interopDefaultLegacy(e) { | ||
return e && typeof e === 'object' && 'default' in e ? e : { | ||
'default': e | ||
}; | ||
} | ||
var i18next__default = /*#__PURE__*/_interopDefaultLegacy(i18next); | ||
var osLocale__default = /*#__PURE__*/_interopDefaultLegacy(osLocale); | ||
var debug = internal.createInternalDebugger('translate'); | ||
const errors = { | ||
LOCALE_REQUIRED: 'A locale must be defined if auto-detection is disabled.', | ||
NAMESPACE_REQUIRED: 'A namespace is required for translations.', | ||
RESOURCE_PATH_INVALID: 'Resource path "{0}" must be a directory.', | ||
RESOURCES_REQUIRED: 'At least 1 resource directory path is required.' | ||
}; | ||
var TranslateError = internal.createScopedError('TLT', 'TranslateError', errors); | ||
const EXTS = { | ||
js: ['js'], | ||
json: ['json', 'json5'], | ||
yaml: ['yaml', 'yml'] | ||
}; | ||
class FileBackend extends common.Contract { | ||
constructor(...args) { | ||
super(...args); | ||
this.fileCache = new Map(); | ||
this.type = 'backend'; | ||
} | ||
init(services, options) { | ||
this.configure(options); // Validate resource paths are directories | ||
this.options.paths.forEach(path => { | ||
if (path.exists() && !path.isDirectory()) { | ||
throw new TranslateError('RESOURCE_PATH_INVALID', [path.path()]); | ||
} | ||
}); | ||
} | ||
blueprint({ | ||
array, | ||
instance, | ||
string | ||
}) { | ||
return { | ||
format: string('yaml').oneOf(['js', 'json', 'yaml']), | ||
paths: array(instance(common.Path, true).notNullable()) | ||
}; | ||
} // istanbul ignore next | ||
create() {// We don't need this but is required by the interface | ||
} | ||
read(locale, namespace, callback) { | ||
const { | ||
format, | ||
paths | ||
} = this.options; | ||
const resources = {}; | ||
paths.forEach(path => { | ||
EXTS[format].some(ext => { | ||
const resPath = path.append(locale, `${namespace}.${ext}`); | ||
const isCached = this.fileCache.has(resPath); | ||
if (!resPath.exists()) { | ||
return false; | ||
} | ||
if (!isCached) { | ||
this.fileCache.set(resPath, common.parseFile(resPath)); | ||
} | ||
Object.assign(resources, this.fileCache.get(resPath)); | ||
return true; | ||
}); | ||
}); | ||
callback(null, resources); | ||
return resources; | ||
} | ||
} | ||
class LocaleDetector { | ||
constructor() { | ||
this.locale = 'en'; | ||
this.type = 'languageDetector'; | ||
} | ||
init() {// We don't need this but is required by the interface | ||
} | ||
cacheUserLanguage(locale) { | ||
this.locale = locale; | ||
} | ||
detect() { | ||
if (this.locale) { | ||
debug('Locale "%s" manually provided', this.locale); | ||
return this.locale; | ||
} | ||
return this.detectFromArgv() || this.detectFromOS(); | ||
} | ||
detectFromArgv() { | ||
const args = process.argv; | ||
const index = args.findIndex(arg => arg === '--locale'); | ||
const nextIndex = index + 1; | ||
if (index >= 0 && args[nextIndex] && !args[nextIndex].startsWith('-')) { | ||
const locale = args[nextIndex]; | ||
debug('Locale "%s" detected from --locale option', locale); | ||
return locale; | ||
} | ||
return undefined; | ||
} | ||
detectFromOS() { | ||
const locale = osLocale__default['default'].sync().replace(/_/gu, '-'); | ||
debug('Locale "%s" detected from operating system', locale); | ||
return locale; | ||
} | ||
} | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
// istanbul ignore next | ||
function handleError(error) { | ||
if (error) { | ||
throw error; | ||
} | ||
} | ||
function createTranslator(namespace, resourcePath, { | ||
autoDetect = true, | ||
debug: debugOpt = false, | ||
fallbackLocale = 'en', | ||
locale, | ||
lookupType = 'all', | ||
resourceFormat = 'yaml' | ||
} = {}) { | ||
const namespaces = common.toArray(namespace); | ||
const resourcePaths = common.toArray(resourcePath).map(common.Path.create); | ||
if (namespaces.length === 0) { | ||
throw new TranslateError('NAMESPACE_REQUIRED'); | ||
} else if (resourcePaths.length === 0) { | ||
throw new TranslateError('RESOURCES_REQUIRED'); | ||
} else if (!autoDetect && !locale) { | ||
throw new TranslateError('LOCALE_REQUIRED'); | ||
} | ||
debug('New translator created: %s namespace(s)', namespaces.join(', ')); | ||
const translator = i18next__default['default'].createInstance().use(new FileBackend()); | ||
if (autoDetect) { | ||
translator.use(new LocaleDetector()); | ||
} | ||
translator.init({ | ||
backend: { | ||
format: resourceFormat, | ||
paths: resourcePaths | ||
}, | ||
cleanCode: true, | ||
debug: debugOpt, | ||
defaultNS: namespaces[0], | ||
fallbackLng: fallbackLocale, | ||
initImmediate: false, | ||
lng: locale, | ||
load: lookupType, | ||
lowerCaseLng: false, | ||
ns: namespaces, | ||
returnEmptyString: true, | ||
returnNull: true | ||
}, handleError); | ||
function msg(key, params, { | ||
interpolation, | ||
locale: lng, | ||
...options | ||
} = {}) { | ||
return translator.t(key, { | ||
interpolation: { | ||
escapeValue: false, | ||
...interpolation | ||
}, | ||
...options, | ||
lng, | ||
replace: params | ||
}); | ||
} | ||
msg.direction = translator.dir(); | ||
msg.locale = translator.language; | ||
msg.changeLocale = async lang => { | ||
debug('Locale manually changed to "%s"', lang); | ||
await translator.changeLanguage(lang); | ||
msg.direction = translator.dir(); | ||
msg.locale = translator.language; | ||
}; | ||
if (process.env.NODE_ENV === 'test') { | ||
msg.i18n = translator; | ||
} | ||
return msg; | ||
} | ||
exports.createTranslator = createTranslator; | ||
exports.TranslateError = TranslateError; | ||
exports.createTranslator = createTranslator; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@boost/translate", | ||
"version": "2.2.4", | ||
"version": "2.2.5", | ||
"release": "1594765247526", | ||
@@ -16,2 +16,7 @@ "description": "Package and application level translations made easy.", | ||
"types": "./dts/index.d.ts", | ||
"files": [ | ||
"dts/**/*.d.ts", | ||
"lib/**/*.{js,map}", | ||
"src/**/*.{ts,tsx,json}" | ||
], | ||
"engines": { | ||
@@ -32,5 +37,5 @@ "node": ">=10.3.0", | ||
"dependencies": { | ||
"@boost/common": "^2.7.1", | ||
"@boost/internal": "^2.2.1", | ||
"i18next": "^19.8.4", | ||
"@boost/common": "^2.7.2", | ||
"@boost/internal": "^2.2.2", | ||
"i18next": "^20.1.0", | ||
"os-locale": "^5.0.0" | ||
@@ -45,3 +50,3 @@ }, | ||
}, | ||
"gitHead": "85109c6c9f196f4374e13519cb52f88dc4860310" | ||
"gitHead": "f9eb83de54fa41334f1a1e487216004a03caf662" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40368
30
575
1
+ Addedi18next@20.6.1(transitive)
- Removedi18next@19.9.2(transitive)
Updated@boost/common@^2.7.2
Updated@boost/internal@^2.2.2
Updatedi18next@^20.1.0