提交 cd03dc19 编写于 作者: fxy060608's avatar fxy060608

feat(i18n): add resolveLocale

上级 ddc767ef
......@@ -1387,10 +1387,10 @@ var serviceContext = (function (vue) {
}
locale = locale.toLowerCase();
if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') !== -1) {
if (locale.indexOf('-hans') > -1) {
return LOCALE_ZH_HANS;
}
if (locale.indexOf('-hant') !== -1) {
if (locale.indexOf('-hant') > -1) {
return LOCALE_ZH_HANT;
}
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
......@@ -7607,6 +7607,7 @@ var serviceContext = (function (vue) {
plus.zip.compressVideo({
src: tempFilePath,
filename,
quality: 'medium',
}, ({ tempFilePath }) => {
resolve(tempFilePath);
}, () => {
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
import {
LOCALE_ZH_HANS,
LOCALE_ZH_HANT,
LOCALE_EN,
LOCALE_ES,
LOCALE_FR,
} from '../src/I18n'
import { resolveLocale } from '../src/locale'
const resolve = resolveLocale([
LOCALE_ZH_HANS,
LOCALE_ZH_HANT,
LOCALE_EN,
LOCALE_ES,
LOCALE_FR,
])
describe('resolveLocale', () => {
test('zh=>zh-Hans', () => {
expect(resolve('zh')).toBe('zh-Hans')
})
test('zh-CN=>zh-Hans', () => {
expect(resolve('zh-CN')).toBe('zh-Hans')
expect(resolve('zh_CN')).toBe('zh-Hans')
})
test('en-US=>en', () => {
expect(resolve('en-US')).toBe('en')
expect(resolveLocale(['en'])('en-US')).toBe('en')
})
test('a-b-c=>a-b', () => {
expect(resolveLocale(['a-b', 'a'])('a-b-c')).toBe('a-b')
})
})
......@@ -126,10 +126,10 @@ function normalizeLocale(locale, messages) {
}
locale = locale.toLowerCase();
if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') !== -1) {
if (locale.indexOf('-hans') > -1) {
return LOCALE_ZH_HANS;
}
if (locale.indexOf('-hant') !== -1) {
if (locale.indexOf('-hant') > -1) {
return LOCALE_ZH_HANT;
}
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
......@@ -429,6 +429,25 @@ function walkJsonObj(jsonObj, walk) {
return false;
}
function resolveLocale(locales) {
return (locale) => {
if (!locale) {
return locale;
}
locale = normalizeLocale(locale) || locale;
return resolveLocaleChain(locale).find((locale) => locales.indexOf(locale) > -1);
};
}
function resolveLocaleChain(locale) {
const chain = [];
const tokens = locale.split('-');
while (tokens.length) {
chain.push(tokens.join('-'));
tokens.pop();
}
return chain;
}
exports.Formatter = BaseFormatter;
exports.I18n = I18n;
exports.LOCALE_EN = LOCALE_EN;
......@@ -443,3 +462,4 @@ exports.isI18nStr = isI18nStr;
exports.isString = isString;
exports.normalizeLocale = normalizeLocale;
exports.parseI18nJson = parseI18nJson;
exports.resolveLocale = resolveLocale;
......@@ -59,6 +59,8 @@ export declare function isI18nStr(value: string, delimiters: [string, string]):
export declare const isString: (val: unknown) => val is string;
declare type Locale = string;
export declare const LOCALE_EN = "en";
export declare const LOCALE_ES = "es";
......@@ -77,6 +79,8 @@ export declare function normalizeLocale(locale: string, messages?: LocaleMessage
export declare function parseI18nJson(jsonObj: unknown, values: Record<string, string>, delimiters: [string, string]): unknown;
export declare function resolveLocale(locales: Locale[]): (locale: Locale) => string | undefined;
declare type Token = {
type: 'text' | 'named' | 'list' | 'unknown';
value: string;
......
......@@ -122,10 +122,10 @@ function normalizeLocale(locale, messages) {
}
locale = locale.toLowerCase();
if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') !== -1) {
if (locale.indexOf('-hans') > -1) {
return LOCALE_ZH_HANS;
}
if (locale.indexOf('-hant') !== -1) {
if (locale.indexOf('-hant') > -1) {
return LOCALE_ZH_HANT;
}
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
......@@ -425,4 +425,23 @@ function walkJsonObj(jsonObj, walk) {
return false;
}
export { BaseFormatter as Formatter, I18n, LOCALE_EN, LOCALE_ES, LOCALE_FR, LOCALE_ZH_HANS, LOCALE_ZH_HANT, compileI18nJsonStr, hasI18nJson, initVueI18n, isI18nStr, isString, normalizeLocale, parseI18nJson };
function resolveLocale(locales) {
return (locale) => {
if (!locale) {
return locale;
}
locale = normalizeLocale(locale) || locale;
return resolveLocaleChain(locale).find((locale) => locales.indexOf(locale) > -1);
};
}
function resolveLocaleChain(locale) {
const chain = [];
const tokens = locale.split('-');
while (tokens.length) {
chain.push(tokens.join('-'));
tokens.pop();
}
return chain;
}
export { BaseFormatter as Formatter, I18n, LOCALE_EN, LOCALE_ES, LOCALE_FR, LOCALE_ZH_HANS, LOCALE_ZH_HANT, compileI18nJsonStr, hasI18nJson, initVueI18n, isI18nStr, isString, normalizeLocale, parseI18nJson, resolveLocale };
......@@ -60,10 +60,10 @@ export function normalizeLocale(
}
locale = locale.toLowerCase()
if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') !== -1) {
if (locale.indexOf('-hans') > -1) {
return LOCALE_ZH_HANS
}
if (locale.indexOf('-hant') !== -1) {
if (locale.indexOf('-hant') > -1) {
return LOCALE_ZH_HANT
}
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
......
export * from './I18n'
export * from './vue-i18n'
export * from './json'
export { resolveLocale } from './locale'
export { default as Formatter } from './format'
import { normalizeLocale } from '../I18n'
type Locale = string
export function resolveLocale(locales: Locale[]) {
return (locale: Locale) => {
if (!locale) {
return locale
}
locale = normalizeLocale(locale) || locale
return resolveLocaleChain(locale).find(
(locale) => locales.indexOf(locale) > -1
)
}
}
function resolveLocaleChain(locale: Locale): Locale[] {
const chain: Locale[] = []
const tokens = locale.split('-')
while (tokens.length) {
chain.push(tokens.join('-'))
tokens.pop()
}
return chain
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册