@ribajs/i18n
Advanced tools
Comparing version 1.5.0 to 1.6.0
{ | ||
"name": "@ribajs/i18n", | ||
"description": "Internationalization module for Riba.js", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"author": "Pascal Garber <pascal@artandcode.studio>", | ||
@@ -28,14 +28,13 @@ "url": "https://github.com/ribajs/riba/tree/master/packages/i18n", | ||
"devDependencies": { | ||
"@babel/cli": "^7.6.4", | ||
"@babel/core": "^7.6.4", | ||
"@babel/plugin-proposal-class-properties": "^7.5.5", | ||
"@babel/cli": "^7.7.0", | ||
"@babel/core": "^7.7.2", | ||
"@babel/plugin-proposal-class-properties": "^7.7.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.6.2", | ||
"@babel/plugin-transform-runtime": "^7.6.2", | ||
"@babel/preset-env": "^7.6.3", | ||
"@babel/preset-typescript": "^7.6.0", | ||
"@types/jquery": "^3.3.31", | ||
"@babel/preset-env": "^7.7.1", | ||
"@babel/preset-typescript": "^7.7.2", | ||
"babel-loader": "^8.0.6", | ||
"babel-plugin-array-includes": "^2.0.3", | ||
"tslint": "^5.20.0", | ||
"typescript": "^3.6.4", | ||
"tslint": "^5.20.1", | ||
"typescript": "^3.7.2", | ||
"webpack": "^4.41.2", | ||
@@ -45,4 +44,4 @@ "webpack-cli": "^3.3.10" | ||
"dependencies": { | ||
"@babel/runtime-corejs2": "^7.6.3", | ||
"@ribajs/core": "^1.5.0" | ||
"@babel/runtime-corejs2": "^7.7.2", | ||
"@ribajs/core": "^1.6.0" | ||
}, | ||
@@ -49,0 +48,0 @@ "bugs": { |
@@ -6,3 +6,3 @@ # Internationalization Module | ||
```bash | ||
npm install --save-dev @ribajs/i18n | ||
npm install --save @ribajs/i18n | ||
``` | ||
@@ -12,6 +12,6 @@ | ||
To regist the module include `import i18nModule from '@ribajs/i18n';` in your `main.ts` file and regist the module with `riba.module.regist(i18nModule(localesService));`: | ||
To regist the module import `import i18nModule from '@ribajs/i18n';` and any `LocalesService` in your `main.ts` file and regist the module with `riba.module.regist(i18nModule(localesService));`: | ||
```ts | ||
import { Riba, Utils } from '@ribajs/core'; | ||
import { coreModule, Riba, Utils } from '@ribajs/core'; | ||
import { LocalesService } from '@ribajs/shopify-tda'; | ||
@@ -22,2 +22,3 @@ import i18nModule from '@ribajs/i18n'; | ||
const model = {}; | ||
riba.module.regist(coreModule); | ||
riba.module.regist(i18nModule(localesService)); | ||
@@ -24,0 +25,0 @@ Utils.domIsReady(() => { |
@@ -1,6 +0,6 @@ | ||
import { Utils, IBinder, BinderWrapper } from '@ribajs/core'; | ||
import { Utils, Binder, BinderWrapper } from '@ribajs/core'; | ||
import { ALocalesService } from '../../services/locales-base.service'; | ||
// see star.binder.ts | ||
export interface IBinderAttributeChangedEvent { | ||
export interface BinderAttributeChangedEvent { | ||
detail: { | ||
@@ -48,7 +48,5 @@ name: string; | ||
el.innerHTML = locale; | ||
} | ||
if (this.customData.attributeName === 'text') { | ||
} else if (this.customData.attributeName === 'text') { | ||
el.innerText = locale; | ||
} | ||
if (this.customData.attributeName === 'value') { | ||
} else if (this.customData.attributeName === 'value') { | ||
// TODO support also: https://github.com/JumpLinkNetwork/tinybind/blob/master/src/binders/basic/value.binder.ts#L51 | ||
@@ -60,4 +58,5 @@ if (this.customData.contenteditable) { | ||
} | ||
} else { | ||
el.setAttribute(this.customData.attributeName, locale); | ||
} | ||
el.setAttribute(this.customData.attributeName, locale); | ||
}; | ||
@@ -69,5 +68,5 @@ | ||
this.customData.vars = Utils.concat(true, this.customData.vars, newVars); | ||
if (Object.keys(this.customData.vars).length) { | ||
this.customData.i18n.debug('parsed templates vars', this.customData.vars); | ||
} | ||
// if (Object.keys(this.customData.vars).length) { | ||
// console.warn('parsed templates vars', this.customData.vars); | ||
// } | ||
@@ -77,11 +76,11 @@ // parse data attributes to vars | ||
this.customData.vars = Utils.concat(true, this.customData.vars, _el.dataset); | ||
if (Object.keys(this.customData.vars).length) { | ||
this.customData.i18n.debug('parsed attribute vars', this.customData.vars); | ||
} | ||
// if (Object.keys(this.customData.vars).length) { | ||
// console.warn('parsed attribute vars', this.customData.vars); | ||
// } | ||
// Parse templates wich have his own translations | ||
this.customData.langVars = this.customData.i18n.parseLocalVars(_el); | ||
if (this.customData.langVars && Object.keys(this.customData.langVars).length) { | ||
this.customData.i18n.debug('parsed own translations', this.customData.langVars); | ||
} | ||
// if (this.customData.langVars && Object.keys(this.customData.langVars).length) { | ||
// console.warn('parsed own translations', this.customData.langVars); | ||
// } | ||
}; | ||
@@ -110,3 +109,3 @@ | ||
if (this.customData.langVars && this.customData.langVars.default) { | ||
this.customData.i18n.debug('Translate by default', this.customData.langVars.default); | ||
// console.warn('Translate by default', this.customData.langVars.default); | ||
return this.customData.applyTranslation(this.customData.langVars.default); | ||
@@ -120,3 +119,3 @@ } | ||
if (local && typeof(local) === 'string') { | ||
// this.customData.i18n.debug('Translate by properties', [langcode, ...this.customData.properties], local); | ||
// console.warn('Translate by properties', [langcode, ...this.customData.properties], local); | ||
return this.customData.applyTranslation(local); | ||
@@ -126,3 +125,3 @@ } | ||
if (this.customData.langVars && this.customData.langVars.default) { | ||
this.customData.i18n.debug('Translate by default as fallback', this.customData.langVars.default); | ||
// console.warn('Translate by default as fallback', this.customData.langVars.default); | ||
return this.customData.applyTranslation(this.customData.langVars.default); | ||
@@ -138,3 +137,3 @@ } | ||
this.customData.onAttributeChanged = (data: IBinderAttributeChangedEvent) => { | ||
this.customData.onAttributeChanged = (data: BinderAttributeChangedEvent) => { | ||
if (data.detail.name.startsWith('data-')) { | ||
@@ -144,3 +143,3 @@ const varName = data.detail.name.slice(5); | ||
newVar[varName] = data.detail.newValue; | ||
this.customData.i18n.debug('binder-changed newVar', newVar); | ||
// console.warn('binder-changed newVar', newVar); | ||
this.customData.vars = Utils.concat(true, this.customData.vars, newVar); | ||
@@ -206,3 +205,3 @@ this.customData.translate(); | ||
} as IBinder<string>; | ||
} as Binder<string>; | ||
}; |
import { i18nStarBinderWrapper } from './i18n/i18n-star.binder'; | ||
import { ALocalesService } from '../services/locales-base.service'; | ||
import { IBinders } from '@ribajs/core'; | ||
import { Binders } from '@ribajs/core'; | ||
@@ -8,3 +8,3 @@ export default (localesService: ALocalesService) => { | ||
i18nStarBinder: i18nStarBinderWrapper(localesService), | ||
} as IBinders<any>; | ||
} as Binders<any>; | ||
}; |
@@ -1,1 +0,10 @@ | ||
export * from './switcher/switcher.abstract.component'; | ||
export { AI18nSwitcherComponent } from './abstract-switcher/switcher.abstract.component'; | ||
import { i18nSwitcherComponentWrapper } from './switcher/switcher.component'; | ||
import { ALocalesService } from '../services/locales-base.service'; | ||
import { Components } from '@ribajs/core'; | ||
export default (localesService: ALocalesService): Components => { | ||
return { | ||
i18nSwitcherComponent: i18nSwitcherComponentWrapper(localesService), | ||
}; | ||
}; |
import { tFormatterWrapper } from './t.formatter'; | ||
import { ALocalesService } from '../services/locales-base.service'; | ||
import { IFormatters } from '@ribajs/core'; | ||
import { Formatters } from '@ribajs/core'; | ||
export default (localesService: ALocalesService): IFormatters => { | ||
export default (localesService: ALocalesService): Formatters => { | ||
return { | ||
@@ -7,0 +7,0 @@ t: tFormatterWrapper(localesService), |
@@ -1,2 +0,2 @@ | ||
import { Debug, IFormatter } from '@ribajs/core'; | ||
import { Formatter } from '@ribajs/core'; | ||
import { ALocalesService } from '../services/locales-base.service'; | ||
@@ -21,31 +21,38 @@ | ||
const debug = Debug('formatter:t'); | ||
export const tFormatterWrapper = (localesService: ALocalesService): IFormatter => { | ||
export const tFormatterWrapper = (localesService: ALocalesService): Formatter => { | ||
return { | ||
name: 't', | ||
read(translateMePathString: string, langcode: string, ...vars: string[]) { | ||
debug('formatter t', translateMePathString, langcode); | ||
if (localesService.ready) { | ||
return translate(translateMePathString, localesService, langcode) | ||
.then((locale) => { | ||
debug('locale'); | ||
return locale; | ||
return new Promise((resolve, reject) => { | ||
localesService.event.on('changed', () => { | ||
console.debug('changed'); | ||
translate(translateMePathString, localesService, langcode) | ||
.then((locale) => { | ||
resolve(locale as any); | ||
}) | ||
.catch((error: Error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
localesService.event.on('ready', () => { | ||
translate(translateMePathString, localesService, langcode) | ||
.then((locale) => { | ||
debug('locale'); | ||
resolve(locale as any); | ||
}) | ||
.catch((error: Error) => { | ||
reject(error); | ||
}); | ||
localesService.event.on('ready', () => { | ||
translate(translateMePathString, localesService, langcode) | ||
.then((locale) => { | ||
resolve(locale as any); | ||
}) | ||
.catch((error: Error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
if (localesService.ready) { | ||
translate(translateMePathString, localesService, langcode) | ||
.then((locale) => { | ||
resolve(locale); | ||
}); | ||
} | ||
}); | ||
}, | ||
} as IFormatter; | ||
} as Formatter; | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { IRibaModule } from '@ribajs/core'; | ||
import { RibaModule } from '@ribajs/core'; | ||
@@ -11,8 +11,9 @@ export * from './binders'; | ||
import formattersWrapper from './formatters'; | ||
import componentsWrapper from './components'; | ||
import * as services from './services'; | ||
export const i18nModule = (localesService: services.ALocalesService): IRibaModule => { | ||
export const i18nModule = (localesService: services.ALocalesService): RibaModule => { | ||
return { | ||
binders: bindersWrapper(localesService), | ||
components: {}, | ||
components: componentsWrapper(localesService), | ||
formatters: formattersWrapper(localesService), | ||
@@ -19,0 +20,0 @@ services, |
@@ -1,4 +0,4 @@ | ||
export interface ILangcode { | ||
export interface Langcode { | ||
code: string; | ||
active: boolean; | ||
} |
@@ -1,2 +0,2 @@ | ||
export interface ILocalPluralization { | ||
export interface LocalPluralization { | ||
zero?: string; | ||
@@ -8,4 +8,4 @@ one?: string; | ||
export interface ILocalVar { | ||
[name: string]: string | ILocalPluralization; | ||
export interface LocalVar { | ||
[name: string]: string | LocalPluralization; | ||
} |
@@ -1,3 +0,3 @@ | ||
import { Utils, EventDispatcher, Debug } from '@ribajs/core'; | ||
import { ILangcode, ILocalPluralization, ILocalVar } from '../interfaces'; | ||
import { Utils, EventDispatcher } from '@ribajs/core'; | ||
import { Langcode, LocalPluralization, LocalVar } from '../interfaces'; | ||
@@ -16,4 +16,2 @@ export abstract class ALocalesService { | ||
protected debug = Debug('services:ALocalesService'); | ||
/** | ||
@@ -40,3 +38,3 @@ * The current setted langcode | ||
*/ | ||
public async get(properties?: string[], vars?: ILocalVar, force: boolean = false) { | ||
public async get(properties?: string[], vars?: LocalVar, force: boolean = false) { | ||
if (!this.ready && !force) { | ||
@@ -66,3 +64,3 @@ throw new Error('not ready'); | ||
if (local === null && properties) { | ||
this.debug(`WARNING translation missing: "${properties.join('.')}"`, local, properties); | ||
console.warn(`WARNING translation missing: "${properties.join('.')}"`, local, properties); | ||
} | ||
@@ -91,3 +89,3 @@ return local; | ||
*/ | ||
public async getByCurrentLang(properties: string[] = [], vars?: ILocalVar) { | ||
public async getByCurrentLang(properties: string[] = [], vars?: LocalVar) { | ||
const langcode = this.getLangcode(); | ||
@@ -126,3 +124,2 @@ if (!langcode) { | ||
if (this.currentLangcode !== langcode) { | ||
this.debug('setLangcode', langcode, this.currentLangcode); | ||
this.currentLangcode = langcode; | ||
@@ -142,3 +139,3 @@ | ||
.then((locals) => { | ||
const langcodes: ILangcode[] = []; | ||
const langcodes: Langcode[] = []; | ||
Object.keys(locals).forEach((langcode) => { | ||
@@ -157,5 +154,5 @@ langcodes.push({ | ||
*/ | ||
public parseTemplateVars(el: HTMLElement): ILocalVar { | ||
public parseTemplateVars(el: HTMLElement): LocalVar { | ||
const templates = el.querySelectorAll<HTMLTemplateElement>('template'); | ||
const vars: ILocalVar = {}; | ||
const vars: LocalVar = {}; | ||
templates.forEach((template) => { | ||
@@ -173,5 +170,5 @@ const name: string | null = template.getAttribute('name'); | ||
*/ | ||
public parseLocalVars(el: HTMLElement): ILocalVar { | ||
public parseLocalVars(el: HTMLElement): LocalVar { | ||
const templates = el.querySelectorAll<HTMLTemplateElement>('template'); | ||
const vars: ILocalVar = {}; | ||
const vars: LocalVar = {}; | ||
templates.forEach((template) => { | ||
@@ -191,7 +188,6 @@ const lang: string | null = template.getAttribute('lang'); | ||
*/ | ||
public setTranslateStringVars(translateString: string, vars: ILocalVar) { | ||
public setTranslateStringVars(translateString: string, vars: LocalVar) { | ||
if (!translateString || Object.keys(vars).length === 0 || typeof(translateString.match) !== 'function') { | ||
return translateString; | ||
} | ||
this.debug('setTranslateStringVars', translateString, vars); | ||
const matches = translateString.match(/{{\s*?[A-Za-z0-9_-]+\s*?}}/gm); | ||
@@ -208,3 +204,2 @@ if (matches) { | ||
} | ||
this.debug('setTranslateStringVars', translateString); | ||
return translateString; | ||
@@ -258,3 +253,3 @@ } | ||
*/ | ||
protected setTranslateStringPluralization(translateObj: ILocalPluralization | string, vars: ILocalVar) { | ||
protected setTranslateStringPluralization(translateObj: LocalPluralization | string, vars: LocalVar) { | ||
if (vars.count && typeof(translateObj) === 'object' && translateObj !== null) { | ||
@@ -261,0 +256,0 @@ const count = Number(vars.count); |
@@ -1,2 +0,2 @@ | ||
import { Debug, Utils } from '@ribajs/core'; | ||
import { Utils } from '@ribajs/core'; | ||
import { ALocalesService } from './locales-base.service'; | ||
@@ -29,4 +29,2 @@ | ||
protected debug = Debug('services:LocalesService'); | ||
constructor(protected url: string, doNotTranslateDefaultLanguage: boolean = false, showMissingTranslation: boolean = false) { | ||
@@ -71,3 +69,2 @@ super(doNotTranslateDefaultLanguage, showMissingTranslation); | ||
this.locales[url as string] = locales; | ||
this.debug('getAll', locales); | ||
return this.locales[url as string]; | ||
@@ -74,0 +71,0 @@ }); |
@@ -1,2 +0,1 @@ | ||
import { Debug } from '@ribajs/core'; | ||
import { ALocalesService } from './locales-base.service'; | ||
@@ -24,4 +23,2 @@ | ||
protected debug = Debug('services:LocalesStaticService'); | ||
constructor(protected locales: any, protected id?: string, doNotTranslateDefaultLanguage: boolean = false, showMissingTranslation: boolean = false) { | ||
@@ -28,0 +25,0 @@ super(doNotTranslateDefaultLanguage, showMissingTranslation); |
@@ -9,2 +9,2 @@ { | ||
"include": ["*.ts", "**/*.ts"] | ||
} | ||
} |
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
31939
13
22
875
26
Updated@ribajs/core@^1.6.0