Skip to content

Commit 7d28dd2

Browse files
authored
fix: Support for te behavior compatibility before v9.6 (#1751)
* fix: Support for `te` behavior compatibility before v9.6 * fix: warning once
1 parent 6a66289 commit 7d28dd2

File tree

8 files changed

+130
-26
lines changed

8 files changed

+130
-26
lines changed

packages/petite-vue-i18n/src/vue.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ declare module '@vue/runtime-core' {
864864
* @param key - A target locale message key
865865
* @param locale - A locale, optional, override locale that global scope or local scope
866866
*
867-
* @returns If found locale message, `true`, else `false`, Note that `false` is returned even if the value present in the Key is not translatable.
867+
* @returns If found locale message, `true`, else `false`, Note that `false` is returned even if the value present in the key is not translatable, yet if `translateExistCompatible` is set to `true`, it will return `true` if the key is available, even if the value is translatable.
868868
*/
869869
$te<
870870
Key extends string,

packages/vue-i18n-bridge/src/vue.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ declare module '@vue/runtime-core' {
869869
* @param key - A target locale message key
870870
* @param locale - A locale, optional, override locale that global scope or local scope
871871
*
872-
* @returns If found locale message, `true`, else `false`, Note that `false` is returned even if the value present in the Key is not translatable.
872+
* @returns If found locale message, `true`, else `false`, Note that `false` is returned even if the value present in the key is not translatable, yet if `translateExistCompatible` is set to `true`, it will return `true` if the key is available, even if the value is translatable.
873873
*/
874874
$te<
875875
Key extends string,

packages/vue-i18n-core/src/composer.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
assign,
1414
inBrowser,
1515
deepCopy,
16-
hasOwn
16+
hasOwn,
17+
warnOnce
1718
} from '@intlify/shared'
1819
import {
1920
isTranslateFallbackWarn,
@@ -614,6 +615,24 @@ export interface ComposerOptions<
614615
* @defaultValue `undefined`
615616
*/
616617
messageCompiler?: MessageCompiler
618+
/**
619+
* @remarks
620+
* An option to make `te` behavior specification before v9.6
621+
*
622+
* @VueI18nTip
623+
* :new: v9.10+
624+
*
625+
* @VueI18nWarning
626+
* This flag will be removed in v10.
627+
*
628+
* @VueI18nSee [GitHub Issue](https://github.com/intlify/vue-i18n-next/issues/1738)
629+
*
630+
* @VueI18nSee [`te`](composition#te-key-locale)
631+
*
632+
* @defaultValue `false`
633+
*
634+
*/
635+
translateExistCompatible?: boolean
617636
}
618637

619638
/**
@@ -1383,7 +1402,7 @@ export interface Composer<
13831402
* @param key - A target locale message key
13841403
* @param locale - A locale, it will be used over than global scope or local scope
13851404
*
1386-
* @returns If found locale message, `true`, else `false`, Note that `false` is returned even if the value present in the Key is not translatable.
1405+
* @returns If found locale message, `true`, else `false`, Note that `false` is returned even if the value present in the key is not translatable, yet if `translateExistCompatible` is set to `true`, it will return `true` if the key is available, even if the value is translatable.
13871406
*/
13881407
te<
13891408
Str extends string,
@@ -1860,6 +1879,17 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
18601879
const flatJson = options.flatJson
18611880
const _ref = inBrowser ? ref : shallowRef
18621881

1882+
const translateExistCompatible = !!options.translateExistCompatible
1883+
if (__DEV__) {
1884+
if (translateExistCompatible && !__TEST__) {
1885+
warnOnce(
1886+
getWarnMessage(
1887+
I18nWarnCodes.NOTICE_DROP_TRANSLATE_EXIST_COMPATIBLE_FLAG
1888+
)
1889+
)
1890+
}
1891+
}
1892+
18631893
let _inheritLocale = isBoolean(options.inheritLocale)
18641894
? options.inheritLocale
18651895
: true
@@ -2357,11 +2387,11 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23572387
const targetLocale = isString(locale) ? locale : _locale.value
23582388
const message = getLocaleMessage(targetLocale)
23592389
const resolved = _context.messageResolver(message, key)
2360-
return (
2361-
isMessageAST(resolved) ||
2362-
isMessageFunction(resolved) ||
2363-
isString(resolved)
2364-
)
2390+
return !translateExistCompatible
2391+
? isMessageAST(resolved) ||
2392+
isMessageFunction(resolved) ||
2393+
isString(resolved)
2394+
: resolved != null
23652395
},
23662396
() => [key],
23672397
'translate exists',

packages/vue-i18n-core/src/i18n.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ export interface I18n<
234234
* An instance of this property is **global scope***.
235235
*/
236236
readonly global: Legacy extends true
237-
? VueI18n<Messages, DateTimeFormats, NumberFormats, OptionLocale>
238-
: Legacy extends false
239-
? Composer<Messages, DateTimeFormats, NumberFormats, OptionLocale>
240-
: unknown
237+
? VueI18n<Messages, DateTimeFormats, NumberFormats, OptionLocale>
238+
: Legacy extends false
239+
? Composer<Messages, DateTimeFormats, NumberFormats, OptionLocale>
240+
: unknown
241241
/**
242242
* The property whether or not the Composition API is available
243243
*
@@ -529,17 +529,17 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
529529
// prettier-ignore
530530
const __legacyMode = __LITE__
531531
? false
532-
: __FEATURE_LEGACY_API__ && isBoolean(options.legacy)
532+
: __FEATURE_LEGACY_API__ && isBoolean(options.legacy)
533533
? options.legacy
534534
: __FEATURE_LEGACY_API__
535535
// prettier-ignore
536536
const __globalInjection = isBoolean(options.globalInjection)
537-
? options.globalInjection
538-
: true
537+
? options.globalInjection
538+
: true
539539
// prettier-ignore
540540
const __allowComposition = __LITE__
541541
? true
542-
: __FEATURE_LEGACY_API__ && __legacyMode
542+
: __FEATURE_LEGACY_API__ && __legacyMode
543543
? !!options.allowComposition
544544
: true
545545
const __instances = new Map<ComponentInternalInstance, VueI18n | Composer>()
@@ -1228,10 +1228,10 @@ function useI18nForLegacy(
12281228
const _locale = ref<Locale>(
12291229
// prettier-ignore
12301230
!isLocalScope || _inheritLocale
1231-
? root.locale.value
1232-
: isString(options.locale)
1233-
? options.locale
1234-
: DEFAULT_LOCALE
1231+
? root.locale.value
1232+
: isString(options.locale)
1233+
? options.locale
1234+
: DEFAULT_LOCALE
12351235
)
12361236

12371237
const _fallbackLocale = ref<FallbackLocale>(

packages/vue-i18n-core/src/legacy.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,8 @@ function convertComposerOptions<
13761376
const datetimeFormats = options.datetimeFormats
13771377
const numberFormats = options.numberFormats
13781378
const flatJson = options.flatJson
1379+
const translateExistCompatible = (options as unknown as ComposerOptions)
1380+
.translateExistCompatible
13791381

13801382
return {
13811383
locale,
@@ -1396,6 +1398,7 @@ function convertComposerOptions<
13961398
escapeParameter,
13971399
messageResolver: options.messageResolver,
13981400
inheritLocale,
1401+
translateExistCompatible,
13991402
__i18n,
14001403
__root,
14011404
__injectWithOption

packages/vue-i18n-core/src/warnings.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const I18nWarnCodes = {
1313
COMPONENT_NAME_LEGACY_COMPATIBLE: inc(), // 14
1414
NOT_FOUND_PARENT_SCOPE: inc(), // 15
1515
IGNORE_OBJ_FLATTEN: inc(), // 16
16-
NOTICE_DROP_ALLOW_COMPOSITION: inc() // 17
16+
NOTICE_DROP_ALLOW_COMPOSITION: inc(), // 17
17+
NOTICE_DROP_TRANSLATE_EXIST_COMPATIBLE_FLAG: inc() // 18
1718
} as const
1819

1920
type I18nWarnCodes = (typeof I18nWarnCodes)[keyof typeof I18nWarnCodes]
@@ -27,7 +28,8 @@ export const warnMessages: { [code: number]: string } = {
2728
[I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE]: `Component name legacy compatible: '{name}' -> 'i18n'`,
2829
[I18nWarnCodes.NOT_FOUND_PARENT_SCOPE]: `Not found parent scope. use the global scope.`,
2930
[I18nWarnCodes.IGNORE_OBJ_FLATTEN]: `Ignore object flatten: '{key}' key has an string value`,
30-
[I18nWarnCodes.NOTICE_DROP_ALLOW_COMPOSITION]: `'allowComposition' option will be dropped in the next major version. For more information, please see 👉 https://tinyurl.com/2p97mcze`
31+
[I18nWarnCodes.NOTICE_DROP_ALLOW_COMPOSITION]: `'allowComposition' option will be dropped in the next major version. For more information, please see 👉 https://tinyurl.com/2p97mcze`,
32+
[I18nWarnCodes.NOTICE_DROP_TRANSLATE_EXIST_COMPATIBLE_FLAG]: `'translateExistCompatible' option will be dropped in the next major version.`
3133
}
3234

3335
export function getWarnMessage(

packages/vue-i18n-core/test/issues.test.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
withDirectives,
2020
resolveDirective,
2121
nextTick,
22-
getCurrentInstance
22+
getCurrentInstance,
23+
onMounted
2324
} from 'vue'
2425
import {
2526
setDevToolsHook,
@@ -1229,7 +1230,6 @@ test('issue #1610', async () => {
12291230
})
12301231

12311232
test('issue #1615', async () => {
1232-
console.log('----')
12331233
const en = {
12341234
hello: (() => {
12351235
const fn = ctx => {
@@ -1289,3 +1289,72 @@ test('issue #1717', async () => {
12891289
'a.b.c': 'Hello, Vue I18n' // should not be transformed to nested object like in issue
12901290
})
12911291
})
1292+
1293+
test('issue #1738', async () => {
1294+
const resources = {
1295+
en: {
1296+
messages: {
1297+
common: {
1298+
actions: {
1299+
cancel: 'Cancel'
1300+
}
1301+
}
1302+
}
1303+
},
1304+
nl: {
1305+
messages: {
1306+
common: {
1307+
actions: {
1308+
cancel: 'Cancel'
1309+
}
1310+
}
1311+
}
1312+
}
1313+
}
1314+
1315+
function loadTranslations(): Promise<typeof resources> {
1316+
return new Promise(resolve => resolve(resources))
1317+
}
1318+
1319+
function delay(ms: number) {
1320+
return new Promise(resolve => setTimeout(resolve, ms))
1321+
}
1322+
1323+
const i18n = createI18n({
1324+
locale: 'nl',
1325+
legacy: false,
1326+
translateExistCompatible: true,
1327+
fallbackLocale: 'en',
1328+
missingWarn: false,
1329+
silentFallbackWarn: true
1330+
})
1331+
1332+
const App = defineComponent({
1333+
setup() {
1334+
const { mergeLocaleMessage, te } = useI18n()
1335+
onMounted(() => {
1336+
setTimeout(async () => {
1337+
const data = await loadTranslations()
1338+
1339+
for (const key in data) {
1340+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1341+
const { messages } = (data as any)[key]
1342+
mergeLocaleMessage(key, messages)
1343+
}
1344+
}, 100)
1345+
})
1346+
return { te }
1347+
},
1348+
template: `<div>
1349+
<p id="te1">{{ te('common') }} - expected true</p>
1350+
<p id="te2">{{ te('common.actions') }} - expected true</p>
1351+
</div>`
1352+
})
1353+
1354+
const wrapper = await mount(App, i18n)
1355+
1356+
await delay(110)
1357+
1358+
expect(wrapper.find('#te1')?.textContent).toEqual(`true - expected true`)
1359+
expect(wrapper.find('#te2')?.textContent).toEqual(`true - expected true`)
1360+
})

packages/vue-i18n/src/vue.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ declare module '@vue/runtime-core' {
869869
* @param key - A target locale message key
870870
* @param locale - A locale, optional, override locale that global scope or local scope
871871
*
872-
* @returns If found locale message, `true`, else `false`, Note that `false` is returned even if the value present in the Key is not translatable.
872+
* @returns If found locale message, `true`, else `false`, Note that `false` is returned even if the value present in the key is not translatable, yet if `translateExistCompatible` is set to `true`, it will return `true` if the key is available, even if the value is translatable.
873873
*/
874874
$te<
875875
Key extends string,

0 commit comments

Comments
 (0)