piral-ext
Advanced tools
Comparing version 0.6.0-pre.524 to 0.6.0-pre.525
@@ -11,3 +11,3 @@ "use strict"; | ||
const lang = config.language || Object.keys(msgs)[0] || 'en'; | ||
return new localize_1.Localizer(msgs, lang, config.fallback, config.load); | ||
return new localize_1.Localizer(msgs, lang, config.fallback); | ||
} | ||
@@ -22,5 +22,8 @@ exports.setupLocalizer = setupLocalizer; | ||
return { | ||
provideTranslations(messages) { | ||
setTranslations(messages) { | ||
localTranslations = messages; | ||
}, | ||
getTranslations() { | ||
return localTranslations; | ||
}, | ||
translate(tag, variables) { | ||
@@ -27,0 +30,0 @@ return localizer.localizeLocal(localTranslations, tag, variables); |
@@ -1,22 +0,12 @@ | ||
import { LocalizationMessages, TranslationLoader } from './types'; | ||
import { LocalizationMessages } from './types'; | ||
declare function defaultFallback(key: string, language: string): string; | ||
export declare class Localizer { | ||
private globalMessages; | ||
private language; | ||
messages: LocalizationMessages; | ||
language: string; | ||
private fallback; | ||
private load?; | ||
/** | ||
* Creates a new instance of a localizer. | ||
*/ | ||
constructor(globalMessages: LocalizationMessages, language: string, fallback?: typeof defaultFallback, load?: TranslationLoader); | ||
constructor(messages: LocalizationMessages, language: string, fallback?: typeof defaultFallback); | ||
/** | ||
* Gets the currently set language. | ||
*/ | ||
readonly currentLanguage: string; | ||
/** | ||
* Changes the currently set language. | ||
* @param language The language to change to. | ||
*/ | ||
changeLanguage(language: string): void; | ||
/** | ||
* Localizes the given key via the global translations. | ||
@@ -36,3 +26,4 @@ * @param key The key of the translation snippet. | ||
private localizeBase; | ||
private translateMessage; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function defaultFallback(key, language) { | ||
return `__${language}_${key}__`; | ||
return language ? `__${language}_${key}__` : ''; | ||
} | ||
@@ -11,7 +11,2 @@ function formatMessage(message, variables) { | ||
} | ||
function translateMessage(language, messages, key, variables) { | ||
const translations = language && messages[language]; | ||
const translation = translations && translations[key]; | ||
return translation && (variables ? formatMessage(translation, variables) : translation); | ||
} | ||
class Localizer { | ||
@@ -21,29 +16,8 @@ /** | ||
*/ | ||
constructor(globalMessages, language, fallback = defaultFallback, load) { | ||
this.globalMessages = globalMessages; | ||
constructor(messages, language, fallback = defaultFallback) { | ||
this.messages = messages; | ||
this.language = language; | ||
this.fallback = fallback; | ||
this.load = load; | ||
} | ||
/** | ||
* Gets the currently set language. | ||
*/ | ||
get currentLanguage() { | ||
return this.language; | ||
} | ||
/** | ||
* Changes the currently set language. | ||
* @param language The language to change to. | ||
*/ | ||
changeLanguage(language) { | ||
if (this.language !== language) { | ||
this.language = language; | ||
if (typeof this.load === 'function') { | ||
this.load(language).then(translations => { | ||
this.globalMessages[language] = translations; | ||
}); | ||
} | ||
} | ||
} | ||
/** | ||
* Localizes the given key via the global translations. | ||
@@ -54,5 +28,3 @@ * @param key The key of the translation snippet. | ||
localizeGlobal(key, variables) { | ||
const language = this.language; | ||
const messages = this.globalMessages; | ||
return this.localizeBase(messages, language, key, variables); | ||
return this.localizeBase(key, variables); | ||
} | ||
@@ -67,18 +39,23 @@ /** | ||
localizeLocal(localMessages, key, variables) { | ||
const language = this.language; | ||
const message = translateMessage(language, localMessages, key, variables); | ||
const message = this.translateMessage(localMessages, key, variables); | ||
if (message === undefined) { | ||
return this.localizeBase(this.globalMessages, language, key, variables); | ||
return this.localizeBase(key, variables); | ||
} | ||
return message; | ||
} | ||
localizeBase(messages, language, key, variables) { | ||
const message = translateMessage(language, messages, key, variables); | ||
localizeBase(key, variables) { | ||
const message = this.translateMessage(this.messages, key, variables); | ||
if (message === undefined) { | ||
return this.fallback(key, language); | ||
return this.fallback(key, this.language); | ||
} | ||
return message; | ||
} | ||
translateMessage(messages, key, variables) { | ||
const language = this.language; | ||
const translations = language && messages[language]; | ||
const translation = translations && translations[key]; | ||
return translation && (variables ? formatMessage(translation, variables) : translation); | ||
} | ||
} | ||
exports.Localizer = Localizer; | ||
//# sourceMappingURL=localize.js.map |
@@ -13,5 +13,2 @@ export interface Translations { | ||
} | ||
export interface TranslationLoader { | ||
(language: string): Promise<Translations>; | ||
} | ||
export interface TranslationFallback { | ||
@@ -31,5 +28,9 @@ (key: string, language: string): string; | ||
* The translations will be exlusively used for retrieving translations for the pilet. | ||
* @param messages The messages to use as transslation basis. | ||
* @param messages The messages to use as translation basis. | ||
*/ | ||
provideTranslations(messages: LocalizationMessages): void; | ||
setTranslations(messages: LocalizationMessages): void; | ||
/** | ||
* Gets the currently provided translations by the pilet. | ||
*/ | ||
getTranslations(): LocalizationMessages; | ||
} | ||
@@ -47,6 +48,2 @@ export interface LocaleConfig { | ||
/** | ||
* Sets the default language to use. | ||
*/ | ||
load?: TranslationLoader; | ||
/** | ||
* Sets the optional fallback to use. | ||
@@ -53,0 +50,0 @@ */ |
{ | ||
"name": "piral-ext", | ||
"version": "0.6.0-pre.524", | ||
"version": "0.6.0-pre.525", | ||
"description": "Useful API extensions and component definitions for extending piral-core.", | ||
@@ -36,3 +36,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"piral-core": "^0.6.0-pre.524" | ||
"piral-core": "^0.6.0-pre.525" | ||
}, | ||
@@ -49,3 +49,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "edc94f2987d634a434ff9787d4cfffacca2276cf" | ||
"gitHead": "40f9af975d8c1c0c5401dc5c115b9f1043b8e31e" | ||
} |
@@ -30,3 +30,3 @@ import { createLocaleApi, setupLocalizer } from './create'; | ||
const api = createLocaleApi(setupLocalizer(config)); | ||
api.provideTranslations({ | ||
api.setTranslations({ | ||
fr: { | ||
@@ -51,3 +51,3 @@ foo: 'boo', | ||
const api = createLocaleApi(setupLocalizer(config)); | ||
api.provideTranslations({ | ||
api.setTranslations({ | ||
fr: { | ||
@@ -54,0 +54,0 @@ foo: 'boo', |
@@ -11,3 +11,3 @@ import { Localizer } from './localize'; | ||
const lang = config.language || Object.keys(msgs)[0] || 'en'; | ||
return new Localizer(msgs, lang, config.fallback, config.load); | ||
return new Localizer(msgs, lang, config.fallback); | ||
} | ||
@@ -22,5 +22,8 @@ | ||
return { | ||
provideTranslations(messages) { | ||
setTranslations(messages) { | ||
localTranslations = messages; | ||
}, | ||
getTranslations() { | ||
return localTranslations; | ||
}, | ||
translate(tag, variables) { | ||
@@ -27,0 +30,0 @@ return localizer.localizeLocal(localTranslations, tag, variables); |
@@ -1,5 +0,5 @@ | ||
import { LocalizationMessages, TranslationLoader } from './types'; | ||
import { LocalizationMessages } from './types'; | ||
function defaultFallback(key: string, language: string): string { | ||
return `__${language}_${key}__`; | ||
return language ? `__${language}_${key}__` : ''; | ||
} | ||
@@ -13,8 +13,2 @@ | ||
function translateMessage<T>(language: string, messages: LocalizationMessages, key: string, variables?: T) { | ||
const translations = language && messages[language]; | ||
const translation = translations && translations[key]; | ||
return translation && (variables ? formatMessage(translation, variables) : translation); | ||
} | ||
export class Localizer { | ||
@@ -24,33 +18,5 @@ /** | ||
*/ | ||
constructor( | ||
private globalMessages: LocalizationMessages, | ||
private language: string, | ||
private fallback = defaultFallback, | ||
private load?: TranslationLoader, | ||
) {} | ||
constructor(public messages: LocalizationMessages, public language: string, private fallback = defaultFallback) {} | ||
/** | ||
* Gets the currently set language. | ||
*/ | ||
public get currentLanguage(): string { | ||
return this.language; | ||
} | ||
/** | ||
* Changes the currently set language. | ||
* @param language The language to change to. | ||
*/ | ||
public changeLanguage(language: string) { | ||
if (this.language !== language) { | ||
this.language = language; | ||
if (typeof this.load === 'function') { | ||
this.load(language).then(translations => { | ||
this.globalMessages[language] = translations; | ||
}); | ||
} | ||
} | ||
} | ||
/** | ||
* Localizes the given key via the global translations. | ||
@@ -61,5 +27,3 @@ * @param key The key of the translation snippet. | ||
public localizeGlobal<T>(key: string, variables?: T) { | ||
const language = this.language; | ||
const messages = this.globalMessages; | ||
return this.localizeBase(messages, language, key, variables); | ||
return this.localizeBase(key, variables); | ||
} | ||
@@ -75,7 +39,6 @@ | ||
public localizeLocal<T>(localMessages: LocalizationMessages, key: string, variables?: T) { | ||
const language = this.language; | ||
const message = translateMessage(language, localMessages, key, variables); | ||
const message = this.translateMessage(localMessages, key, variables); | ||
if (message === undefined) { | ||
return this.localizeBase(this.globalMessages, language, key, variables); | ||
return this.localizeBase(key, variables); | ||
} | ||
@@ -86,7 +49,7 @@ | ||
private localizeBase<T>(messages: LocalizationMessages, language: string, key: string, variables?: T) { | ||
const message = translateMessage(language, messages, key, variables); | ||
private localizeBase<T>(key: string, variables?: T) { | ||
const message = this.translateMessage(this.messages, key, variables); | ||
if (message === undefined) { | ||
return this.fallback(key, language); | ||
return this.fallback(key, this.language); | ||
} | ||
@@ -96,2 +59,9 @@ | ||
} | ||
private translateMessage<T>(messages: LocalizationMessages, key: string, variables?: T) { | ||
const language = this.language; | ||
const translations = language && messages[language]; | ||
const translation = translations && translations[key]; | ||
return translation && (variables ? formatMessage(translation, variables) : translation); | ||
} | ||
} |
@@ -15,6 +15,2 @@ export interface Translations { | ||
export interface TranslationLoader { | ||
(language: string): Promise<Translations>; | ||
} | ||
export interface TranslationFallback { | ||
@@ -35,5 +31,9 @@ (key: string, language: string): string; | ||
* The translations will be exlusively used for retrieving translations for the pilet. | ||
* @param messages The messages to use as transslation basis. | ||
* @param messages The messages to use as translation basis. | ||
*/ | ||
provideTranslations(messages: LocalizationMessages): void; | ||
setTranslations(messages: LocalizationMessages): void; | ||
/** | ||
* Gets the currently provided translations by the pilet. | ||
*/ | ||
getTranslations(): LocalizationMessages; | ||
} | ||
@@ -52,6 +52,2 @@ | ||
/** | ||
* Sets the default language to use. | ||
*/ | ||
load?: TranslationLoader; | ||
/** | ||
* Sets the optional fallback to use. | ||
@@ -58,0 +54,0 @@ */ |
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
51779
1247