Comparing version 1.8.0 to 2.0.0
@@ -1,3 +0,7 @@ | ||
export { formatTimestamp } from './formatTimestamp'; | ||
export { DateInterval } from './DateInterval'; | ||
export { DateExtensions } from './DateExtensions'; | ||
export { timestampToInterval } from './timestampToInterval'; | ||
export { formatInterval } from './formatInterval'; | ||
export { formatTimer } from './formatTimer'; | ||
export { parseMicrosoftFormatOffset } from './ParseMicrosoftFormatOffset'; | ||
export { dateToUTCString } from './DateToUTCString'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var formatTimestamp_1 = require("./formatTimestamp"); | ||
exports.formatTimestamp = formatTimestamp_1.formatTimestamp; | ||
var DateExtensions_1 = require("./DateExtensions"); | ||
exports.DateExtensions = DateExtensions_1.DateExtensions; | ||
var timestampToInterval_1 = require("./timestampToInterval"); | ||
exports.timestampToInterval = timestampToInterval_1.timestampToInterval; | ||
var formatInterval_1 = require("./formatInterval"); | ||
exports.formatInterval = formatInterval_1.formatInterval; | ||
var formatTimer_1 = require("./formatTimer"); | ||
exports.formatTimer = formatTimer_1.formatTimer; | ||
var ParseMicrosoftFormatOffset_1 = require("./ParseMicrosoftFormatOffset"); | ||
@@ -6,0 +12,0 @@ exports.parseMicrosoftFormatOffset = ParseMicrosoftFormatOffset_1.parseMicrosoftFormatOffset; |
@@ -10,3 +10,3 @@ export * from './fn/array/index'; | ||
export { Logger } from './Logger'; | ||
export { Intl, _, __ } from './Intl'; | ||
export { _, Intl, StringsMap, StringsCategory, Strings } from './Intl'; | ||
export { IDisposable, ITyped, IError } from './Types'; |
@@ -13,5 +13,4 @@ "use strict"; | ||
var Intl_1 = require("./Intl"); | ||
exports._ = Intl_1._; | ||
exports.Intl = Intl_1.Intl; | ||
exports._ = Intl_1._; | ||
exports.__ = Intl_1.__; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,1 @@ | ||
declare type Locales = 'ru-ru' | 'en-us'; | ||
export interface StringsMap { | ||
@@ -6,21 +5,20 @@ [key: string]: string | string[]; | ||
export interface StringsCategory { | ||
[lang: string]: StringsMap; | ||
[module: string]: StringsMap; | ||
} | ||
export interface Strings { | ||
[module: string]: StringsCategory; | ||
[lang: string]: StringsCategory; | ||
} | ||
export declare class Intl { | ||
private static currentLocale; | ||
private static categories; | ||
private static intlInitialized; | ||
static setLocale: (value: string) => Locales; | ||
private static safeT; | ||
private static safeTS; | ||
static t: (category: string, key: string) => string; | ||
static ts: (category: string, key: string) => string[]; | ||
static registerStrings: (category: string, strings: StringsCategory) => void; | ||
static register: () => void; | ||
declare class IntlManager { | ||
private currentLocale; | ||
private strings; | ||
constructor(); | ||
setLocale: (value: string) => string; | ||
private safeT; | ||
t: (category: string, key: string, args?: any) => string; | ||
private registerStrings; | ||
private registerCategories; | ||
register: (i18n?: StringsCategory) => void; | ||
} | ||
export declare const _: (category: string, key: string) => string; | ||
export declare const __: (category: string, key: string) => string[]; | ||
export declare const Intl: IntlManager; | ||
export declare const _: (category: string, key: string, args?: any) => string; | ||
export {}; |
161
dist/Intl.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const intl_messageformat_1 = require("intl-messageformat"); | ||
const warning = require("warning"); | ||
const defaultLocale = 'ru-ru'; | ||
class Intl { | ||
} | ||
exports.Intl = Intl; | ||
Intl.currentLocale = defaultLocale; | ||
Intl.categories = {}; | ||
Intl.intlInitialized = false; | ||
Intl.setLocale = (value) => { | ||
if (value == "en" || value == "en-us" || value == "en-uk") { | ||
Intl.currentLocale = 'en-us'; | ||
} | ||
else { | ||
Intl.currentLocale = defaultLocale; | ||
} | ||
return Intl.currentLocale; | ||
}; | ||
Intl.safeT = (category, locale, key) => { | ||
let result = ""; | ||
if (Intl.categories[category] && Intl.categories[category][locale]) { | ||
result = Intl.categories[category][locale][key]; | ||
} | ||
return result; | ||
}; | ||
Intl.safeTS = (category, locale, key) => { | ||
let result = []; | ||
if (Intl.categories[category] && Intl.categories[category][locale]) { | ||
result = Intl.categories[category][locale][key]; | ||
} | ||
return result; | ||
}; | ||
Intl.t = (category, key) => { | ||
let result = Intl.safeT(category, Intl.currentLocale, key); | ||
if (!result) { | ||
result = Intl.safeT(category, defaultLocale, key); | ||
} | ||
warning(result !== null, `Empty string for category ${category} and key ${key}.`); | ||
return result; | ||
}; | ||
Intl.ts = (category, key) => { | ||
let result = Intl.safeTS(category, Intl.currentLocale, key); | ||
if (!result) { | ||
result = Intl.safeTS(category, defaultLocale, key); | ||
} | ||
warning(result !== null, `Empty string for category ${category} and key ${key}.`); | ||
return result; | ||
}; | ||
Intl.registerStrings = (category, strings) => { | ||
if (!Intl.categories[category]) { | ||
Intl.categories[category] = strings; | ||
} | ||
else { | ||
for (const lang in strings) { | ||
if (!Intl.categories[category][lang]) { | ||
Intl.categories[category][lang] = {}; | ||
const enTranslation = require("./i18n/en-us.json"); | ||
const ruTranslation = require("./i18n/ru-ru.json"); | ||
const defaultLocale = "en-us"; | ||
let initialized = false; | ||
class IntlManager { | ||
constructor() { | ||
this.currentLocale = defaultLocale; | ||
this.strings = {}; | ||
this.setLocale = (value) => { | ||
const msg = new intl_messageformat_1.IntlMessageFormat('', value); | ||
const locale = msg.resolvedOptions().locale; | ||
if (locale) { | ||
this.currentLocale = locale.toLowerCase(); | ||
} | ||
const langCat = strings[lang]; | ||
for (const key in langCat) { | ||
Intl.categories[category][lang][key] = langCat[key]; | ||
else { | ||
this.currentLocale = defaultLocale; | ||
} | ||
return this.currentLocale; | ||
}; | ||
this.safeT = (category, locale, key) => { | ||
let result = key; | ||
if (this.strings[locale] && this.strings[locale][category]) { | ||
result = this.strings[locale][category][key]; | ||
} | ||
return result; | ||
}; | ||
this.t = (category, key, args) => { | ||
let message = this.safeT(category, this.currentLocale, key); | ||
if (message === key) { | ||
message = this.safeT(category, defaultLocale, key); | ||
} | ||
warning(message !== key, `Empty string for category ${category} and key ${key}.`); | ||
const formatter = new intl_messageformat_1.IntlMessageFormat(message, this.currentLocale); | ||
const result = formatter.format(args); | ||
if (result) { | ||
if (typeof result === "string") { | ||
return result; | ||
} | ||
else { | ||
return result.join(" "); | ||
} | ||
} | ||
return message; | ||
}; | ||
this.registerStrings = (lang, category, strings) => { | ||
if (this.strings[lang] === undefined) { | ||
this.strings[lang] = {}; | ||
} | ||
if (this.strings[lang][category] === undefined) { | ||
this.strings[lang][category] = {}; | ||
} | ||
for (const key in strings) { | ||
this.strings[lang][category][key] = strings[key]; | ||
} | ||
}; | ||
this.registerCategories = (lang, strings) => { | ||
for (const category in strings) { | ||
this.registerStrings(lang, category, strings[category]); | ||
} | ||
}; | ||
this.register = (i18n) => { | ||
if (!initialized) { | ||
this.registerCategories("en-us", enTranslation); | ||
this.registerCategories("ru-ru", ruTranslation); | ||
initialized = true; | ||
} | ||
if (i18n !== undefined) { | ||
this.registerCategories(this.currentLocale, i18n); | ||
} | ||
}; | ||
if ((window !== undefined) && (window["navigator"])) { | ||
this.setLocale(window.navigator.language); | ||
} | ||
this.register(); | ||
} | ||
}; | ||
Intl.register = () => { | ||
if (!Intl.intlInitialized) { | ||
Intl.registerStrings('app', { | ||
'ru-ru': { | ||
time_lessone: "менее 1", | ||
time_empty: "---", | ||
time_nolabel: ["", "", "", ""], | ||
time_days: ["дн.", "день", "дня", "дней"], | ||
time_hours: ["ч.", "час", "часа", "часов"], | ||
time_minutes: ["мин.", "минута", "минуты", "минут"], | ||
time_seconds: ["сек.", "секунда", "секунды", "секунд"] | ||
}, | ||
'en-us': { | ||
time_lessone: "less 1", | ||
time_empty: "---", | ||
time_nolabel: ["", "", "", ""], | ||
time_days: ["d.", "day", "days", "days"], | ||
time_hours: ["h.", "hour", "hours", "hours"], | ||
time_minutes: ["m.", "minute", "minutes", "minutes"], | ||
time_seconds: ["sec.", "second", "seconds", "seconds"] | ||
} | ||
}); | ||
Intl.intlInitialized = true; | ||
} | ||
}; | ||
if ((window !== undefined) && (window['navigator'])) { | ||
Intl.setLocale(window.navigator.language); | ||
} | ||
exports._ = Intl.t; | ||
exports.__ = Intl.ts; | ||
exports.Intl = new IntlManager(); | ||
exports._ = exports.Intl.t; | ||
//# sourceMappingURL=Intl.js.map |
{ | ||
"name": "onix-core", | ||
"version": "1.8.0", | ||
"version": "2.0.0", | ||
"description": "Onix library core", | ||
@@ -13,3 +13,3 @@ "main": "dist/index.js", | ||
"statsp": "webpack --profile --json --mode=production > stats.json", | ||
"test": "mocha --recursive --require @babel/register --require ./test/helpers.js" | ||
"test": "cross-env NODE_ICU_DATA=node_modules/full-icu mocha --recursive --require @babel/register --require ./test/helpers.js" | ||
}, | ||
@@ -24,5 +24,6 @@ "keywords": [ | ||
}, | ||
"author": "Nick Panteleeff", | ||
"author": "Nick Panteleeff <drnixx@gmail.com> (https://github.com/DrNixx)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"intl-messageformat": "^8.3.2", | ||
"lodash-es": "^4.17.15", | ||
@@ -38,4 +39,7 @@ "warning": "^4.0.3" | ||
"@types/warning": "^3.0.0", | ||
"babel-polyfill": "^6.26.0", | ||
"chai": "^4.2.0", | ||
"clean-webpack-plugin": "^3.0.0", | ||
"cross-env": "^7.0.2", | ||
"full-icu": "^1.3.1", | ||
"mocha": "^7.1.0", | ||
@@ -42,0 +46,0 @@ "ts-loader": "^6.2.1", |
# onix-core | ||
[![npm version][npm-badge]][npm] [![npm downloads][npd-badge]][npm] [![dependencies Status][dps-badge]][dps][![FOSSA Status][fossa-badge]][fossa] | ||
[![npm version][npm-badge]][npm] [![npm downloads][npd-badge]][npm] [![dependencies Status][dps-badge]][dps] | ||
@@ -18,3 +18,1 @@ Core JavaSctipt library for the all Onix JS components. | ||
[npm]: https://www.npmjs.com/package/onix-core | ||
[fossa-badge]: https://app.fossa.com/api/projects/git%2Bgithub.com%2FDrNixx%2Fonix-core.svg?type=shield | ||
[fossa]: https://app.fossa.com/projects/git%2Bgithub.com%2FDrNixx%2Fonix-core?ref=badge_shield |
@@ -1,3 +0,7 @@ | ||
export { formatTimestamp } from './formatTimestamp'; | ||
export { DateInterval } from './DateInterval'; | ||
export { DateExtensions } from './DateExtensions'; | ||
export { timestampToInterval } from './timestampToInterval'; | ||
export { formatInterval } from './formatInterval'; | ||
export { formatTimer } from './formatTimer'; | ||
export { parseMicrosoftFormatOffset } from './ParseMicrosoftFormatOffset'; | ||
export { dateToUTCString } from './DateToUTCString'; | ||
export { dateToUTCString } from './DateToUTCString'; |
@@ -14,3 +14,3 @@ export * from './fn/array/index'; | ||
export { Logger } from './Logger'; | ||
export { Intl, _, __ } from './Intl'; | ||
export { _, Intl, StringsMap, StringsCategory, Strings } from './Intl'; | ||
export { IDisposable, ITyped, IError } from './Types'; |
150
src/Intl.ts
@@ -0,6 +1,6 @@ | ||
import { IntlMessageFormat } from 'intl-messageformat'; | ||
import * as warning from 'warning'; | ||
import * as enTranslation from './i18n/en-us.json'; | ||
import * as ruTranslation from './i18n/ru-ru.json'; | ||
declare type Locales = 'ru-ru' | 'en-us'; | ||
declare type StringsCallback = (key: string) => string; | ||
export interface StringsMap { | ||
@@ -11,39 +11,41 @@ [key: string]: string | string[]; | ||
export interface StringsCategory { | ||
[lang: string]: StringsMap; | ||
[module: string]: StringsMap; | ||
} | ||
export interface Strings { | ||
[module: string]: StringsCategory; | ||
[lang: string]: StringsCategory; | ||
} | ||
const defaultLocale: Locales = 'ru-ru'; | ||
const defaultLocale = "en-us"; | ||
let initialized = false; | ||
export class Intl { | ||
private static currentLocale: Locales = defaultLocale; | ||
private static categories: Strings = {}; | ||
private static intlInitialized = false; | ||
class IntlManager { | ||
private currentLocale: string = defaultLocale; | ||
private strings: Strings = {}; | ||
public static setLocale = (value: string) => { | ||
if (value=="en" || value=="en-us" || value=="en-uk") { | ||
Intl.currentLocale = 'en-us'; | ||
} else { | ||
Intl.currentLocale = defaultLocale; | ||
public constructor() { | ||
if ((window !== undefined) && (window["navigator"])) { | ||
this.setLocale(window.navigator.language); | ||
} | ||
return Intl.currentLocale; | ||
}; | ||
this.register(); | ||
} | ||
private static safeT = (category: string, locale: string, key: string) => { | ||
let result: string = ""; | ||
if (Intl.categories[category] && Intl.categories[category][locale]) { | ||
result = <string>Intl.categories[category][locale][key]; | ||
public setLocale = (value: string) => { | ||
const msg = new IntlMessageFormat('', value); | ||
const locale = msg.resolvedOptions().locale; | ||
if (locale) { | ||
this.currentLocale = locale.toLowerCase(); | ||
} else { | ||
this.currentLocale = defaultLocale; | ||
} | ||
return result; | ||
return this.currentLocale; | ||
}; | ||
private static safeTS = (category: string, locale: string, key: string) => { | ||
let result: string[] = []; | ||
if (Intl.categories[category] && Intl.categories[category][locale]) { | ||
result = <string[]>Intl.categories[category][locale][key]; | ||
private safeT = (category: string, locale: string, key: string) => { | ||
let result: string = key; | ||
if (this.strings[locale] && this.strings[locale][category]) { | ||
result = <string>this.strings[locale][category][key]; | ||
} | ||
@@ -54,79 +56,59 @@ | ||
public static t = (category: string, key: string) => { | ||
let result = Intl.safeT(category, Intl.currentLocale, key); | ||
if (!result) { | ||
result = Intl.safeT(category, defaultLocale, key); | ||
public t = (category: string, key: string, args?: any): string => { | ||
let message = this.safeT(category, this.currentLocale, key); | ||
if (message === key) { | ||
message = this.safeT(category, defaultLocale, key); | ||
} | ||
warning(result !== null, | ||
warning(message !== key, | ||
`Empty string for category ${category} and key ${key}.` | ||
); | ||
return result; | ||
const formatter = new IntlMessageFormat(message, this.currentLocale); | ||
const result = formatter.format(args); | ||
if (result) { | ||
if (typeof result === "string") { | ||
return result; | ||
} else { | ||
return result.join(" "); | ||
} | ||
} | ||
return message; | ||
}; | ||
public static ts = (category: string, key: string) => { | ||
let result = Intl.safeTS(category, Intl.currentLocale, key); | ||
if (!result) { | ||
result = Intl.safeTS(category, defaultLocale, key); | ||
private registerStrings = (lang: string, category: string, strings: StringsMap) => { | ||
if (this.strings[lang] === undefined) { | ||
this.strings[lang] = {}; | ||
} | ||
warning(result !== null, | ||
`Empty string for category ${category} and key ${key}.` | ||
); | ||
if (this.strings[lang][category] === undefined) { | ||
this.strings[lang][category] = {}; | ||
} | ||
return result; | ||
}; | ||
public static registerStrings = (category: string, strings: StringsCategory) => { | ||
if (!Intl.categories[category]) { | ||
Intl.categories[category] = strings; | ||
} else { | ||
for (const lang in strings) { | ||
if (!Intl.categories[category][lang]) { | ||
Intl.categories[category][lang] = {}; | ||
} | ||
const langCat = strings[lang]; | ||
for (const key in langCat) { | ||
Intl.categories[category][lang][key] = langCat[key]; | ||
} | ||
} | ||
for (const key in strings) { | ||
this.strings[lang][category][key] = strings[key]; | ||
} | ||
}; | ||
public static register = () => { | ||
if (!Intl.intlInitialized) { | ||
Intl.registerStrings('app', { | ||
'ru-ru': { | ||
time_lessone: "менее 1", | ||
time_empty: "---", | ||
time_nolabel: ["", "", "", ""], | ||
time_days: ["дн.", "день", "дня", "дней"], | ||
time_hours: ["ч.", "час", "часа", "часов"], | ||
time_minutes: ["мин.", "минута", "минуты", "минут"], | ||
time_seconds: ["сек.", "секунда", "секунды", "секунд"] | ||
}, | ||
private registerCategories = (lang: string, strings: StringsCategory) => { | ||
for (const category in strings) { | ||
this.registerStrings(lang, category, strings[category]); | ||
} | ||
} | ||
'en-us': { | ||
time_lessone: "less 1", | ||
time_empty: "---", | ||
time_nolabel: ["", "", "", ""], | ||
time_days: ["d.", "day", "days", "days"], | ||
time_hours: ["h.", "hour", "hours", "hours"], | ||
time_minutes: ["m.", "minute", "minutes", "minutes"], | ||
time_seconds: ["sec.", "second", "seconds", "seconds"] | ||
} | ||
}); | ||
public register = (i18n?: StringsCategory) => { | ||
if (!initialized) { | ||
this.registerCategories("en-us", enTranslation); | ||
this.registerCategories("ru-ru", ruTranslation); | ||
Intl.intlInitialized = true; | ||
initialized = true; | ||
} | ||
if (i18n !== undefined) { | ||
this.registerCategories(this.currentLocale, i18n); | ||
} | ||
} | ||
} | ||
if ((window !== undefined) && (window['navigator'])) { | ||
Intl.setLocale(window.navigator.language); | ||
} | ||
export const Intl = new IntlManager(); | ||
export const _ = Intl.t; | ||
export const __ = Intl.ts; |
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
111076
139
2121
3
16
18
+ Addedintl-messageformat@^8.3.2
+ Added@formatjs/ecma402-abstract@1.4.0(transitive)
+ Added@formatjs/intl-numberformat@5.7.6(transitive)
+ Addedintl-format-cache@4.3.1(transitive)
+ Addedintl-messageformat@8.4.1(transitive)
+ Addedintl-messageformat-parser@5.5.1(transitive)
+ Addedtslib@2.8.1(transitive)