@boost/translate
Advanced tools
Comparing version 1.3.10 to 2.0.0-alpha.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.debug = void 0; | ||
const internal_1 = require("@boost/internal"); | ||
exports.debug = internal_1.createInternalDebugger('translate'); |
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -19,6 +9,6 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const common_1 = require("@boost/common"); | ||
const internal_1 = require("@boost/internal"); | ||
const TranslateError_1 = __importDefault(require("./TranslateError")); | ||
const LocaleDetector_1 = __importDefault(require("./LocaleDetector")); | ||
const FileBackend_1 = __importDefault(require("./FileBackend")); | ||
const constants_1 = require("./constants"); | ||
const debug_1 = __importDefault(require("./debug")); | ||
// istanbul ignore next | ||
@@ -30,15 +20,15 @@ function handleError(error) { | ||
} | ||
function createTranslator(namespace, resourcePath, { autoDetect = true, debug: debugOpt = false, fallbackLocale = 'en', locale, lookupType, resourceFormat = 'yaml', } = {}) { | ||
function createTranslator(namespace, resourcePath, { autoDetect = true, debug: debugOpt = false, fallbackLocale = 'en', locale, lookupType = 'all', resourceFormat = 'yaml', } = {}) { | ||
const namespaces = common_1.toArray(namespace); | ||
const resourcePaths = common_1.toArray(resourcePath).map(common_1.Path.create); | ||
if (namespaces.length === 0) { | ||
throw new internal_1.RuntimeError('translate', 'TL_REQ_NAMESPACE'); | ||
throw new TranslateError_1.default('NAMESPACE_REQUIRED'); | ||
} | ||
else if (resourcePaths.length === 0) { | ||
throw new internal_1.RuntimeError('translate', 'TL_REQ_RES_PATHS'); | ||
throw new TranslateError_1.default('RESOURCES_REQUIRED'); | ||
} | ||
else if (!autoDetect && !locale) { | ||
throw new internal_1.RuntimeError('translate', 'TL_REQ_MANUAL_LOCALE'); | ||
throw new TranslateError_1.default('LOCALE_REQUIRED'); | ||
} | ||
constants_1.debug('New translator created: %s namespace(s)', namespaces.join(', ')); | ||
debug_1.default('New translator created: %s namespace(s)', namespaces.join(', ')); | ||
const translator = i18next_1.default.createInstance().use(new FileBackend_1.default()); | ||
@@ -53,2 +43,3 @@ if (autoDetect) { | ||
}, | ||
cleanCode: true, | ||
debug: debugOpt, | ||
@@ -60,19 +51,22 @@ defaultNS: namespaces[0], | ||
load: lookupType, | ||
lowerCaseLng: true, | ||
lowerCaseLng: false, | ||
ns: namespaces, | ||
returnNull: false, | ||
returnEmptyString: true, | ||
returnNull: true, | ||
}, handleError); | ||
function msg(key, params, _a = {}) { | ||
var { interpolation, locale: lng } = _a, options = __rest(_a, ["interpolation", "locale"]); | ||
return translator.t(key, Object.assign(Object.assign({ interpolation: Object.assign({ escapeValue: false }, interpolation) }, options), { lng, replace: params })); | ||
function msg(key, params, { interpolation, locale: lng, ...options } = {}) { | ||
return translator.t(key, { | ||
interpolation: { escapeValue: false, ...interpolation }, | ||
...options, | ||
lng, | ||
replace: params, | ||
}); | ||
} | ||
msg.dir = translator.dir(); | ||
msg.direction = translator.dir(); | ||
msg.locale = translator.language; | ||
msg.changeLocale = (lang) => { | ||
constants_1.debug('Locale manually changed to "%s"', lang); | ||
translator.changeLanguage(lang, error => { | ||
handleError(error); | ||
msg.dir = translator.dir(); | ||
msg.locale = translator.language; | ||
}); | ||
msg.changeLocale = async (lang) => { | ||
debug_1.default('Locale manually changed to "%s"', lang); | ||
await translator.changeLanguage(lang); | ||
msg.direction = translator.dir(); | ||
msg.locale = translator.language; | ||
}; | ||
@@ -79,0 +73,0 @@ if (process.env.NODE_ENV === 'test') { |
import { BackendModule, Resource, ResourceKey } from 'i18next'; | ||
import { Contract, Path, Predicates } from '@boost/common'; | ||
import { Contract, Path, Predicates, Blueprint } from '@boost/common'; | ||
import { Locale, Format } from './types'; | ||
@@ -12,6 +12,3 @@ export interface FileBackendOptions { | ||
init(services: unknown, options: Partial<FileBackendOptions>): void; | ||
blueprint({ array, instance, string }: Predicates): { | ||
format: import("optimal").StringPredicate<Format>; | ||
paths: import("optimal").ArrayPredicate<Path>; | ||
}; | ||
blueprint({ array, instance, string }: Predicates): Blueprint<FileBackendOptions>; | ||
create(): void; | ||
@@ -18,0 +15,0 @@ read(locale: Locale, namespace: string, callback: (error: Error | null, resources: Resource) => void): ResourceKey; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const common_1 = require("@boost/common"); | ||
const internal_1 = require("@boost/internal"); | ||
const TranslateError_1 = __importDefault(require("./TranslateError")); | ||
const EXTS = { | ||
@@ -19,5 +22,5 @@ js: ['js'], | ||
// Validate resource paths are directories | ||
this.options.paths.forEach(path => { | ||
this.options.paths.forEach((path) => { | ||
if (path.exists() && !path.isDirectory()) { | ||
throw new internal_1.RuntimeError('translate', 'TL_INVALID_RES_PATH', [path.path()]); | ||
throw new TranslateError_1.default('RESOURCE_PATH_INVALID', [path.path()]); | ||
} | ||
@@ -39,4 +42,4 @@ }); | ||
const resources = {}; | ||
paths.forEach(path => { | ||
EXTS[format].some(ext => { | ||
paths.forEach((path) => { | ||
EXTS[format].some((ext) => { | ||
const resPath = path.append(locale, `${namespace}.${ext}`); | ||
@@ -43,0 +46,0 @@ const isCached = this.fileCache.has(resPath); |
/** | ||
* @copyright 2019, Miles Johnson | ||
* @copyright 2020, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import createTranslator, { TranslatorOptions } from './createTranslator'; | ||
import TranslateError from './TranslateError'; | ||
export * from './types'; | ||
export { createTranslator, TranslatorOptions }; | ||
export { createTranslator, TranslatorOptions, TranslateError }; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
/** | ||
* @copyright 2019, Miles Johnson | ||
* @copyright 2020, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -10,3 +20,7 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TranslateError = exports.createTranslator = void 0; | ||
const createTranslator_1 = __importDefault(require("./createTranslator")); | ||
exports.createTranslator = createTranslator_1.default; | ||
const TranslateError_1 = __importDefault(require("./TranslateError")); | ||
exports.TranslateError = TranslateError_1.default; | ||
__exportStar(require("./types"), exports); |
@@ -7,3 +7,3 @@ "use strict"; | ||
const os_locale_1 = __importDefault(require("os-locale")); | ||
const constants_1 = require("./constants"); | ||
const debug_1 = __importDefault(require("./debug")); | ||
class LocaleDetector { | ||
@@ -22,3 +22,3 @@ constructor() { | ||
if (this.locale) { | ||
constants_1.debug('Locale "%s" manually provided', this.locale); | ||
debug_1.default('Locale "%s" manually provided', this.locale); | ||
return this.locale; | ||
@@ -30,7 +30,7 @@ } | ||
const args = process.argv; | ||
const index = args.findIndex(arg => arg === '--locale'); | ||
const index = args.findIndex((arg) => arg === '--locale'); | ||
const nextIndex = index + 1; | ||
if (index >= 0 && args[nextIndex] && !args[nextIndex].startsWith('-')) { | ||
const locale = args[nextIndex]; | ||
constants_1.debug('Locale "%s" detected from --locale option', locale); | ||
debug_1.default('Locale "%s" detected from --locale option', locale); | ||
return locale; | ||
@@ -42,3 +42,3 @@ } | ||
const locale = os_locale_1.default.sync().replace(/_/gu, '-'); | ||
constants_1.debug('Locale "%s" detected from operating system', locale); | ||
debug_1.default('Locale "%s" detected from operating system', locale); | ||
return locale; | ||
@@ -45,0 +45,0 @@ } |
@@ -23,8 +23,8 @@ import { i18n, InterpolationOptions } from 'i18next'; | ||
export interface Translator { | ||
dir: Direction; | ||
direction: Direction; | ||
locale: Locale; | ||
changeLocale: (locale: Locale) => Promise<void>; | ||
(key: string | string[], params?: InterpolationParams, options?: MessageOptions): string; | ||
changeLocale(locale: Locale): void; | ||
i18n: i18n; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "@boost/translate", | ||
"version": "1.3.10", | ||
"version": "2.0.0-alpha.1", | ||
"release": "1592788092197", | ||
"description": "Package and application level translations made easy.", | ||
@@ -15,5 +16,4 @@ "keywords": [ | ||
"types": "./lib/index.d.ts", | ||
"nonce": "1579986563745", | ||
"engines": { | ||
"node": ">=8.9.0" | ||
"node": ">=10.17.0" | ||
}, | ||
@@ -26,6 +26,6 @@ "repository": "https://github.com/milesj/boost/tree/master/packages/log", | ||
"dependencies": { | ||
"@boost/common": "^1.9.0", | ||
"@boost/internal": "^1.2.0", | ||
"i18next": "^19.4.2", | ||
"os-locale": "^4.0.0" | ||
"@boost/common": "^2.0.0-alpha.1", | ||
"@boost/internal": "^2.0.0-alpha.1", | ||
"i18next": "^19.5.0", | ||
"os-locale": "^5.0.0" | ||
}, | ||
@@ -36,3 +36,3 @@ "funding": { | ||
}, | ||
"gitHead": "257f5a0a342a014f3ce9cf8c288879a73b36ef76" | ||
"gitHead": "3fffbcf3231b86592d2083902b8c39a2ed1b2e4c" | ||
} |
@@ -1,2 +0,2 @@ | ||
# Boost Translate | ||
# Translate - Boost | ||
@@ -3,0 +3,0 @@ [![Build Status](https://github.com/milesj/boost/workflows/Build/badge.svg)](https://github.com/milesj/boost/actions?query=branch%3Amaster) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
24965
28
317
2
+ Added@boost/common@2.8.2(transitive)
+ Added@boost/decorators@2.1.4(transitive)
+ Added@boost/internal@2.2.3(transitive)
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedcross-spawn@7.0.6(transitive)
+ Addedexeca@4.1.0(transitive)
+ Addedfast-glob@3.3.2(transitive)
+ Addedfastq@1.17.1(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedget-stream@5.2.0(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedhuman-signals@1.1.1(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedmerge-stream@2.0.0(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addednpm-run-path@4.0.1(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedos-locale@5.0.0(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedpretty-ms@7.0.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedstrip-final-newline@2.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedwhich@2.0.2(transitive)
- Removed@boost/common@1.9.0(transitive)
- Removed@boost/internal@1.2.0(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedchalk@3.0.0(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedcross-spawn@6.0.6(transitive)
- Removedexeca@1.0.0(transitive)
- Removedget-stream@4.1.0(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removednice-try@1.0.5(transitive)
- Removednpm-run-path@2.0.2(transitive)
- Removedos-locale@4.0.0(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedpath-key@2.0.1(transitive)
- Removedpretty-ms@5.1.0(transitive)
- Removedsemver@5.7.2(transitive)
- Removedshebang-command@1.2.0(transitive)
- Removedshebang-regex@1.0.0(transitive)
- Removedstrip-eof@1.0.0(transitive)
- Removedsupports-color@7.2.0(transitive)
- Removedwhich@1.3.1(transitive)
Updated@boost/common@^2.0.0-alpha.1
Updatedi18next@^19.5.0
Updatedos-locale@^5.0.0