react-simple-i18n
Advanced tools
Comparing version
import _typeof from "@babel/runtime/helpers/typeof"; | ||
export var getNestedValue = function getNestedValue(data, keyArr) { | ||
var getNestedValue = function getNestedValue(data, keyArr) { | ||
var currentIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; | ||
@@ -8,9 +9,24 @@ | ||
return data; | ||
} else { | ||
return ''; | ||
} | ||
return null; | ||
} | ||
return getNestedValue(data[keyArr[currentIndex]], keyArr, currentIndex + 1); | ||
if (typeof data !== 'string') { | ||
return getNestedValue(data === null || data === void 0 ? void 0 : data[keyArr === null || keyArr === void 0 ? void 0 : keyArr[currentIndex]], keyArr, currentIndex + 1); | ||
} | ||
return null; | ||
}; | ||
export var getI18nValue = function getI18nValue(data, key, getDefaultText) { | ||
var keyArr = key.split('.'); | ||
var text = getNestedValue(data, keyArr); | ||
if (!text && getDefaultText) { | ||
return getDefaultText(key); | ||
} | ||
return text || key; | ||
}; | ||
export default function createI18n() { | ||
@@ -30,3 +46,4 @@ var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var lang = options.lang; | ||
var lang = options.lang, | ||
getDefaultText = options.getDefaultText; | ||
var langData = data; | ||
@@ -40,6 +57,12 @@ currentLang = lang || (data.enUS ? 'enUS' : null); | ||
if (!key || !currentLang || typeof key !== 'string') return ''; | ||
var keyArr = key.split('.'); | ||
var value = getNestedValue(langData[currentLang], keyArr); | ||
if (!currentLang) { | ||
throw new Error('React Simple I18n: Current language must be set before using t()'); | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error('React Simple I18n: t() must have string type as its first argument'); | ||
} | ||
var value = getI18nValue(langData[currentLang], key, getDefaultText); | ||
if (args.length === 0) { | ||
@@ -46,0 +69,0 @@ return value; |
@@ -1,3 +0,14 @@ | ||
export declare const getNestedValue: (data: any, keyArr: Array<string>, currentIndex?: number) => string; | ||
export default function createI18n(data?: any, options?: any): { | ||
export interface I18nTextData { | ||
[textKey: string]: string | I18nTextData; | ||
} | ||
export interface I18nLanguageData { | ||
[languageKey: string]: I18nTextData; | ||
} | ||
export declare type GetDefaultTextType = (key: string) => string; | ||
export interface I18nOptions { | ||
lang?: string; | ||
getDefaultText?: GetDefaultTextType; | ||
} | ||
export declare const getI18nValue: (data: I18nTextData, key: string, getDefaultText?: GetDefaultTextType) => string; | ||
export default function createI18n(data?: I18nLanguageData, options?: I18nOptions): { | ||
t: (key: string, ...args: string[]) => string; | ||
@@ -4,0 +15,0 @@ getLang: () => string; |
@@ -9,3 +9,3 @@ "use strict"; | ||
exports.default = createI18n; | ||
exports.getNestedValue = void 0; | ||
exports.getI18nValue = void 0; | ||
@@ -20,12 +20,27 @@ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
return data; | ||
} else { | ||
return ''; | ||
} | ||
return null; | ||
} | ||
return getNestedValue(data[keyArr[currentIndex]], keyArr, currentIndex + 1); | ||
if (typeof data !== 'string') { | ||
return getNestedValue(data === null || data === void 0 ? void 0 : data[keyArr === null || keyArr === void 0 ? void 0 : keyArr[currentIndex]], keyArr, currentIndex + 1); | ||
} | ||
return null; | ||
}; | ||
exports.getNestedValue = getNestedValue; | ||
var getI18nValue = function getI18nValue(data, key, getDefaultText) { | ||
var keyArr = key.split('.'); | ||
var text = getNestedValue(data, keyArr); | ||
if (!text && getDefaultText) { | ||
return getDefaultText(key); | ||
} | ||
return text || key; | ||
}; | ||
exports.getI18nValue = getI18nValue; | ||
function createI18n() { | ||
@@ -45,3 +60,4 @@ var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var lang = options.lang; | ||
var lang = options.lang, | ||
getDefaultText = options.getDefaultText; | ||
var langData = data; | ||
@@ -55,6 +71,12 @@ currentLang = lang || (data.enUS ? 'enUS' : null); | ||
if (!key || !currentLang || typeof key !== 'string') return ''; | ||
var keyArr = key.split('.'); | ||
var value = getNestedValue(langData[currentLang], keyArr); | ||
if (!currentLang) { | ||
throw new Error('React Simple I18n: Current language must be set before using t()'); | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error('React Simple I18n: t() must have string type as its first argument'); | ||
} | ||
var value = getI18nValue(langData[currentLang], key, getDefaultText); | ||
if (args.length === 0) { | ||
@@ -61,0 +83,0 @@ return value; |
{ | ||
"name": "react-simple-i18n", | ||
"version": "1.3.0", | ||
"version": "1.4.0-beta.0", | ||
"description": "React i18n solution with context API", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -119,2 +119,3 @@ # React Simple I18n | ||
- `lang` Initial language to use | ||
- `defaultText` Function that returns default text if i18n doesn't exist. It takes a single argument which is the i18n key string. | ||
@@ -121,0 +122,0 @@ #### Return |
@@ -1,2 +0,2 @@ | ||
import createI18n, { getNestedValue } from './create-i18n' | ||
import createI18n, { getI18nValue } from './create-i18n' | ||
@@ -27,2 +27,3 @@ export const testLangData = { | ||
expect(i18n.getLang()).toBe(null) | ||
expect(() => i18n.t('hello')).toThrow() | ||
@@ -32,3 +33,3 @@ global.console.error = jest.fn() | ||
expect(global.console.error).toBeCalled() | ||
expect(i18n.t('hello')).toBe('') | ||
expect(() => i18n.t('hello')).toThrow() | ||
@@ -38,2 +39,5 @@ i18n.addLangData(testLangData) | ||
expect(i18n.t('hello')).toBe('Hello') | ||
expect(i18n.t('home.about')).toBe('home.about') | ||
expect(() => i18n.t(undefined)).toThrow() | ||
expect(() => i18n.t(null)).toThrow() | ||
}) | ||
@@ -88,15 +92,19 @@ | ||
expect(() => createI18n(null)).toThrow() | ||
// @ts-ignore | ||
expect(() => createI18n({}, 123)).toThrow() | ||
}) | ||
it('should test different cases of getNestedValue', () => { | ||
expect(getNestedValue(testLangData.enUS, ['nav', 'home'])).toBe('Home') | ||
expect(getNestedValue(testLangData.zhCN, ['nav', 'home'])).toBe('首页') | ||
expect(getNestedValue(testLangData.enUS, ['nav'])).toBe('') | ||
expect(getNestedValue(testLangData.enUS, ['hello'])).toBe('Hello') | ||
expect(getNestedValue({}, [])).toBe('') | ||
expect(getNestedValue({}, [''])).toBe('') | ||
expect(getNestedValue({}, ['key'])).toBe('') | ||
expect(getNestedValue({ a: 'hi' }, ['key'])).toBe('') | ||
it('should test different cases of getI18nValue', () => { | ||
expect(getI18nValue(testLangData.enUS, 'nav.home')).toBe('Home') | ||
expect(getI18nValue(testLangData.enUS, 'nav.home', key => `EMPTY_${key}`)).toBe('Home') | ||
expect(getI18nValue(testLangData.zhCN, 'nav.home')).toBe('首页') | ||
expect(getI18nValue(testLangData.enUS, 'nav')).toBe('nav') | ||
expect(getI18nValue(testLangData.enUS, 'nav.about')).toBe('nav.about') | ||
expect(getI18nValue(testLangData.enUS, 'nav.about', key => `EMPTY_${key}`)).toBe('EMPTY_nav.about') | ||
expect(getI18nValue(testLangData.enUS, 'hello')).toBe('Hello') | ||
expect(getI18nValue({}, '', key => 'No translation')).toBe('No translation') | ||
expect(getI18nValue({}, '')).toBe('') | ||
expect(getI18nValue({}, 'key')).toBe('key') | ||
expect(getI18nValue({ a: 'hi' }, 'key')).toBe('key') | ||
}) | ||
}) |
@@ -1,14 +0,43 @@ | ||
export const getNestedValue = (data: any, keyArr: Array<string>, currentIndex = 0): string => { | ||
export interface I18nTextData { | ||
[textKey: string]: string | I18nTextData | ||
} | ||
export interface I18nLanguageData { | ||
[languageKey: string]: I18nTextData | ||
} | ||
export type GetDefaultTextType = (key: string) => string | ||
export interface I18nOptions { | ||
lang?: string | ||
getDefaultText?: GetDefaultTextType | ||
} | ||
const getNestedValue = (data: I18nTextData | string, keyArr: Array<string>, currentIndex = 0): string => { | ||
if (currentIndex === keyArr.length) { | ||
if (typeof data === 'string') { | ||
return data | ||
} else { | ||
return '' | ||
} | ||
return null | ||
} if (typeof data !== 'string') { | ||
return getNestedValue(data?.[keyArr?.[currentIndex]], keyArr, currentIndex + 1) | ||
} | ||
return getNestedValue(data[keyArr[currentIndex]], keyArr, currentIndex + 1) | ||
return null | ||
} | ||
export default function createI18n(data: any = {}, options: any = {}) { | ||
export const getI18nValue = (data: I18nTextData, key: string, getDefaultText?: GetDefaultTextType): string => { | ||
const keyArr = key.split('.') | ||
const text = getNestedValue(data, keyArr) | ||
if (!text && getDefaultText) { | ||
return getDefaultText(key) | ||
} | ||
return text || key | ||
} | ||
export default function createI18n(data: I18nLanguageData = {}, options: I18nOptions = {}) { | ||
let currentLang: string | ||
@@ -25,3 +54,3 @@ let listenHandlers: Array<() => void> = [] | ||
const { lang } = options | ||
const { lang, getDefaultText } = options | ||
const langData = data | ||
@@ -32,6 +61,11 @@ | ||
function t(key: string, ...args: string[]) { | ||
if (!key || !currentLang || typeof key !== 'string') return '' | ||
if (!currentLang) { | ||
throw new Error('React Simple I18n: Current language must be set before using t()') | ||
} | ||
const keyArr = key.split('.') | ||
const value = getNestedValue(langData[currentLang], keyArr) | ||
if (!key || typeof key !== 'string') { | ||
throw new Error('React Simple I18n: t() must have string type as its first argument') | ||
} | ||
const value = getI18nValue(langData[currentLang], key, getDefaultText) | ||
if (args.length === 0) { | ||
@@ -38,0 +72,0 @@ return value |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
296711
1.05%1130
6.91%159
0.63%2
100%