@ewb/translate
Advanced tools
Comparing version 1.1.3 to 1.2.0
@@ -11,3 +11,3 @@ "use strict"; | ||
translate(_) { | ||
return 'N/W'; | ||
return `N/W (${this.addWord})`; | ||
} | ||
@@ -14,0 +14,0 @@ add(translations) { |
@@ -14,2 +14,8 @@ import Tree, { WordTranslations, TreeOptions } from './Tree'; | ||
} | ||
interface Variables { | ||
[variable: string]: string | number | undefined; | ||
} | ||
interface TextOptions extends Variables { | ||
locale?: string; | ||
} | ||
export default class Translate { | ||
@@ -24,4 +30,4 @@ defaultLocale: string; | ||
word(word: string, locale?: string): string; | ||
t(text: string, locale?: string): string; | ||
text(text: string, locale?: string): string; | ||
t(text: string, opts?: TextOptions): string; | ||
text(text: string, { locale, ...variables }?: TextOptions): string; | ||
changeLocale(locale: string): void; | ||
@@ -34,4 +40,5 @@ export(): { | ||
exportTexts(): WordTranslations; | ||
private replaceVariables; | ||
private translateAndRunNoMatch; | ||
} | ||
export { Tree, Empty, Branch, Translate, TranslateOptions, WordTranslations, TreeOptions, Translations, BranchObject }; | ||
export { Tree, Empty, Branch, Translate, TranslateOptions, WordTranslations, TreeOptions, Translations, BranchObject, Variables, TextOptions }; |
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -9,2 +20,3 @@ const Tree_1 = require("./Tree"); | ||
exports.Branch = Branch_1.default; | ||
const helpers_1 = require("./utils/helpers"); | ||
class Translate { | ||
@@ -31,11 +43,13 @@ constructor({ defaultLocale = 'en', locale = 'en', words = {}, texts = {}, noMatch, noTranslation }) { | ||
} | ||
t(text, locale) { | ||
return this.text(text, locale); | ||
t(text, opts) { | ||
return this.text(text, opts); | ||
} | ||
text(text, locale = this.locale) { | ||
text(text, _a = {}) { | ||
var { locale = this.locale } = _a, variables = __rest(_a, ["locale"]); | ||
if (this.defaultLocale === locale) { | ||
return text; | ||
return this.replaceVariables(text, variables); | ||
} | ||
const foundText = this.tree.text(text); | ||
return this.translateAndRunNoMatch(foundText, locale); | ||
const translated = this.translateAndRunNoMatch(foundText, locale); | ||
return this.replaceVariables(translated, variables); | ||
} | ||
@@ -57,2 +71,10 @@ changeLocale(locale) { | ||
} | ||
replaceVariables(text, variables) { | ||
if (Object.keys(variables).length === 0) { | ||
return text; | ||
} | ||
return text.replace(helpers_1.VARIABLE_REGEXP, (word, group) => { | ||
return String(variables[group] || word); | ||
}); | ||
} | ||
translateAndRunNoMatch(foundText, locale) { | ||
@@ -69,3 +91,3 @@ if (foundText instanceof Empty_1.default) { | ||
} | ||
return 'N/T'; | ||
return `N/T (${foundText.word})`; | ||
} | ||
@@ -72,0 +94,0 @@ return translated; |
import Branch, { BranchObject, Translations } from './Branch'; | ||
import Empty from './Empty'; | ||
export interface WordTranslations { | ||
[key: string]: { | ||
[key: string]: string; | ||
[translate: string]: { | ||
[locale: string]: string; | ||
}; | ||
@@ -7,0 +7,0 @@ } |
import Branch from '../Branch'; | ||
export declare const VARIABLE_REGEXP: RegExp; | ||
export declare function getWord(level: number, word: string, sentence?: boolean): string; | ||
@@ -3,0 +4,0 @@ export declare function suggestions(this: Branch): any[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.VARIABLE_REGEXP = /{{(.*?)}}/g; | ||
function getWord(level, word, sentence = false) { | ||
@@ -4,0 +5,0 @@ if (sentence) { |
{ | ||
"name": "@ewb/translate", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "Translate as you create your app. Saves the words and text in a tree structure for fast and easy lookup.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -43,2 +43,6 @@ # Translate library | ||
'no-nb': 'Dette er ikke en setning' | ||
}, | ||
// Variables use | ||
'Are you {{age}} years old?': { | ||
'no-nb': 'Er du {{age}} år gammel?' | ||
} | ||
@@ -86,3 +90,3 @@ } | ||
translate.word('Word', 'en'); // Word | ||
translate.text('This is a sentence', 'en'); // Dette er en setning | ||
translate.text('This is a sentence', { locale: 'en' }); // Dette er en setning | ||
@@ -94,2 +98,14 @@ translate.setLocale('en'); | ||
## Variables | ||
Text function supports variables with parallel to the locale object. `locale` is a reserved key in TextOptions and will not be translated. | ||
Translation is targeted with `{{variable}}` | ||
``` | ||
translate.text('Are you {{age}} years old?', { age: 18 }) | ||
// no-nb: Er du 18 år gammel? | ||
// en: Are you 18 years old? | ||
``` | ||
## Match | ||
@@ -132,4 +148,4 @@ | ||
interface WordTranslations { | ||
[key: string]: { | ||
[key: string]: string; | ||
[translation: string]: { | ||
[locale: string]: string; | ||
}; | ||
@@ -142,2 +158,10 @@ } | ||
interface Variables { | ||
[variable: string]: string | number | undefined; | ||
} | ||
interface TextOptions extends Variables { | ||
locale?: string; | ||
} | ||
interface TranslateOptions { | ||
@@ -172,4 +196,4 @@ defaultLocale: string; | ||
t(text: string, locale?: string): string; | ||
text(text: string, locale?: string): string; | ||
t(text: string, opts?: TextOptions): string; | ||
text(text: string, opts?: TextOptions): string; | ||
@@ -215,4 +239,4 @@ changeLocale(locale: string): void; | ||
class Empty { | ||
// Will always return N/T | ||
translate(locale: string): 'N/T'; | ||
// Will always return N/T (%word%) | ||
translate(locale: string): 'N/T (%word%)'; | ||
@@ -219,0 +243,0 @@ // Add word to the tree with translations if wanted |
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
48454
21
444
243