@tolgee/core
Advanced tools
Comparing version 2.8.2 to 3.0.0
@@ -12,2 +12,4 @@ import { ArgumentTypes } from './commonTypes'; | ||
private static evaluateGenerator; | ||
static getNodeText(node: Node): string; | ||
static setNodeText(node: Node, text: string): void; | ||
} |
@@ -131,2 +131,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) { | ||
}; | ||
NodeHelper.getNodeText = function (node) { | ||
return node.textContent; | ||
}; | ||
NodeHelper.setNodeText = function (node, text) { | ||
node.textContent = text; | ||
}; | ||
return NodeHelper; | ||
@@ -133,0 +139,0 @@ }()); |
@@ -11,9 +11,10 @@ import { ElementWithMeta } from '../types'; | ||
constructor(properties: Properties); | ||
run(): void; | ||
handle(element: ElementWithMeta & ElementCSSInlineStyle, onclick: (clickEvent: MouseEvent) => void): void; | ||
private initEventListeners; | ||
private onConditionsChanged; | ||
private highlight; | ||
private unhighlight; | ||
private onMouseOut; | ||
private onMouseOver; | ||
private readonly onConditionsChanged; | ||
private readonly highlight; | ||
private readonly unhighlight; | ||
private readonly onMouseOut; | ||
private readonly onMouseOver; | ||
private getMouseOn; | ||
@@ -20,0 +21,0 @@ private initKeyListener; |
@@ -16,2 +16,3 @@ var __values = (this && this.__values) || function(o) { | ||
function MouseEventHandler(properties) { | ||
var _this = this; | ||
this.properties = properties; | ||
@@ -22,2 +23,32 @@ this.keysDown = new Set(); | ||
this.keysChanged = new EventEmitterImpl(); | ||
this.onConditionsChanged = function () { | ||
_this.unhighlight(); | ||
if (_this.areKeysDown() && _this.getMouseOn()) { | ||
_this.highlight(); | ||
} | ||
}; | ||
this.highlight = function () { | ||
_this.getMouseOn()._tolgee.highlight(); | ||
_this.highlighted = _this.getMouseOn(); | ||
}; | ||
this.unhighlight = function () { | ||
if (_this.highlighted) { | ||
_this.highlighted._tolgee.unhighlight(); | ||
_this.highlighted = null; | ||
} | ||
}; | ||
this.onMouseOut = function (element) { | ||
element._tolgee.preventClean = false; | ||
_this.mouseOn.delete(element); | ||
_this.mouseOnChanged.emit(_this.getMouseOn()); | ||
}; | ||
this.onMouseOver = function (element) { | ||
_this.filterMouseOn(); | ||
element._tolgee.preventClean = true; | ||
_this.mouseOn.delete(element); //to get in to last place | ||
_this.mouseOn.add(element); | ||
_this.mouseOnChanged.emit(_this.getMouseOn()); | ||
}; | ||
} | ||
MouseEventHandler.prototype.run = function () { | ||
if (typeof window !== 'undefined') { | ||
@@ -27,3 +58,3 @@ this.initKeyListener(); | ||
} | ||
} | ||
}; | ||
MouseEventHandler.prototype.handle = function (element, onclick) { | ||
@@ -77,30 +108,2 @@ var _this = this; | ||
}; | ||
MouseEventHandler.prototype.onConditionsChanged = function () { | ||
this.unhighlight(); | ||
if (this.areKeysDown() && this.getMouseOn()) { | ||
this.highlight(); | ||
} | ||
}; | ||
MouseEventHandler.prototype.highlight = function () { | ||
this.getMouseOn()._tolgee.highlight(); | ||
this.highlighted = this.getMouseOn(); | ||
}; | ||
MouseEventHandler.prototype.unhighlight = function () { | ||
if (this.highlighted) { | ||
this.highlighted._tolgee.unhighlight(); | ||
this.highlighted = null; | ||
} | ||
}; | ||
MouseEventHandler.prototype.onMouseOut = function (element) { | ||
element._tolgee.preventClean = false; | ||
this.mouseOn.delete(element); | ||
this.mouseOnChanged.emit(this.getMouseOn()); | ||
}; | ||
MouseEventHandler.prototype.onMouseOver = function (element) { | ||
this.filterMouseOn(); | ||
element._tolgee.preventClean = true; | ||
this.mouseOn.delete(element); //to get in to last place | ||
this.mouseOn.add(element); | ||
this.mouseOnChanged.emit(this.getMouseOn()); | ||
}; | ||
MouseEventHandler.prototype.getMouseOn = function () { | ||
@@ -107,0 +110,0 @@ var mouseOnArray = Array.from(this.mouseOn); |
import { ElementWithMeta } from '../types'; | ||
import { PluginManager } from '../toolsManager/PluginManager'; | ||
import { DependencyStore } from '../services/DependencyStore'; | ||
import { DependencyService } from '../services/DependencyService'; | ||
export declare class TranslationHighlighter { | ||
@@ -8,3 +8,3 @@ private dependencies; | ||
private _renderer; | ||
constructor(dependencies: DependencyStore); | ||
constructor(dependencies: DependencyService); | ||
private static getKeyOptions; | ||
@@ -11,0 +11,0 @@ listen(element: ElementWithMeta & ElementCSSInlineStyle): void; |
@@ -5,2 +5,3 @@ export { Tolgee } from './Tolgee'; | ||
export { TOLGEE_WRAPPED_ONLY_DATA_ATTRIBUTE } from './Constants/Global'; | ||
export * from './modules'; | ||
export * from './types/DTOs'; |
@@ -5,3 +5,4 @@ export { Tolgee } from './Tolgee'; | ||
export { TOLGEE_WRAPPED_ONLY_DATA_ATTRIBUTE } from './Constants/Global'; | ||
export * from './modules'; | ||
export * from './types/DTOs'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,13 +0,9 @@ | ||
import { CoreHandler } from './handlers/CoreHandler'; | ||
import { Properties } from './Properties'; | ||
import { TextHandler } from './handlers/TextHandler'; | ||
import { AttributeHandler } from './handlers/AttributeHandler'; | ||
import { ElementRegistrar } from './services/ElementRegistrar'; | ||
import { AbstractWrapper } from './wrappers/AbstractWrapper'; | ||
export declare class Observer { | ||
private properties; | ||
private coreHandler; | ||
private basicTextHandler; | ||
private attributeHandler; | ||
private textWrapper; | ||
private nodeRegistrar; | ||
constructor(properties: Properties, coreHandler: CoreHandler, basicTextHandler: TextHandler, attributeHandler: AttributeHandler, nodeRegistrar: ElementRegistrar); | ||
constructor(properties: Properties, textWrapper: AbstractWrapper, nodeRegistrar: ElementRegistrar); | ||
private _observer; | ||
@@ -14,0 +10,0 @@ private get observer(); |
@@ -49,7 +49,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
var Observer = /** @class */ (function () { | ||
function Observer(properties, coreHandler, basicTextHandler, attributeHandler, nodeRegistrar) { | ||
function Observer(properties, textWrapper, nodeRegistrar) { | ||
this.properties = properties; | ||
this.coreHandler = coreHandler; | ||
this.basicTextHandler = basicTextHandler; | ||
this.attributeHandler = attributeHandler; | ||
this.textWrapper = textWrapper; | ||
this.nodeRegistrar = nodeRegistrar; | ||
@@ -75,3 +73,3 @@ this._observer = undefined; | ||
if (!(mutation.type === 'characterData')) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.basicTextHandler.handle(mutation.target)]; | ||
return [4 /*yield*/, this.textWrapper.handleText(mutation.target)]; | ||
case 2: | ||
@@ -82,3 +80,3 @@ _b.sent(); | ||
if (!(mutation.type === 'childList')) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, this.coreHandler.handleSubtree(mutation.target)]; | ||
return [4 /*yield*/, this.textWrapper.handleSubtree(mutation.target)]; | ||
case 4: | ||
@@ -89,3 +87,3 @@ _b.sent(); | ||
if (!(mutation.type === 'attributes')) return [3 /*break*/, 7]; | ||
return [4 /*yield*/, this.attributeHandler.handle(mutation.target)]; | ||
return [4 /*yield*/, this.textWrapper.handleAttribute(mutation.target)]; | ||
case 6: | ||
@@ -92,0 +90,0 @@ _b.sent(); |
@@ -13,3 +13,6 @@ var PREFERRED_LANGUAGES_LOCAL_STORAGE_KEY = '__tolgee_preferredLanguages'; | ||
if (typeof localStorage !== 'undefined') { | ||
result = localStorage.getItem(CURRENT_LANGUAGE_LOCAL_STORAGE_KEY); | ||
var storedLanguage = localStorage.getItem(CURRENT_LANGUAGE_LOCAL_STORAGE_KEY); | ||
if (storedLanguage) { | ||
result = storedLanguage; | ||
} | ||
if (this.config.availableLanguages) { | ||
@@ -32,3 +35,6 @@ var isSavedLanguageAvailable = this.config.availableLanguages.indexOf(result) > -1; | ||
set: function (language) { | ||
if (typeof localStorage == 'undefined') { | ||
if (!language) { | ||
throw new Error("Setting invalid language value ".concat(language)); | ||
} | ||
if (typeof localStorage === 'undefined') { | ||
this._currentLanguage = language; | ||
@@ -35,0 +41,0 @@ return; |
@@ -1,22 +0,14 @@ | ||
import { KeyAndParams, TranslationParams } from '../types'; | ||
import { TranslationParams } from '../types'; | ||
import { TranslationService } from './TranslationService'; | ||
import { Properties } from '../Properties'; | ||
export declare type ReplacedType = { | ||
text: string; | ||
keys: KeyAndParams[]; | ||
}; | ||
import { ModuleService } from './ModuleService'; | ||
export declare class TextService { | ||
private properties; | ||
private translationService; | ||
constructor(properties: Properties, translationService: TranslationService); | ||
private get rawUnWrapRegex(); | ||
private static parseUnwrapped; | ||
private moduleService; | ||
constructor(properties: Properties, translationService: TranslationService, moduleService: ModuleService); | ||
translate(key: string, params: TranslationParams, lang?: string, orEmpty?: boolean, defaultValue?: string): Promise<string>; | ||
instant(key: string, params: TranslationParams, lang?: string, orEmpty?: any, defaultValue?: string): string; | ||
replace(text: string): Promise<ReplacedType>; | ||
wrap(key: string, params?: TranslationParams, defaultValue?: string | undefined): string; | ||
private getTranslatedWithMetadata; | ||
instant(key: string, params: TranslationParams, lang?: string, orEmpty?: boolean, defaultValue?: string): string; | ||
private formatTranslation; | ||
private readonly format; | ||
private readonly escapeForRegExp; | ||
private readonly escapeParam; | ||
} |
@@ -37,40 +37,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
import { TextHelper } from '../helpers/TextHelper'; | ||
import IntlMessageFormat from 'intl-messageformat'; | ||
var TextService = /** @class */ (function () { | ||
function TextService(properties, translationService) { | ||
function TextService(properties, translationService, moduleService) { | ||
var _this = this; | ||
this.properties = properties; | ||
this.translationService = translationService; | ||
this.format = function (translation, params) { | ||
this.moduleService = moduleService; | ||
this.format = function (translation, params, lang) { | ||
try { | ||
//try to format the text | ||
return new IntlMessageFormat(translation, _this.properties.currentLanguage).format(params); | ||
// try to format the text | ||
var result = translation; | ||
if (_this.moduleService.formatter) { | ||
result = _this.moduleService.formatter.format({ | ||
translation: result, | ||
params: params, | ||
language: lang || _this.properties.currentLanguage, | ||
}); | ||
} | ||
return result; | ||
} | ||
@@ -83,117 +63,13 @@ catch (e) { | ||
}; | ||
this.escapeForRegExp = function (string) { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
}; | ||
this.escapeParam = function (param) { | ||
if (typeof param === 'string') { | ||
return param.replace(/[,:\\]/gs, '\\$&'); | ||
} | ||
if (typeof param === 'number' || typeof param === 'bigint') { | ||
return param.toString(); | ||
} | ||
// eslint-disable-next-line no-console | ||
console.warn(param); | ||
// eslint-disable-next-line no-console | ||
console.warn('Unsupported value type of above param. Consider converting to string.'); | ||
return param; | ||
}; | ||
} | ||
Object.defineProperty(TextService.prototype, "rawUnWrapRegex", { | ||
get: function () { | ||
var escapedPrefix = this.escapeForRegExp(this.properties.config.inputPrefix); | ||
var escapedSuffix = this.escapeForRegExp(this.properties.config.inputSuffix); | ||
return "(\\\\?)(".concat(escapedPrefix, "(.*?)").concat(escapedSuffix, ")"); | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
TextService.parseUnwrapped = function (unwrappedString) { | ||
var e_1, _a; | ||
var escaped = false; | ||
var actual = ''; | ||
var paramName = ''; | ||
var readingState = 'KEY'; | ||
var result = { | ||
key: '', | ||
params: {}, | ||
defaultValue: undefined, | ||
}; | ||
try { | ||
for (var unwrappedString_1 = __values(unwrappedString), unwrappedString_1_1 = unwrappedString_1.next(); !unwrappedString_1_1.done; unwrappedString_1_1 = unwrappedString_1.next()) { | ||
var char = unwrappedString_1_1.value; | ||
if (char === '\\' && !escaped) { | ||
escaped = true; | ||
continue; | ||
} | ||
if (escaped) { | ||
escaped = false; | ||
actual += char; | ||
continue; | ||
} | ||
if (readingState === 'KEY' && char === ',') { | ||
readingState = 'DEFAULT_VALUE'; | ||
result.key = actual; | ||
actual = ''; | ||
continue; | ||
} | ||
if (readingState === 'KEY' && char === ':') { | ||
readingState = 'PARAM_NAME'; | ||
result.key = actual; | ||
actual = ''; | ||
continue; | ||
} | ||
if (readingState === 'DEFAULT_VALUE' && char === ':') { | ||
readingState = 'PARAM_NAME'; | ||
result.defaultValue = actual; | ||
actual = ''; | ||
continue; | ||
} | ||
if (readingState === 'PARAM_NAME' && char === ':') { | ||
readingState = 'PARAM_VALUE'; | ||
paramName = actual; | ||
actual = ''; | ||
continue; | ||
} | ||
if (readingState === 'PARAM_VALUE' && char === ',') { | ||
readingState = 'PARAM_NAME'; | ||
result.params[paramName] = actual; | ||
actual = ''; | ||
continue; | ||
} | ||
actual += char; | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (unwrappedString_1_1 && !unwrappedString_1_1.done && (_a = unwrappedString_1.return)) _a.call(unwrappedString_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
if (readingState === 'KEY') { | ||
result.key = actual; | ||
} | ||
if (readingState === 'DEFAULT_VALUE') { | ||
result.defaultValue = actual; | ||
} | ||
if (readingState === 'PARAM_VALUE') { | ||
result.params[paramName] = actual; | ||
} | ||
return result; | ||
}; | ||
TextService.prototype.translate = function (key, params, lang, orEmpty, defaultValue) { | ||
if (lang === void 0) { lang = this.properties.currentLanguage; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = this.format; | ||
return [4 /*yield*/, this.translationService.getTranslation(key, lang, orEmpty, defaultValue)]; | ||
var translation; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.translationService.getTranslation(key, lang, defaultValue)]; | ||
case 1: | ||
result = _a.apply(this, [_b.sent(), params]); | ||
if (result !== undefined) { | ||
return [2 /*return*/, result]; | ||
} | ||
return [2 /*return*/, key]; | ||
translation = _a.sent(); | ||
return [2 /*return*/, this.formatTranslation(key, translation, params, lang, orEmpty)]; | ||
} | ||
@@ -205,84 +81,14 @@ }); | ||
if (lang === void 0) { lang = this.properties.currentLanguage; } | ||
var result = this.format(this.translationService.getFromCacheOrFallback(key, lang, orEmpty, defaultValue), params); | ||
if (result !== undefined) { | ||
return result; | ||
var translation = this.translationService.getFromCacheOrFallback(key, lang, defaultValue); | ||
return this.formatTranslation(key, translation, params, lang, orEmpty); | ||
}; | ||
TextService.prototype.formatTranslation = function (key, translation, params, lang, orEmpty) { | ||
if (translation !== undefined) { | ||
return this.format(translation, params, lang); | ||
} | ||
return key; | ||
if (!orEmpty) { | ||
return key; | ||
} | ||
return ''; | ||
}; | ||
TextService.prototype.replace = function (text) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var matchRegexp, keysAndParams, matched, match, start, result, pre, _a, fullMatch, _, wrapped, unwrapped, index, input, translated; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
matchRegexp = new RegExp(this.rawUnWrapRegex, 'gs'); | ||
keysAndParams = []; | ||
matched = false; | ||
start = 0; | ||
result = ''; | ||
_b.label = 1; | ||
case 1: | ||
if (!((match = matchRegexp.exec(text)) !== null)) return [3 /*break*/, 3]; | ||
pre = match[1]; | ||
_a = __read(match, 4), fullMatch = _a[0], _ = _a[1], wrapped = _a[2], unwrapped = _a[3]; | ||
index = match.index, input = match.input; | ||
result += input.substr(start, index - start); | ||
start = index + fullMatch.length; | ||
if (pre === '\\') { | ||
if (!TextHelper.isCharEscaped(index, text)) { | ||
result += wrapped; | ||
return [3 /*break*/, 1]; | ||
} | ||
pre = ''; | ||
} | ||
return [4 /*yield*/, this.getTranslatedWithMetadata(unwrapped)]; | ||
case 2: | ||
translated = _b.sent(); | ||
keysAndParams.push({ | ||
key: translated.key, | ||
params: translated.params, | ||
defaultValue: translated.defaultValue, | ||
}); | ||
matched = true; | ||
result += pre + translated.translated; | ||
return [3 /*break*/, 1]; | ||
case 3: | ||
result += text.substring(start); | ||
if (matched) { | ||
return [2 /*return*/, { text: result, keys: keysAndParams }]; | ||
} | ||
return [2 /*return*/, undefined]; | ||
} | ||
}); | ||
}); | ||
}; | ||
TextService.prototype.wrap = function (key, params, defaultValue) { | ||
var _this = this; | ||
if (params === void 0) { params = {}; } | ||
if (defaultValue === void 0) { defaultValue = undefined; } | ||
var paramString = Object.entries(params) | ||
.map(function (_a) { | ||
var _b = __read(_a, 2), name = _b[0], value = _b[1]; | ||
return "".concat(_this.escapeParam(name), ":").concat(_this.escapeParam(value)); | ||
}) | ||
.join(','); | ||
paramString = paramString.length ? ":".concat(paramString) : ''; | ||
var defaultString = defaultValue !== undefined ? ",".concat(this.escapeParam(defaultValue)) : ''; | ||
return "".concat(this.properties.config.inputPrefix).concat(this.escapeParam(key)).concat(defaultString).concat(paramString).concat(this.properties.config.inputSuffix); | ||
}; | ||
TextService.prototype.getTranslatedWithMetadata = function (text) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, key, params, defaultValue, translated; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = TextService.parseUnwrapped(text), key = _a.key, params = _a.params, defaultValue = _a.defaultValue; | ||
return [4 /*yield*/, this.translate(key, params, undefined, false, defaultValue)]; | ||
case 1: | ||
translated = _b.sent(); | ||
return [2 /*return*/, { translated: translated, key: key, params: params, defaultValue: defaultValue }]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return TextService; | ||
@@ -289,0 +95,0 @@ }()); |
@@ -0,1 +1,2 @@ | ||
import { Translations } from '../types'; | ||
import { Properties } from '../Properties'; | ||
@@ -24,5 +25,6 @@ import { CoreService } from './CoreService'; | ||
initStatic(): void; | ||
getChachedTranslations(): Map<string, Translations>; | ||
updateTranslationInCache: (data: Key) => Promise<void>; | ||
loadTranslations(lang?: string): Promise<void>; | ||
getTranslation(key: string, lang?: string, orEmpty?: boolean, defaultValue?: string): Promise<string>; | ||
getTranslation(key: string, lang?: string, defaultValue?: string): Promise<string>; | ||
updateKeyComplex(id: number, data: ComplexEditKeyDto): Promise<KeyWithDataModel>; | ||
@@ -39,3 +41,3 @@ createKey(data: CreateKeyDto): Promise<KeyWithDataModel>; | ||
changeTranslations({ key, translations, }: TranslationData): Promise<() => void>; | ||
getFromCacheOrFallback(key: string, lang?: string, orEmpty?: boolean, defaultValue?: string): string; | ||
getFromCacheOrFallback(key: string, lang?: string, defaultValue?: string): string; | ||
getTranslationsOfKey: (key: string, languages?: Set<string>) => Promise<readonly [{ | ||
@@ -42,0 +44,0 @@ keyId: number; |
@@ -164,3 +164,3 @@ var __assign = (this && this.__assign) || function () { | ||
} | ||
TranslationService.translationByValue = function (message, key, orEmpty, defaultValue) { | ||
TranslationService.translationByValue = function (message, defaultValue) { | ||
if (message) { | ||
@@ -172,6 +172,3 @@ return message; | ||
} | ||
if (orEmpty) { | ||
return ''; | ||
} | ||
return key; | ||
return undefined; | ||
}; | ||
@@ -192,2 +189,5 @@ TranslationService.prototype.initStatic = function () { | ||
}; | ||
TranslationService.prototype.getChachedTranslations = function () { | ||
return this.translationsCache; | ||
}; | ||
TranslationService.prototype.loadTranslations = function (lang) { | ||
@@ -215,5 +215,4 @@ if (lang === void 0) { lang = this.properties.currentLanguage; } | ||
}; | ||
TranslationService.prototype.getTranslation = function (key, lang, orEmpty, defaultValue) { | ||
TranslationService.prototype.getTranslation = function (key, lang, defaultValue) { | ||
if (lang === void 0) { lang = this.properties.currentLanguage; } | ||
if (orEmpty === void 0) { orEmpty = false; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -238,3 +237,3 @@ var message; | ||
_a.label = 3; | ||
case 3: return [2 /*return*/, TranslationService.translationByValue(message, key, orEmpty, defaultValue)]; | ||
case 3: return [2 /*return*/, TranslationService.translationByValue(message, defaultValue)]; | ||
} | ||
@@ -368,8 +367,7 @@ }); | ||
}; | ||
TranslationService.prototype.getFromCacheOrFallback = function (key, lang, orEmpty, defaultValue) { | ||
TranslationService.prototype.getFromCacheOrFallback = function (key, lang, defaultValue) { | ||
if (lang === void 0) { lang = this.properties.currentLanguage; } | ||
if (orEmpty === void 0) { orEmpty = false; } | ||
var message = this.getFromCache(key, lang) || | ||
this.getFromCache(key, this.properties.config.fallbackLanguage); | ||
return TranslationService.translationByValue(message, key, orEmpty, defaultValue); | ||
return TranslationService.translationByValue(message, defaultValue); | ||
}; | ||
@@ -376,0 +374,0 @@ TranslationService.prototype.fetchTranslations = function (lang) { |
import { TolgeeConfig } from './TolgeeConfig'; | ||
import { InstantProps, TranslateProps, TranslationParams } from './types'; | ||
import { InstantProps, TolgeeModule, TranslateProps, TranslationParams } from './types'; | ||
export declare class Tolgee { | ||
private dependencyStore; | ||
constructor(config: TolgeeConfig); | ||
private dependencyService; | ||
static use(module: TolgeeModule): Tolgee; | ||
static init(config: TolgeeConfig): Tolgee; | ||
private constructor(); | ||
use(module: TolgeeModule): this; | ||
init(config: TolgeeConfig): this; | ||
get properties(): import("./Properties").Properties; | ||
@@ -26,2 +30,3 @@ get translationService(): import("./services/TranslationService").TranslationService; | ||
translate(key: string, params?: TranslationParams, noWrap?: boolean, defaultValue?: string): Promise<string>; | ||
wrap(key: string, params?: TranslationParams, defaultValue?: string | undefined, translation?: string): string; | ||
instant(key: string, params?: TranslationParams, noWrap?: boolean, orEmpty?: boolean, defaultValue?: string): string; | ||
@@ -32,2 +37,1 @@ instant(props: InstantProps): string; | ||
} | ||
export default Tolgee; |
@@ -37,18 +37,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { TolgeeConfig } from './TolgeeConfig'; | ||
import { NodeHelper } from './helpers/NodeHelper'; | ||
import { DependencyStore } from './services/DependencyStore'; | ||
import { DependencyService } from './services/DependencyService'; | ||
var Tolgee = /** @class */ (function () { | ||
function Tolgee(config) { | ||
function Tolgee() { | ||
var _this = this; | ||
this.stop = function () { | ||
_this.dependencyStore.observer.stopObserving(); | ||
_this.dependencyStore.elementRegistrar.cleanAll(); | ||
NodeHelper.unmarkElementAsTargetElement(_this.properties.config.targetElement); | ||
_this.dependencyService.stop(); | ||
}; | ||
this.dependencyStore = new DependencyStore(new TolgeeConfig(config)); | ||
this.dependencyService = new DependencyService(); | ||
} | ||
Tolgee.use = function (module) { | ||
return new Tolgee().use(module); | ||
}; | ||
Tolgee.init = function (config) { | ||
return new Tolgee().init(config); | ||
}; | ||
Tolgee.prototype.use = function (module) { | ||
this.dependencyService.moduleService.addModule(module); | ||
return this; | ||
}; | ||
Tolgee.prototype.init = function (config) { | ||
this.dependencyService.init(config); | ||
return this; | ||
}; | ||
Object.defineProperty(Tolgee.prototype, "properties", { | ||
get: function () { | ||
return this.dependencyStore.properties; | ||
return this.dependencyService.properties; | ||
}, | ||
@@ -60,3 +70,3 @@ enumerable: false, | ||
get: function () { | ||
return this.dependencyStore.translationService; | ||
return this.dependencyService.translationService; | ||
}, | ||
@@ -68,3 +78,3 @@ enumerable: false, | ||
get: function () { | ||
return this.dependencyStore.coreService; | ||
return this.dependencyService.coreService; | ||
}, | ||
@@ -80,3 +90,3 @@ enumerable: false, | ||
this.properties.currentLanguage = value; | ||
this.dependencyStore.eventService | ||
this.dependencyService.eventService | ||
.LANGUAGE_CHANGED.emit(value); | ||
@@ -96,3 +106,3 @@ }, | ||
get: function () { | ||
return this.dependencyStore.eventService.LANGUAGE_CHANGED; | ||
return this.dependencyService.eventService.LANGUAGE_CHANGED; | ||
}, | ||
@@ -104,3 +114,3 @@ enumerable: false, | ||
get: function () { | ||
return this.dependencyStore.eventService.TRANSLATION_CHANGED; | ||
return this.dependencyService.eventService.TRANSLATION_CHANGED; | ||
}, | ||
@@ -112,3 +122,3 @@ enumerable: false, | ||
get: function () { | ||
return this.dependencyStore.eventService.LANGUAGE_LOADED; | ||
return this.dependencyService.eventService.LANGUAGE_LOADED; | ||
}, | ||
@@ -148,2 +158,3 @@ enumerable: false, | ||
case 0: | ||
this.dependencyService.run(); | ||
if (!(this.properties.config.mode === 'development')) return [3 /*break*/, 2]; | ||
@@ -157,3 +168,3 @@ return [4 /*yield*/, this.loadApiKeyDetails()]; | ||
_a.sent(); | ||
return [4 /*yield*/, this.dependencyStore.pluginManager.run()]; | ||
return [4 /*yield*/, this.dependencyService.pluginManager.run()]; | ||
case 4: | ||
@@ -170,3 +181,3 @@ _a.sent(); | ||
if (this.properties.config.watch) { | ||
this.dependencyStore.observer.observe(); | ||
this.dependencyService.observer.observe(); | ||
} | ||
@@ -181,3 +192,3 @@ return [2 /*return*/]; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this.dependencyStore.coreHandler.handleSubtree(this.properties.config.targetElement)]; | ||
return [2 /*return*/, this.dependencyService.wrapper.handleSubtree(this.properties.config.targetElement)]; | ||
}); | ||
@@ -191,3 +202,3 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var key, orEmpty, props; | ||
var key, orEmpty, props, translation; | ||
return __generator(this, function (_a) { | ||
@@ -208,3 +219,3 @@ switch (_a.label) { | ||
} | ||
if (!(this.properties.config.mode === 'development' && !noWrap)) return [3 /*break*/, 3]; | ||
if (!(this.properties.config.mode === 'development' && !noWrap)) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, this.loadApiKeyDetails()]; | ||
@@ -216,4 +227,7 @@ case 1: | ||
_a.sent(); | ||
return [2 /*return*/, this.dependencyStore.textService.wrap(key, params, defaultValue)]; | ||
case 3: return [2 /*return*/, this.dependencyStore.textService.translate(key, params, undefined, orEmpty, defaultValue)]; | ||
return [4 /*yield*/, this.dependencyService.textService.translate(key, params, undefined, orEmpty, defaultValue)]; | ||
case 3: | ||
translation = _a.sent(); | ||
return [2 /*return*/, this.dependencyService.wrapper.wrap(key, params, defaultValue, translation)]; | ||
case 4: return [2 /*return*/, this.dependencyService.textService.translate(key, params, undefined, orEmpty, defaultValue)]; | ||
} | ||
@@ -223,2 +237,10 @@ }); | ||
}; | ||
Tolgee.prototype.wrap = function (key, params, defaultValue, translation) { | ||
if (this.properties.config.mode === 'development') { | ||
return this.dependencyService.wrapper.wrap(key, params, defaultValue, translation); | ||
} | ||
else { | ||
return translation || defaultValue; | ||
} | ||
}; | ||
Tolgee.prototype.instant = function (keyOrProps, params, noWrap, orEmpty, defaultValue) { | ||
@@ -238,6 +260,7 @@ if (params === void 0) { params = {}; } | ||
} | ||
var translation = this.dependencyService.textService.instant(key, params, undefined, orEmpty, defaultValue); | ||
if (this.properties.config.mode === 'development' && !noWrap) { | ||
return this.dependencyStore.textService.wrap(key, params, defaultValue); | ||
return this.dependencyService.wrapper.wrap(key, params, defaultValue, translation); | ||
} | ||
return this.dependencyStore.textService.instant(key, params, undefined, orEmpty, defaultValue); | ||
return translation; | ||
}; | ||
@@ -251,3 +274,3 @@ Tolgee.prototype.loadApiKeyDetails = function () { | ||
if (!(this.properties.scopes === undefined)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.dependencyStore.coreService.getApiKeyDetails()]; | ||
return [4 /*yield*/, this.dependencyService.coreService.getApiKeyDetails()]; | ||
case 1: | ||
@@ -266,3 +289,2 @@ details = _a.sent(); | ||
export { Tolgee }; | ||
export default Tolgee; | ||
//# sourceMappingURL=Tolgee.js.map |
@@ -28,2 +28,3 @@ import { Mode, TreeTranslationsData } from './types'; | ||
}; | ||
wrapperMode?: 'text' | 'invisible'; | ||
/** | ||
@@ -30,0 +31,0 @@ * When true, fallback language will be preloaded on Tolgee.run |
@@ -25,2 +25,3 @@ import { NodeHelper } from './helpers/NodeHelper'; | ||
this.highlightColor = 'rgb(224 240 255)'; | ||
this.wrapperMode = 'text'; | ||
/** | ||
@@ -27,0 +28,0 @@ * When true, fallback language will be preloaded on Tolgee.run |
@@ -79,1 +79,19 @@ export interface TextInputElementData { | ||
export declare type Mode = 'development' | 'production'; | ||
export declare type Unwrapped = { | ||
text: string; | ||
keys: KeyAndParams[]; | ||
}; | ||
export interface Formatter { | ||
format: FormatFunction; | ||
} | ||
interface FormatterStatic { | ||
type: 'formatter'; | ||
new (): Formatter; | ||
} | ||
export declare type TolgeeModule = FormatterStatic; | ||
export declare type FormatFunction = (props: { | ||
translation: string; | ||
params: Record<string, any>; | ||
language: string; | ||
}) => string; | ||
export {}; |
{ | ||
"name": "@tolgee/core", | ||
"version": "2.8.2", | ||
"version": "3.0.0", | ||
"description": "Library providing ability to translate messages directly in context of developed application.", | ||
@@ -27,7 +27,8 @@ "main": "index.js", | ||
"scripts": { | ||
"build": "rollup -c rollup.config.js && tsc", | ||
"build": "rollup -c rollup.config.js && tsc --project tsconfig.prod.json", | ||
"test": "jest --config ./jest.config.js", | ||
"develop": "concurrently \"rollup -c rollup.config.js -w\" \"tsc --watch\" ", | ||
"develop": "concurrently \"rollup -c rollup.config.js -w\" \"tsc --watch --project tsconfig.prod.json\" ", | ||
"schema": "openapi-typescript http://localhost:8080/v3/api-docs/All%20Internal%20-%20for%20Tolgee%20Web%20application --output ./src/types/apiSchema.generated.ts", | ||
"typedoc": "typedoc --entryPointStrategy expand --out docs src/index.ts src/Tolgee.ts src/types.ts src/services/EventEmitter.ts src/services/Subscription.ts" | ||
"typedoc": "typedoc --entryPointStrategy expand --out docs src/index.ts src/Tolgee.ts src/types.ts src/services/EventEmitter.ts src/services/Subscription.ts", | ||
"tsc": "tsc --noEmit" | ||
}, | ||
@@ -62,3 +63,3 @@ "author": "JanCizmar", | ||
}, | ||
"gitHead": "865e02ac0676521dc91d141509abb2fb67a10723", | ||
"gitHead": "ef08fb08a98f2240a1bd42e9ae4bdcc0a3872262", | ||
"publishConfig": { | ||
@@ -65,0 +66,0 @@ "access": "public" |
# Tolgee core library | ||
![test workflow](https://github.com/tolgee/tolgee-js/actions/workflows/test.yml/badge.svg) | ||
@@ -8,4 +9,2 @@ ![@tolgee/core version](https://img.shields.io/npm/v/@tolgee/core?label=%40tolgee%2Fcore) | ||
[<img src="https://raw.githubusercontent.com/tolgee/documentation/main/tolgee_logo_text.svg" alt="Tolgee" width="200" />](https://tolgee.io) | ||
@@ -26,3 +25,3 @@ | ||
const tg = new Tolgee({ | ||
const tg = Tolgee.init({ | ||
apiKey: "your_api_key", | ||
@@ -41,2 +40,3 @@ apiUrl: "https://app.tolgee.io", | ||
## Features | ||
- All in One localization solution for your JS application 🙌 | ||
@@ -43,0 +43,0 @@ - Out of box in-context localization 🎉 |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
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
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
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
18
3238407
161
21951