Skip to content

Commit 5a42786

Browse files
authored
fix: fallback locale bug (#64)
* fix: fallback locale with simple * fix: bump typescript
1 parent 3fb6d68 commit 5a42786

File tree

8 files changed

+74
-11
lines changed

8 files changed

+74
-11
lines changed

examples/legacy/fallback/basic.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
const i18n = createI18n({
1818
legacy: true,
1919
locale: 'ja',
20-
fallbackLocale: ['en'],
20+
fallbackLocale: 'en',
2121
messages: {
2222
en: {
2323
message: {

examples/legacy/fallback/suppress.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
const i18n = createI18n({
1818
legacy: true,
1919
locale: 'ja',
20-
fallbackLocale: ['en'],
20+
fallbackLocale: 'en',
2121
silentFallbackWarn: true, // warning off
2222
messages: {
2323
en: {

examples/legacy/missing/handler.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
const i18n = createI18n({
1818
legacy: true,
1919
locale: 'ja',
20-
fallbackLocale: ['en'],
20+
fallbackLocale: 'en',
2121
missing: (locale, key, instance) => {
2222
// something to do ...
2323
console.warn(`detect '${key}' key missing in '${locale}'`)

examples/legacy/missing/suppress.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
const i18n = createI18n({
1818
legacy: true,
1919
locale: 'ja',
20-
fallbackLocale: ['en'],
20+
fallbackLocale: 'en',
2121
silentTranslationWarn: true, // warning off
2222
messages: {
2323
en: {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"set-tz": "^0.2.0",
6363
"shipjs": "^0.19.0",
6464
"ts-jest": "^26.0.0",
65-
"typescript": "^3.8.3",
65+
"typescript": "^3.9.3",
6666
"typescript-eslint-language-service": "^3.0.0",
6767
"vue": "^3.0.0-beta.15"
6868
},
@@ -122,8 +122,8 @@
122122
"test:cover": "yarn test:unit --coverage",
123123
"test:e2e": "yarn build && jest --runInBand --config ./jest.e2e.config.js",
124124
"test:type": "tsc -p . --noEmit",
125-
"test:unit": "cross-env NODE_ICU_DATA=./node_modules/full-icu jest --env node",
126-
"test:watch": "cross-env NODE_ICU_DATA=./node_modules/full-icu jest --env node --watch"
125+
"test:unit": "yarn clean:cache:jest && cross-env NODE_ICU_DATA=./node_modules/full-icu jest --env node",
126+
"test:watch": "yarn clean:cache:jest && cross-env NODE_ICU_DATA=./node_modules/full-icu jest --env node --watch"
127127
},
128128
"sideEffects": false,
129129
"types": "dist/vue-i18n.d.ts",

src/core/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export function createRuntimeContext(
122122
const fallbackLocale =
123123
isArray(options.fallbackLocale) ||
124124
isPlainObject(options.fallbackLocale) ||
125+
isString(options.fallbackLocale) ||
125126
options.fallbackLocale === false
126127
? options.fallbackLocale
127128
: locale

test/core/translate.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,68 @@ describe('default option', () => {
113113
})
114114
})
115115

116+
describe('context fallbackLocale option', () => {
117+
test('false', () => {
118+
const mockWarn = warn as jest.MockedFunction<typeof warn>
119+
mockWarn.mockImplementation(() => {})
120+
121+
const ctx = context({
122+
locale: 'en',
123+
fallbackLocale: false,
124+
messages: {
125+
en: {}
126+
}
127+
})
128+
129+
expect(translate(ctx, 'hello')).toEqual('hello')
130+
expect(mockWarn.mock.calls[0][0]).toEqual(
131+
`Not found 'hello' key in 'en' locale messages.`
132+
)
133+
})
134+
135+
test('string', () => {
136+
const mockWarn = warn as jest.MockedFunction<typeof warn>
137+
mockWarn.mockImplementation(() => {})
138+
139+
const ctx = context({
140+
locale: 'en',
141+
fallbackLocale: 'ja',
142+
messages: {
143+
en: {},
144+
ja: {
145+
hello: 'こんにちは!'
146+
}
147+
}
148+
})
149+
150+
expect(translate(ctx, 'hello')).toEqual('こんにちは!')
151+
expect(mockWarn.mock.calls[0][0]).toEqual(
152+
`Not found 'hello' key in 'en' locale messages.`
153+
)
154+
})
155+
156+
test('array', () => {
157+
const mockWarn = warn as jest.MockedFunction<typeof warn>
158+
mockWarn.mockImplementation(() => {})
159+
160+
const ctx = context({
161+
locale: 'en',
162+
fallbackLocale: ['ja'],
163+
messages: {
164+
en: {},
165+
ja: {
166+
hello: 'こんにちは!'
167+
}
168+
}
169+
})
170+
171+
expect(translate(ctx, 'hello')).toEqual('こんにちは!')
172+
expect(mockWarn.mock.calls[0][0]).toEqual(
173+
`Not found 'hello' key in 'en' locale messages.`
174+
)
175+
})
176+
})
177+
116178
describe('context missing option', () => {
117179
test('not specified missing handler', () => {
118180
const mockWarn = warn as jest.MockedFunction<typeof warn>

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6814,10 +6814,10 @@ typescript-eslint-language-service@^3.0.0:
68146814
dependencies:
68156815
read-pkg-up "^7.0.0"
68166816

6817-
typescript@^3.8.3:
6818-
version "3.8.3"
6819-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
6820-
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==
6817+
typescript@^3.9.3:
6818+
version "3.9.5"
6819+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.5.tgz#586f0dba300cde8be52dd1ac4f7e1009c1b13f36"
6820+
integrity sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==
68216821

68226822
typescript@~3.7.2:
68236823
version "3.7.5"

0 commit comments

Comments
 (0)