Skip to content

Commit 2f78a69

Browse files
authored
fix: cannot resolve object path (#16)
closes #15
1 parent d2bf2b1 commit 2f78a69

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/runtime/translate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ export function translate(
205205
warn(`Fall back to translate '${key}' key with '${targetLocale}' locale.`)
206206
}
207207
message = messages[targetLocale] || {}
208-
format = resolveValue(message, key)
208+
if ((format = resolveValue(message, key)) === null) {
209+
// if null, resolve with object key path
210+
format = (message as any)[key] // eslint-disable-line @typescript-eslint/no-explicit-any
211+
}
209212
if (isString(format) || isFunction(format)) break
210213
handleMissing(context, key, targetLocale, missingWarn, 'translate')
211214
}

test/runtime/translate.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,16 @@ describe('edge cases', () => {
539539
})
540540
expect(translate(ctx, 'こんにちは')).toEqual('こんにちは!')
541541
})
542+
543+
test('object path key', () => {
544+
const ctx = context({
545+
locale: 'en',
546+
messages: {
547+
en: {
548+
'side.left': 'Left'
549+
}
550+
}
551+
})
552+
expect(translate(ctx, 'side.left')).toEqual('Left')
553+
})
542554
})

0 commit comments

Comments
 (0)