Skip to content
Open
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
12 changes: 10 additions & 2 deletions packages/react/src/lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ class I18n {
const keyType = typeof key
if (keyType !== 'string') throw new Error(`Translation key must be a string, got: ${keyType}`)

const template = this.translations[key]
if (!template) throw new Error(`Missing translation: ${key}`)
// Split the key into parts to support nested keys
const keyParts = key.split('.')
let template = this.translations

// Traverse the translations object to get the final value
for (const part of keyParts) {
template = template[part]
if (!template) throw new Error(`Missing translation: ${key}`)
}

if (typeof template !== 'string') throw new Error(`Invalid translation for key: ${key}`)

return template.replace(/{{(.*?)}}/g, (_, match) => context[match] || '')
Expand Down