Skip to content

Fix nested key linked message #2237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions e2e/formatting/linked.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getText, url } from '../helper'
expect(await getText(page, '#app p.modifier')).toMatch(
'custom modifiers example: snake-case'
)
expect(await getText(page, '#app p.linked-nested')).toMatch('Nested key')
})

test('change locale', async () => {
Expand All @@ -28,6 +29,9 @@ import { getText, url } from '../helper'
expect(await getText(page, '#app p.modifier')).toMatch(
'カスタム修飾子の例: スネーク-ケース'
)
expect(await getText(page, '#app p.linked-nested')).toMatch(
'ネストされたキー'
)
})
})
})
9 changes: 7 additions & 2 deletions examples/composition/formatting/linked.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<p class="modifier">
{{ t('message.custom_modifier', { snake: 'message.snake' }) }}
</p>
<p class="linked-nested">{{ t('message.linkedWithNested') }}</p>
</div>
<script>
const { createApp } = Vue
Expand All @@ -30,6 +31,7 @@
locale: 'en',
messages: {
en: {
'nested.key': 'Nested key',
message: {
language: 'Language',
the_world: 'the world',
Expand All @@ -39,10 +41,12 @@
missingHomeAddress: 'Please provide @.lower:message.homeAddress',
snake: 'snake case',
custom_modifier:
"custom modifiers example: @.snakeCase:{'message.snake'}"
"custom modifiers example: @.snakeCase:{'message.snake'}",
linkedWithNested: '@:nested.key'
}
},
ja: {
'nested.key': 'ネストされたキー',
message: {
language: '言語',
the_world: 'ザ・ワールド',
Expand All @@ -54,7 +58,8 @@
snake: 'スネーク ケース',
foo: 'message.snake',
custom_modifier:
"カスタム修飾子の例: @.snakeCase:{'message.snake'}"
"カスタム修飾子の例: @.snakeCase:{'message.snake'}",
linkedWithNested: '@:nested.key'
}
}
},
Expand Down
9 changes: 7 additions & 2 deletions examples/petite/formatting/linked.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<p class="modifier">
{{ t('message.custom_modifier', { snake: 'message.snake' }) }}
</p>
<p class="linked-nested">{{ t('message.linkedWithNested') }}</p>
</div>
<script>
const { createApp } = Vue
Expand All @@ -30,6 +31,7 @@
locale: 'en',
messages: {
en: {
'nested.key': 'Nested key',
'message.language': 'Language',
'message.the_world': 'the world',
'message.dio': 'DIO:',
Expand All @@ -39,9 +41,11 @@
'Please provide @.lower:message.homeAddress',
'message.snake': 'snake case',
'message.custom_modifier':
"custom modifiers example: @.snakeCase:{'message.snake'}"
"custom modifiers example: @.snakeCase:{'message.snake'}",
'message.linkedWithNested': '@:nested.key'
},
ja: {
'nested.key': 'ネストされたキー',
'message.language': '言語',
'message.the_world': 'ザ・ワールド',
'message.dio': 'ディオ:',
Expand All @@ -52,7 +56,8 @@
'message.snake': 'スネーク ケース',
'message.foo': 'message.snake',
'message.custom_modifier':
"カスタム修飾子の例: @.snakeCase:{'message.snake'}"
"カスタム修飾子の例: @.snakeCase:{'message.snake'}",
'message.linkedWithNested': '@:nested.key'
}
},
modifiers: {
Expand Down
4 changes: 2 additions & 2 deletions packages/core-base/src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,15 +1165,15 @@ function getMessageContextOptions<Messages, Message = string>(

// fallback
if (val == null && (fallbackContext || useLinked)) {
const [, , message] = resolveMessageFormat(
const [format] = resolveMessageFormat(
fallbackContext || context, // NOTE: if has fallbackContext, fallback to root, else if use linked, fallback to local context
key,
locale,
fallbackLocale as FallbackLocale,
fallbackWarn,
missingWarn
)
val = resolveValue(message, key)
val = format
}

if (isString(val) || isMessageAST(val)) {
Expand Down
13 changes: 13 additions & 0 deletions packages/core-base/test/translate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,3 +1053,16 @@ test('locale detector', () => {
expect(translate(ctx, 'hi')).toEqual('hi kazupon !')
expect(locale).toHaveBeenCalledTimes(2)
})

test('linked nested key', () => {
const ctx = context({
locale: 'en',
messages: {
en: {
'nested.key': 'Nested key',
'message.linkedWithNested': '@:nested.key'
}
}
})
expect(translate(ctx, 'message.linkedWithNested')).toEqual('Nested key')
})