piral-translate
Advanced tools
Comparing version 0.8.0-pre.672 to 0.8.0-pre.678
@@ -0,3 +1,4 @@ | ||
import { Extend } from 'piral-core'; | ||
import { Localizer } from './localize'; | ||
import { PiralLocaleApi, LocaleConfig } from './types'; | ||
import { PiletLocaleApi, LocaleConfig } from './types'; | ||
/** | ||
@@ -10,4 +11,4 @@ * Sets up a new localizer by using the given config. | ||
* Creates a new Piral localization API extension. | ||
* @param config The configuration to use. | ||
* @param localizer The specific localizer to be used, if any. | ||
*/ | ||
export declare function createLocaleApi(localizer: Localizer): PiralLocaleApi; | ||
export declare function createLocaleApi(localizer?: Localizer): Extend<PiletLocaleApi>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_atom_1 = require("@dbeining/react-atom"); | ||
const actions_1 = require("./actions"); | ||
const localize_1 = require("./localize"); | ||
@@ -16,16 +18,25 @@ /** | ||
* Creates a new Piral localization API extension. | ||
* @param config The configuration to use. | ||
* @param localizer The specific localizer to be used, if any. | ||
*/ | ||
function createLocaleApi(localizer) { | ||
let localTranslations = {}; | ||
return { | ||
setTranslations(messages) { | ||
localTranslations = messages; | ||
}, | ||
getTranslations() { | ||
return localTranslations; | ||
}, | ||
translate(tag, variables) { | ||
return localizer.localizeLocal(localTranslations, tag, variables); | ||
}, | ||
function createLocaleApi(localizer = setupLocalizer()) { | ||
return context => { | ||
context.defineActions(actions_1.createActions(localizer)); | ||
react_atom_1.swap(context.state, state => (Object.assign({}, state, { language: { | ||
available: [localizer.language], | ||
selected: localizer.language, | ||
} }))); | ||
return () => { | ||
let localTranslations = {}; | ||
return { | ||
setTranslations(messages) { | ||
localTranslations = messages; | ||
}, | ||
getTranslations() { | ||
return localTranslations; | ||
}, | ||
translate(tag, variables) { | ||
return localizer.localizeLocal(localTranslations, tag, variables); | ||
}, | ||
}; | ||
}; | ||
}; | ||
@@ -32,0 +43,0 @@ } |
@@ -0,3 +1,4 @@ | ||
export * from './current'; | ||
export * from './hooks'; | ||
export * from './create'; | ||
export * from './types'; | ||
export * from './localize'; | ||
export * from './create'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./localize"), exports); | ||
tslib_1.__exportStar(require("./current"), exports); | ||
tslib_1.__exportStar(require("./hooks"), exports); | ||
tslib_1.__exportStar(require("./create"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,4 +0,4 @@ | ||
import { LocalizationMessages } from './types'; | ||
import { LocalizationMessages, Localizable } from './types'; | ||
declare function defaultFallback(key: string, language: string): string; | ||
export declare class Localizer { | ||
export declare class Localizer implements Localizable { | ||
messages: LocalizationMessages; | ||
@@ -5,0 +5,0 @@ language: string; |
declare module 'piral-core/lib/types/custom' { | ||
interface PiletCustomApi extends PiralLocaleApi { | ||
interface PiletCustomApi extends PiletLocaleApi { | ||
} | ||
interface PiralCustomState { | ||
/** | ||
* Information for the language display. | ||
*/ | ||
language: { | ||
/** | ||
* The selected, i.e., active, language. | ||
*/ | ||
selected: string; | ||
/** | ||
* The available languages. | ||
*/ | ||
available: Array<string>; | ||
}; | ||
} | ||
interface PiralCustomActions { | ||
/** | ||
* Changes the selected language. | ||
* @param selected The selected language. | ||
*/ | ||
selectLanguage(selected: string): void; | ||
/** | ||
* Gets the translation for the given key at the current | ||
* language. | ||
*/ | ||
translate(tag: string, variables?: Record<string, string>): string; | ||
/** | ||
* Sets the translations (both global and local) for | ||
* the given language. | ||
* @param language The language to set the translations for. | ||
* @param data The global and local translations. | ||
*/ | ||
setTranslations(language: string, data: LanguageData): void; | ||
/** | ||
* Gets the translations (both global and local) for | ||
* the given language. | ||
* @param language The language to get the translations for. | ||
* @result The global and local translations. | ||
*/ | ||
getTranslations(language: string): LanguageData; | ||
} | ||
} | ||
export interface LanguageLoader { | ||
(language: string, current: LanguageData): Promise<LanguageData>; | ||
} | ||
export interface LanguageData { | ||
global: Translations; | ||
locals: Array<{ | ||
name: string; | ||
value: Translations; | ||
}>; | ||
} | ||
export interface Localizable { | ||
messages: LocalizationMessages; | ||
language: string; | ||
localizeGlobal<T>(key: string, variables?: T): string; | ||
localizeLocal<T>(localMessages: LocalizationMessages, key: string, variables?: T): string; | ||
} | ||
export interface Translations { | ||
@@ -20,3 +77,3 @@ /** | ||
} | ||
export interface PiralLocaleApi { | ||
export interface PiletLocaleApi { | ||
/** | ||
@@ -23,0 +80,0 @@ * Translates the given tag (using the optional variables) into a string using the current language. |
{ | ||
"name": "piral-translate", | ||
"version": "0.8.0-pre.672", | ||
"version": "0.8.0-pre.678", | ||
"description": "Introduces the translate API extension for extending piral-core.", | ||
@@ -40,10 +40,10 @@ "keywords": [ | ||
"devDependencies": { | ||
"piral-core": "^0.8.0-pre.672" | ||
"piral-core": "^0.8.0-pre.678" | ||
}, | ||
"peerDependencies": { | ||
"piral-core": "^0.3.0 || ^0.4.0 || ^0.5.0 || ^0.6.0 || ^0.7.0", | ||
"piral-core": "^0.8.0 || ^0.9.0", | ||
"react": ">= 16.8.0", | ||
"react-dom": ">= 16.8.0" | ||
}, | ||
"gitHead": "f8016114d252a352f8c8b09c3f437e12aab133e1" | ||
"gitHead": "6cd1ad5edc469f7e08f91767424ba288ea15bd57" | ||
} |
@@ -0,4 +1,10 @@ | ||
import { Atom } from '@dbeining/react-atom'; | ||
import { createLocaleApi, setupLocalizer } from './create'; | ||
describe('Create Localize API', () => { | ||
const context: any = { | ||
defineActions() {}, | ||
state: Atom.of({}), | ||
}; | ||
it('createApi can translate from global translations using the current language', () => { | ||
@@ -14,3 +20,3 @@ const config = { | ||
}; | ||
const api = createLocaleApi(setupLocalizer(config)); | ||
const api = (createLocaleApi(setupLocalizer(config))(context) as any)(); | ||
const result = api.translate('foo'); | ||
@@ -30,3 +36,3 @@ expect(result).toEqual('bár'); | ||
}; | ||
const api = createLocaleApi(setupLocalizer(config)); | ||
const api = (createLocaleApi(setupLocalizer(config))(context) as any)(); | ||
api.setTranslations({ | ||
@@ -51,3 +57,3 @@ fr: { | ||
}; | ||
const api = createLocaleApi(setupLocalizer(config)); | ||
const api = (createLocaleApi(setupLocalizer(config))(context) as any)(); | ||
api.setTranslations({ | ||
@@ -72,3 +78,3 @@ fr: { | ||
}; | ||
const api = createLocaleApi(setupLocalizer(config)); | ||
const api = (createLocaleApi(setupLocalizer(config))(context) as any)(); | ||
const result = api.translate('bar'); | ||
@@ -88,3 +94,3 @@ expect(result).toEqual('__en_bar__'); | ||
}; | ||
const api = createLocaleApi(setupLocalizer(config)); | ||
const api = (createLocaleApi(setupLocalizer(config))(context) as any)(); | ||
const result = api.translate('qxz'); | ||
@@ -91,0 +97,0 @@ expect(result).toEqual('__fr_qxz__'); |
@@ -0,3 +1,6 @@ | ||
import { swap } from '@dbeining/react-atom'; | ||
import { Extend } from 'piral-core'; | ||
import { createActions } from './actions'; | ||
import { Localizer } from './localize'; | ||
import { PiralLocaleApi, LocalizationMessages, LocaleConfig } from './types'; | ||
import { PiletLocaleApi, LocalizationMessages, LocaleConfig } from './types'; | ||
@@ -16,17 +19,32 @@ /** | ||
* Creates a new Piral localization API extension. | ||
* @param config The configuration to use. | ||
* @param localizer The specific localizer to be used, if any. | ||
*/ | ||
export function createLocaleApi(localizer: Localizer): PiralLocaleApi { | ||
let localTranslations: LocalizationMessages = {}; | ||
return { | ||
setTranslations(messages) { | ||
localTranslations = messages; | ||
}, | ||
getTranslations() { | ||
return localTranslations; | ||
}, | ||
translate(tag, variables) { | ||
return localizer.localizeLocal(localTranslations, tag, variables); | ||
}, | ||
}; | ||
export function createLocaleApi(localizer = setupLocalizer()): Extend<PiletLocaleApi> { | ||
return context => { | ||
context.defineActions(createActions(localizer)); | ||
swap(context.state, state => ({ | ||
...state, | ||
language: { | ||
available: [localizer.language], | ||
selected: localizer.language, | ||
}, | ||
})); | ||
return () => { | ||
let localTranslations: LocalizationMessages = {}; | ||
return { | ||
setTranslations(messages) { | ||
localTranslations = messages; | ||
}, | ||
getTranslations() { | ||
return localTranslations; | ||
}, | ||
translate(tag, variables) { | ||
return localizer.localizeLocal(localTranslations, tag, variables); | ||
}, | ||
}; | ||
}; | ||
} | ||
} |
@@ -0,3 +1,4 @@ | ||
export * from './current'; | ||
export * from './hooks'; | ||
export * from './create'; | ||
export * from './types'; | ||
export * from './localize'; | ||
export * from './create'; |
@@ -1,2 +0,2 @@ | ||
import { LocalizationMessages } from './types'; | ||
import { LocalizationMessages, Localizable } from './types'; | ||
@@ -22,3 +22,3 @@ function defaultFallback(key: string, language: string): string { | ||
export class Localizer { | ||
export class Localizer implements Localizable { | ||
/** | ||
@@ -25,0 +25,0 @@ * Creates a new instance of a localizer. |
import {} from 'piral-core'; | ||
declare module 'piral-core/lib/types/custom' { | ||
interface PiletCustomApi extends PiralLocaleApi {} | ||
interface PiletCustomApi extends PiletLocaleApi {} | ||
interface PiralCustomState { | ||
/** | ||
* Information for the language display. | ||
*/ | ||
language: { | ||
/** | ||
* The selected, i.e., active, language. | ||
*/ | ||
selected: string; | ||
/** | ||
* The available languages. | ||
*/ | ||
available: Array<string>; | ||
}; | ||
} | ||
interface PiralCustomActions { | ||
/** | ||
* Changes the selected language. | ||
* @param selected The selected language. | ||
*/ | ||
selectLanguage(selected: string): void; | ||
/** | ||
* Gets the translation for the given key at the current | ||
* language. | ||
*/ | ||
translate(tag: string, variables?: Record<string, string>): string; | ||
/** | ||
* Sets the translations (both global and local) for | ||
* the given language. | ||
* @param language The language to set the translations for. | ||
* @param data The global and local translations. | ||
*/ | ||
setTranslations(language: string, data: LanguageData): void; | ||
/** | ||
* Gets the translations (both global and local) for | ||
* the given language. | ||
* @param language The language to get the translations for. | ||
* @result The global and local translations. | ||
*/ | ||
getTranslations(language: string): LanguageData; | ||
} | ||
} | ||
export interface LanguageLoader { | ||
(language: string, current: LanguageData): Promise<LanguageData>; | ||
} | ||
export interface LanguageData { | ||
global: Translations; | ||
locals: Array<{ | ||
name: string; | ||
value: Translations; | ||
}>; | ||
} | ||
export interface Localizable { | ||
messages: LocalizationMessages; | ||
language: string; | ||
localizeGlobal<T>(key: string, variables?: T): string; | ||
localizeLocal<T>(localMessages: LocalizationMessages, key: string, variables?: T): string; | ||
} | ||
export interface Translations { | ||
@@ -25,3 +87,3 @@ /** | ||
export interface PiralLocaleApi { | ||
export interface PiletLocaleApi { | ||
/** | ||
@@ -28,0 +90,0 @@ * Translates the given tag (using the optional variables) into a string using the current language. |
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
42550
35
952