Skip to content

Commit

Permalink
fix(unplugin-vue-i18n): exclude locales when using mixed locales #329 (
Browse files Browse the repository at this point in the history
  • Loading branch information
imslepov authored May 16, 2024
1 parent 68f63aa commit cbbf632
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 54 deletions.
9 changes: 8 additions & 1 deletion packages/bundle-utils/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export function generate(
escapeHtml,
jit
} as CodeGenOptions
const generator = createCodeGenerator(options)

let ast = parseJSON(value, { filePath: filename })

Expand All @@ -81,6 +80,14 @@ export function generate(
ast = parseJSON(value, { filePath: filename })
}

if (locale && onlyLocales?.length && !onlyLocales.includes(locale)) {
value = JSON.stringify({})
ast = parseJSON(value, { filePath: filename })
options.locale = ''
options.source = undefined
}

const generator = createCodeGenerator(options)
const codeMaps = _generate(generator, ast, options)

const { code, map } = generator.context()
Expand Down
9 changes: 8 additions & 1 deletion packages/bundle-utils/src/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export function generate(
escapeHtml,
jit
} as CodeGenOptions
const generator = createCodeGenerator(options)

let ast = parseYAML(value, { filePath: filename })

Expand All @@ -81,6 +80,14 @@ export function generate(
ast = parseYAML(value, { filePath: filename })
}

if (locale && onlyLocales?.length && !onlyLocales.includes(locale)) {
value = JSON.stringify({})
ast = parseYAML(value, { filePath: filename })
options.locale = ''
options.source = undefined
}

const generator = createCodeGenerator(options)
const codeMaps = _generate(generator, ast, options)

const { code, map } = generator.context()
Expand Down
13 changes: 13 additions & 0 deletions packages/unplugin-vue-i18n/test/fixtures/locale-mix-json.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<i18n lang="json">
{
"en": {
"hello": "hello world!"
}
}
</i18n>

<i18n locale="ja" lang="json">
{
"hello": "こんにちは、世界!"
}
</i18n>
8 changes: 8 additions & 0 deletions packages/unplugin-vue-i18n/test/fixtures/locale-mix-yaml.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<i18n lang="yaml">
en:
hello: hello world!
</i18n>

<i18n locale="ja" lang="yaml">
hello: こんにちは、世界!
</i18n>
Loading

0 comments on commit cbbf632

Please sign in to comment.