@spinajs/intl
Advanced tools
Comparing version 1.0.1 to 1.2.103
@@ -1,6 +0,17 @@ | ||
module.exports.intl = { | ||
defaultLocale: 'pl', | ||
locales: ['en', 'pl'], | ||
queryParameter: 'lang', | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Localisation configuration, structure like in | ||
* https://github.com/mashpie/i18n-node | ||
*/ | ||
const config = { | ||
validation: { | ||
defaultLocale: 'en', | ||
// supported locales | ||
locales: ['en'], | ||
// query parameter to switch locale (ie. /home?lang=ch) - defaults to NULL | ||
queryParameter: 'lang', | ||
}, | ||
}; | ||
exports.default = config; | ||
//# sourceMappingURL=locales.js.map |
@@ -1,4 +0,4 @@ | ||
import { IContainer, SyncModule } from "@spinajs/di"; | ||
import { SyncModule } from '@spinajs/di'; | ||
import { Configuration } from '@spinajs/configuration'; | ||
import { Log } from "@spinajs/log"; | ||
import { Log } from '@spinajs/log'; | ||
export interface IPhraseWithOptions { | ||
@@ -9,21 +9,91 @@ phrase: string; | ||
export declare abstract class Intl extends SyncModule { | ||
CurrentLocale: string; | ||
private _currentLocale; | ||
/** | ||
* Currently selected locale | ||
*/ | ||
get CurrentLocale(): string; | ||
/** | ||
* Currently selected locale | ||
*/ | ||
set CurrentLocale(value: string); | ||
/** | ||
* Map with avaible translations, keyed by locale name | ||
*/ | ||
Locales: Map<string, any>; | ||
/** | ||
* I18n localization function. Returns localized string. | ||
* If no translation is avaible at current selected language, then fallback to | ||
* default language, if still no translation exists, original text is returned | ||
* | ||
* @param text - text to localize. | ||
* @param args - argument passed to formatted text | ||
*/ | ||
abstract __(text: string | IPhraseWithOptions, ...args: any[]): string; | ||
/** | ||
* Plurals translation of a single phrase. Singular and plural forms will get added to locales if unknown. | ||
* Returns translated parsed and substituted string based on last count parameter. | ||
* | ||
* @param text - text to localize | ||
* @param count - number of items/things | ||
* @example use like `__n("%s cats", 1) returns `1 cat` | ||
*/ | ||
abstract __n(text: string | IPhraseWithOptions, count: number): string; | ||
/** | ||
* Returns a list of translations for a given phrase in each language. | ||
* | ||
* @param text - text to translate | ||
*/ | ||
abstract __l(text: string): string[]; | ||
/** | ||
* Returns a hashed list of translations for a given phrase in each language. | ||
* | ||
* @param text - text to translate | ||
*/ | ||
abstract __h(text: string): any[]; | ||
} | ||
/** | ||
* Basic internationalization support. Text phrases are read from json files specified | ||
* in system.dirs.locales | ||
*/ | ||
export declare class SpineJsInternationalizationFromJson extends Intl { | ||
get CurrentLocale(): string; | ||
set CurrentLocale(value: string); | ||
/** | ||
* Map with avaible translations, keyed by locale name | ||
*/ | ||
Locales: Map<string, any>; | ||
/** | ||
* Logger for this module | ||
*/ | ||
protected Log: Log; | ||
protected Configuration: Configuration; | ||
private _currentLocale; | ||
resolve(_c: IContainer): void; | ||
resolve(): void; | ||
/** | ||
* I18n localization function. Returns localized string. | ||
* If no translation is avaible at current selected language, then fallback to | ||
* default language, if still no translation exists, original text is returned | ||
* | ||
* @param text - text to localize. | ||
* @param args - argument passed to formatted text | ||
*/ | ||
__(text: string | IPhraseWithOptions, ...args: any[]): string; | ||
/** | ||
* Plurals translation of a single phrase. | ||
* Returns translated, parsed and substituted string based on count parameter. | ||
* | ||
* @param text - text to localize | ||
* @param count - number of items/things | ||
* @example use like `__n("%s cats", 1) returns '1 cat'` | ||
*/ | ||
__n(text: string | IPhraseWithOptions, count: number): string; | ||
/** | ||
* Returns a list of translations for a given phrase in each language. | ||
* | ||
* @param text - text to translate | ||
*/ | ||
__l(text: string): string[]; | ||
/** | ||
* Returns a hashed list of translations for a given phrase in each language. | ||
* | ||
* @param text - text to translate | ||
*/ | ||
__h(text: string): any[]; | ||
} |
142
lib/index.js
"use strict"; | ||
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]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -23,3 +27,3 @@ if (k2 === undefined) k2 = k; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
@@ -48,38 +52,56 @@ return result; | ||
super(...arguments); | ||
/** | ||
* Map with avaible translations, keyed by locale name | ||
*/ | ||
this.Locales = new Map(); | ||
} | ||
} | ||
exports.Intl = Intl; | ||
let SpineJsInternationalizationFromJson = class SpineJsInternationalizationFromJson extends Intl { | ||
constructor() { | ||
super(...arguments); | ||
this.Locales = new Map(); | ||
} | ||
/** | ||
* Currently selected locale | ||
*/ | ||
get CurrentLocale() { | ||
return this._currentLocale; | ||
} | ||
/** | ||
* Currently selected locale | ||
*/ | ||
set CurrentLocale(value) { | ||
if (!value) { | ||
throw new exceptions_1.InvalidArgument("value cannot be empty or null"); | ||
throw new exceptions_1.InvalidArgument('value cannot be empty or null'); | ||
} | ||
this._currentLocale = value; | ||
} | ||
resolve(_c) { | ||
this.CurrentLocale = this.Configuration.get("intl.defaultLocale", "en"); | ||
const localeDirs = this.Configuration.get("system.dirs.locales", []); | ||
localeDirs.filter(d => fs.existsSync(d)) | ||
.map(d => glob.sync(`${d}/**/*.json`)) | ||
} | ||
exports.Intl = Intl; | ||
/** | ||
* Basic internationalization support. Text phrases are read from json files specified | ||
* in system.dirs.locales | ||
*/ | ||
let SpineJsInternationalizationFromJson = class SpineJsInternationalizationFromJson extends Intl { | ||
constructor() { | ||
super(...arguments); | ||
/** | ||
* Map with avaible translations, keyed by locale name | ||
*/ | ||
this.Locales = new Map(); | ||
} | ||
// tslint:disable-next-line: variable-name | ||
resolve() { | ||
this.CurrentLocale = this.Configuration.get('intl.defaultLocale', 'en'); | ||
const localeDirs = this.Configuration.get('system.dirs.locales', []); | ||
localeDirs | ||
.filter((d) => fs.existsSync(d)) | ||
.map((d) => glob.sync(`${d}/**/*.json`)) | ||
.reduce((prev, current) => { | ||
return prev.concat(_.flattenDeep(current)); | ||
}, []) | ||
.map(f => path_1.normalize(path_1.resolve(f))) | ||
.map(f => { | ||
.map((f) => (0, path_1.normalize)((0, path_1.resolve)(f))) | ||
.map((f) => { | ||
this.Log.trace(`Found localisation file at ${f}`); | ||
return f; | ||
}) | ||
.forEach(f => { | ||
const lang = path_1.basename(f, '.json'); | ||
.forEach((f) => { | ||
const lang = (0, path_1.basename)(f, '.json'); | ||
let data; | ||
try { | ||
data = JSON.parse(fs.readFileSync(f, "utf-8")); | ||
data = JSON.parse(fs.readFileSync(f, 'utf-8')); | ||
} | ||
@@ -97,2 +119,10 @@ catch (ex) { | ||
} | ||
/** | ||
* I18n localization function. Returns localized string. | ||
* If no translation is avaible at current selected language, then fallback to | ||
* default language, if still no translation exists, original text is returned | ||
* | ||
* @param text - text to localize. | ||
* @param args - argument passed to formatted text | ||
*/ | ||
__(text, ...args) { | ||
@@ -102,2 +132,4 @@ var _a, _b; | ||
let toLocalize; | ||
if (!text) | ||
return ''; | ||
if (_.isString(text)) { | ||
@@ -116,2 +148,10 @@ locTable = this.Locales.get(this.CurrentLocale); | ||
} | ||
/** | ||
* Plurals translation of a single phrase. | ||
* Returns translated, parsed and substituted string based on count parameter. | ||
* | ||
* @param text - text to localize | ||
* @param count - number of items/things | ||
* @example use like `__n("%s cats", 1) returns '1 cat'` | ||
*/ | ||
__n(text, count) { | ||
@@ -122,2 +162,4 @@ var _a, _b; | ||
let locale; | ||
if (!text) | ||
return ''; | ||
if (_.isString(text)) { | ||
@@ -148,3 +190,3 @@ locale = this.CurrentLocale; | ||
const phrases = text.split(/\|/); | ||
phrases.some(phrase => { | ||
phrases.some((phrase) => { | ||
const matches = phrase.match(/^\s*([\(\)\[\]\d,]+)?\s*(.*)$/); | ||
@@ -160,2 +202,10 @@ if (matches[1] && _matchInterval(count, matches[1])) { | ||
return toReturn; | ||
/** | ||
* test a number to match mathematical interval expressions | ||
* [0,2] - 0 to 2 (including, matches: 0, 1, 2) | ||
* ]0,3[ - 0 to 3 (excluding, matches: 1, 2) | ||
* [1] - 1 (matches: 1) | ||
* [20,] - all numbers ≥20 (matches: 20, 21, 22, ...) | ||
* [,20] - all numbers ≤20 (matches: 20, 21, 22, ...) | ||
*/ | ||
function _matchInterval(c, eq) { | ||
@@ -170,3 +220,3 @@ const interval = InvervalParser.default(eq); | ||
} | ||
return (Math.min(interval.from.value, c) === interval.from.value && Math.max(interval.to.value, c) === interval.to.value); | ||
return Math.min(interval.from.value, c) === interval.from.value && Math.max(interval.to.value, c) === interval.to.value; | ||
} | ||
@@ -177,3 +227,10 @@ return false; | ||
} | ||
/** | ||
* Returns a list of translations for a given phrase in each language. | ||
* | ||
* @param text - text to translate | ||
*/ | ||
__l(text) { | ||
if (!text) | ||
return []; | ||
const extract = _.property(text); | ||
@@ -184,3 +241,10 @@ return Array.from(this.Locales.values()).map((v) => { | ||
} | ||
/** | ||
* Returns a hashed list of translations for a given phrase in each language. | ||
* | ||
* @param text - text to translate | ||
*/ | ||
__h(text) { | ||
if (!text) | ||
return []; | ||
const extract = _.property(text); | ||
@@ -193,22 +257,48 @@ return Array.from(this.Locales.values()).map((v, locale) => { | ||
__decorate([ | ||
log_1.Logger({ module: 'Locales' }), | ||
__metadata("design:type", Object) | ||
(0, log_1.Logger)('Intl'), | ||
__metadata("design:type", log_1.Log) | ||
], SpineJsInternationalizationFromJson.prototype, "Log", void 0); | ||
__decorate([ | ||
di_1.Autoinject(), | ||
(0, di_1.Autoinject)(), | ||
__metadata("design:type", configuration_1.Configuration) | ||
], SpineJsInternationalizationFromJson.prototype, "Configuration", void 0); | ||
SpineJsInternationalizationFromJson = __decorate([ | ||
di_1.Injectable(Intl) | ||
(0, di_1.Injectable)(Intl) | ||
], SpineJsInternationalizationFromJson); | ||
exports.SpineJsInternationalizationFromJson = SpineJsInternationalizationFromJson; | ||
/** | ||
* I18n localization function. Returns localized string. | ||
* If no translation is avaible at current selected language, then fallback to | ||
* default language, if still no translation exists, original text is returned | ||
* | ||
* @param text - text to localize. | ||
* @param locale - selected locale, if not specified - default locale is selected | ||
*/ | ||
globalAny.__ = (text, ...args) => { | ||
return di_1.DI.get(Intl).__(text, ...args); | ||
}; | ||
/** | ||
* Plurals translation of a single phrase. Singular and plural forms will get added to locales if unknown. | ||
* Returns translated parsed and substituted string based on last count parameter. | ||
* | ||
* @param text - text to localize | ||
* @param count - number of items/things | ||
* @example use like `__n("%s cats", 1) returns `1 cat` | ||
*/ | ||
globalAny.__n = (text, count) => { | ||
return di_1.DI.get(Intl).__n(text, count); | ||
}; | ||
/** | ||
* Returns a list of translations for a given phrase in each language. | ||
* | ||
* @param text - text to translate | ||
*/ | ||
globalAny.__l = (text) => { | ||
return di_1.DI.get(Intl).__l(text); | ||
}; | ||
/** | ||
* Returns a hashed list of translations for a given phrase in each language. | ||
* | ||
* @param text - text to translate | ||
*/ | ||
globalAny.__h = (text) => { | ||
@@ -215,0 +305,0 @@ return di_1.DI.get(Intl).__h(text); |
{ | ||
"name": "@spinajs/intl", | ||
"version": "1.0.1", | ||
"description": "spinajs internationalization api", | ||
"version": "1.2.103", | ||
"description": "internationalization for spinajs framework", | ||
"main": "lib/index.js", | ||
"private": false, | ||
"scripts": { | ||
"build": "npm run clean && npm run compile", | ||
"compile": "tsc -p tsconfig.build.json", | ||
"clean": "", | ||
"test": "ts-mocha -p tsconfig.json test/**/*.test.ts", | ||
"coverage": "nyc npm run test", | ||
"build-docs": "rimraf docs && typedoc --options typedoc.json src/", | ||
"build": "tsc", | ||
"prepare": "npm run build", | ||
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"", | ||
"lint": "tslint -p tsconfig.json", | ||
"format": "prettier --write \"src/**/*.ts\"", | ||
"lint": "eslint -c .eslintrc.js --ext .ts src --fix", | ||
"prepublishOnly": "npm test && npm run lint", | ||
@@ -20,26 +23,25 @@ "preversion": "npm run lint", | ||
"files": [ | ||
"lib/**/*", | ||
"typings/**/*" | ||
"lib/**/*" | ||
], | ||
"types": "lib", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/spinajs/intl.git" | ||
"url": "git+https://github.com/spinajs/main.git" | ||
}, | ||
"keywords": [ | ||
"spinajs", | ||
"internationalization", | ||
"intl" | ||
], | ||
"author": "SpinaJS <spinajs@coderush.pl> (https://github.com/spinajs/intl)", | ||
"author": "SpinaJS <spinajs@coderush.pl> (https://github.com/spinajs/main)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/spinajs/intl/issues" | ||
"url": "https://github.com/spinajs/main/issues" | ||
}, | ||
"homepage": "https://github.com/spinajs/intl#readme", | ||
"homepage": "https://github.com/spinajs/main#readme", | ||
"dependencies": { | ||
"@spinajs/configuration": "^1.0.5", | ||
"@spinajs/di": "^1.0.12", | ||
"@spinajs/exceptions": "^1.0.0", | ||
"@spinajs/log": "^1.0.5", | ||
"@spinajs/reflection": "^1.0.4", | ||
"@spinajs/configuration": "^1.2.81", | ||
"@spinajs/di": "^1.2.81", | ||
"@spinajs/exceptions": "^1.2.81", | ||
"@spinajs/log": "^1.2.103", | ||
"@spinajs/reflection": "^1.2.103", | ||
"glob": "^7.1.6", | ||
@@ -50,25 +52,3 @@ "lodash": "^4.17.15", | ||
}, | ||
"devDependencies": { | ||
"@types/bunyan": "^1.8.6", | ||
"@types/chai": "^4.1.7", | ||
"@types/chai-as-promised": "^7.1.0", | ||
"@types/lodash": "^4.14.136", | ||
"@types/mocha": "^5.2.7", | ||
"@types/sinon": "^7.0.13", | ||
"chai": "^4.2.0", | ||
"chai-as-promised": "^7.1.1", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.1", | ||
"prettier": "^1.18.2", | ||
"sinon": "^7.3.2", | ||
"ts-mocha": "^6.0.0", | ||
"ts-node": "^8.3.0", | ||
"tslint": "^5.20.1", | ||
"tslint-circular-dependencies": "^0.1.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-config-standard": "^8.0.1", | ||
"tslint-no-unused-expression-chai": "^0.1.4", | ||
"typedoc": "^0.14.2", | ||
"typescript": "^3.7.3" | ||
} | ||
"gitHead": "18a08cf4cd3e4d2c728d2d372e3a42ddde071be8" | ||
} |
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
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
65643
0
20
1222
1
12
3
1
Updated@spinajs/di@^1.2.81
Updated@spinajs/exceptions@^1.2.81
Updated@spinajs/log@^1.2.103
Updated@spinajs/reflection@^1.2.103