@spinajs/intl
Advanced tools
Comparing version 1.2.103 to 1.2.112
@@ -1,4 +0,5 @@ | ||
import { SyncModule } from '@spinajs/di'; | ||
import { AsyncModule } from '@spinajs/di'; | ||
import { Configuration } from '@spinajs/configuration'; | ||
import { Log } from '@spinajs/log'; | ||
export * from './sources'; | ||
export interface IPhraseWithOptions { | ||
@@ -8,3 +9,3 @@ phrase: string; | ||
} | ||
export declare abstract class Intl extends SyncModule { | ||
export declare abstract class Intl extends AsyncModule { | ||
private _currentLocale; | ||
@@ -22,3 +23,3 @@ /** | ||
*/ | ||
Locales: Map<string, any>; | ||
Locales: {}; | ||
/** | ||
@@ -63,3 +64,3 @@ * I18n localization function. Returns localized string. | ||
*/ | ||
Locales: Map<string, any>; | ||
Locales: {}; | ||
/** | ||
@@ -70,3 +71,3 @@ * Logger for this module | ||
protected Configuration: Configuration; | ||
resolve(): void; | ||
resolveAsync(): Promise<void>; | ||
/** | ||
@@ -73,0 +74,0 @@ * I18n localization function. Returns localized string. |
@@ -31,2 +31,5 @@ "use strict"; | ||
}; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
@@ -41,11 +44,10 @@ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
const exceptions_1 = require("@spinajs/exceptions"); | ||
const fs = __importStar(require("fs")); | ||
const glob = __importStar(require("glob")); | ||
const _ = __importStar(require("lodash")); | ||
const path_1 = require("path"); | ||
const util = __importStar(require("util")); | ||
const MakePlural = __importStar(require("make-plural")); | ||
const InvervalParser = __importStar(require("math-interval-parser")); | ||
const TranslatioSources = __importStar(require("./sources")); | ||
__exportStar(require("./sources"), exports); | ||
const globalAny = global; | ||
class Intl extends di_1.SyncModule { | ||
class Intl extends di_1.AsyncModule { | ||
constructor() { | ||
@@ -56,3 +58,3 @@ super(...arguments); | ||
*/ | ||
this.Locales = new Map(); | ||
this.Locales = {}; | ||
} | ||
@@ -86,35 +88,12 @@ /** | ||
*/ | ||
this.Locales = new Map(); | ||
this.Locales = {}; | ||
} | ||
// tslint:disable-next-line: variable-name | ||
resolve() { | ||
async resolveAsync() { | ||
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) => (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 = (0, path_1.basename)(f, '.json'); | ||
let data; | ||
try { | ||
data = JSON.parse(fs.readFileSync(f, 'utf-8')); | ||
} | ||
catch (ex) { | ||
this.Log.warn(ex, `Cannot load localisation data from file ${f} for lang ${lang}`); | ||
return; | ||
} | ||
if (!data) { | ||
this.Log.warn(`No localisation data at ${f} for lang ${lang}`); | ||
return; | ||
} | ||
this.Locales.set(lang, _.merge(data, this.Locales.get(lang) && {})); | ||
}); | ||
const sources = await di_1.DI.resolve(Array.ofType(TranslatioSources.TranslationSource)); | ||
for (const s of sources) { | ||
const translations = await s.load(); | ||
this.Locales = _.merge(translations, this.Locales); | ||
} | ||
} | ||
@@ -136,7 +115,7 @@ /** | ||
if (_.isString(text)) { | ||
locTable = this.Locales.get(this.CurrentLocale); | ||
locTable = this.Locales[this.CurrentLocale]; | ||
toLocalize = text; | ||
} | ||
else { | ||
locTable = (_a = this.Locales.get(text.locale)) !== null && _a !== void 0 ? _a : this.Locales.get(this.CurrentLocale); | ||
locTable = (_a = this.Locales[text.locale]) !== null && _a !== void 0 ? _a : this.Locales[this.CurrentLocale]; | ||
toLocalize = text.phrase; | ||
@@ -166,3 +145,3 @@ } | ||
locale = this.CurrentLocale; | ||
locTable = this.Locales.get(this.CurrentLocale); | ||
locTable = this.Locales[this.CurrentLocale]; | ||
toLocalize = text; | ||
@@ -172,6 +151,6 @@ } | ||
locale = (_a = text.locale) !== null && _a !== void 0 ? _a : this.CurrentLocale; | ||
locTable = (_b = this.Locales.get(text.locale)) !== null && _b !== void 0 ? _b : this.Locales.get(this.CurrentLocale); | ||
locTable = (_b = this.Locales[text.locale]) !== null && _b !== void 0 ? _b : this.Locales[this.CurrentLocale]; | ||
toLocalize = text.phrase; | ||
} | ||
if (/%/.test(toLocalize) && this.Locales.has(locale)) { | ||
if (/%/.test(toLocalize) && this.Locales[locale]) { | ||
const phrase = locTable[toLocalize]; | ||
@@ -234,3 +213,3 @@ const pluralVerb = MakePlural[locale](count); | ||
const extract = _.property(text); | ||
return Array.from(this.Locales.values()).map((v) => { | ||
return Array.from(Object.values(this.Locales)).map((v) => { | ||
return extract(v); | ||
@@ -248,3 +227,3 @@ }); | ||
const extract = _.property(text); | ||
return Array.from(this.Locales.values()).map((v, locale) => { | ||
return Array.from(Object.values(this.Locales)).map((v, locale) => { | ||
return { [locale]: extract(v) }; | ||
@@ -251,0 +230,0 @@ }); |
{ | ||
"name": "@spinajs/intl", | ||
"version": "1.2.103", | ||
"version": "1.2.112", | ||
"description": "internationalization for spinajs framework", | ||
@@ -51,3 +51,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "18a08cf4cd3e4d2c728d2d372e3a42ddde071be8" | ||
"gitHead": "a301403049bb83a6651a117ec5d6339390987a5b" | ||
} |
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
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
2
31391
11
538