@yandex-cloud/i18n
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -1,16 +0,4 @@ | ||
import { Params } from "./types"; | ||
import { Logger, Params } from "./types"; | ||
declare type KeysData = Record<string, string | string[]>; | ||
declare type KeysetData = Record<string, KeysData>; | ||
declare global { | ||
interface Window { | ||
Ya?: { | ||
Rum?: { | ||
logError: (arg?: any) => void; | ||
ERROR_LEVEL: { | ||
INFO: string; | ||
}; | ||
}; | ||
}; | ||
} | ||
} | ||
export * from './types'; | ||
@@ -23,2 +11,6 @@ export declare class I18N { | ||
lang: string | undefined; | ||
logger: Logger | null; | ||
constructor(options?: { | ||
logger?: Logger; | ||
}); | ||
setLang(lang: string): void; | ||
@@ -25,0 +17,0 @@ registerKeyset(lang: string, keysetName: string, data?: KeysData): void; |
import { pluralize } from "./pluralize"; | ||
import { replaceParams } from "./replace-params"; | ||
import { rumLogger } from "./rum-logger"; | ||
import { Plural } from "./types"; | ||
const warnCache = new Set(); | ||
export * from './types'; | ||
export class I18N { | ||
constructor() { | ||
constructor(options = { logger: rumLogger }) { | ||
this.data = { | ||
@@ -13,2 +13,4 @@ [I18N.LANGS.ru]: {}, | ||
this.lang = undefined; | ||
this.logger = null; | ||
this.logger = (options === null || options === void 0 ? void 0 : options.logger) || null; | ||
} | ||
@@ -112,2 +114,3 @@ static setDefaultLang(lang) { | ||
warn(msg, keyset, key) { | ||
var _a; | ||
let cacheKey = ''; | ||
@@ -123,22 +126,9 @@ if (keyset) { | ||
} | ||
if (!warnCache.has(cacheKey)) { | ||
console.warn(`[i18n][${cacheKey}] ${msg}`); | ||
if (typeof window !== 'undefined' | ||
&& window.Ya | ||
&& window.Ya.Rum | ||
&& typeof window.Ya.Rum.logError === 'function') { | ||
try { | ||
window.Ya.Rum.logError({ | ||
message: `I18n: ${msg}`, | ||
type: 'i18n', | ||
level: window.Ya.Rum.ERROR_LEVEL.INFO, | ||
block: cacheKey, | ||
}); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
} | ||
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.log(`I18n: ${msg}`, { | ||
level: 'info', | ||
logger: cacheKey, | ||
extra: { | ||
type: 'i18n' | ||
} | ||
warnCache.add(cacheKey); | ||
} | ||
}); | ||
} | ||
@@ -145,0 +135,0 @@ } |
@@ -33,2 +33,9 @@ declare type NoEnumLikeStringLiteral<T> = string extends T ? T : never; | ||
} | ||
export interface Logger { | ||
log(message: string, options?: { | ||
level?: string; | ||
logger?: string; | ||
extra?: Record<string, unknown>; | ||
}): void; | ||
} | ||
export {}; |
# Changelog | ||
## [0.4.0](https://www.github.com/yandex-cloud/i18n/compare/v0.3.0...v0.4.0) (2022-04-04) | ||
### Features | ||
* add `logger` option to constructor ([#20](https://www.github.com/yandex-cloud/i18n/issues/20)) ([b135807](https://www.github.com/yandex-cloud/i18n/commit/b1358071a215b85c9e34d611a84b184d85511bfc)) | ||
## [0.3.0](https://www.github.com/yandex-cloud/i18n/compare/v0.2.2...v0.3.0) (2022-03-23) | ||
@@ -4,0 +11,0 @@ |
{ | ||
"name": "@yandex-cloud/i18n", | ||
"description": "i18n library for Yandex Cloud UI services", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "main": "build/esm/index.js", |
@@ -9,2 +9,18 @@ ## I18N utilities | ||
### API | ||
#### constructor(options) | ||
Accepts `options` object with optional `logger` that would be used for logging library warnings. | ||
##### logger | ||
Logger should have explicit `log` method with following signature: | ||
* `message` - string of message that would be logged | ||
* `options` - object of logging options: | ||
* `level` - level for logging message, always `'info'` | ||
* `logger` - where to log library messages | ||
* `extra` - additional options object, with a single `type` string, that is always `i18n` | ||
### Use examples | ||
@@ -11,0 +27,0 @@ |
105988
17
1771
246