Skip to content

Commit

Permalink
feat: merging of locale files with the same name for `@intlify/unplug…
Browse files Browse the repository at this point in the history
…in-vue-i18n/messages` (#274)

* Fix merging of locale files with the same name

* Replace lodash.merge with a plain JS implementation

* Update README.md
  • Loading branch information
DanielleHuisman authored Nov 2, 2023
1 parent 8e74926 commit 8d042fc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/unplugin-vue-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export default defineConfig({
],
})
```
unplugin-vue-i18n will automatically merge locale files into `@intlify/unplugin-vue-i18n/messages`. This allows locales to be split across multiple files, for example `src/locales/fruits/en.json` and `src/locales/vegetables/en.json`.

### Types

If you want type definition of `@intlify/unplugin-vue-i18n/messages`, add `unplugin-vue-i18n/messages` to `compilerOptions.types` of your tsconfig:
Expand Down
2 changes: 0 additions & 2 deletions packages/unplugin-vue-i18n/examples/vite/src/locales/en.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
select: Do you want banana?
fruits:
banana: 'no bananas | {n} banana | {n} bananas'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fruits:
banana: 'no bananas | {n} banana | {n} bananas'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
fruits: {
banana: 'バナナがない | バナナ {n} 個'
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
select: "バナナが欲しい?",
fruits: {
banana: "バナナがない | バナナ {n} 個"
}
select: 'バナナが欲しい?'
}
9 changes: 2 additions & 7 deletions packages/unplugin-vue-i18n/examples/vite/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { createApp } from 'vue'
import { createI18n } from 'vue-i18n'
import App from './App.vue'
import ja from './locales/ja.json5'
import en from './locales/en.yaml'
// import messages from '@intlify/vite-plugin-vue-i18n/messages'
import messages from '@intlify/unplugin-vue-i18n/messages'

const i18n = createI18n({
legacy: false,
locale: 'ja',
messages: {
ja,
en
}
messages
})

const app = createApp(App)
Expand Down
26 changes: 23 additions & 3 deletions packages/unplugin-vue-i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,29 @@ async function generateBundleResources(
}
}

return `export default {
${codes.join(`,\n`)}
}`
return `const isObject = (item) => item && typeof item === 'object' && !Array.isArray(item);
const mergeDeep = (target, ...sources) => {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return mergeDeep(target, ...sources);
}
export default mergeDeep({},
${codes.map(code => `{${code}}`).join(',\n')}
);`
}

async function getCode(
Expand Down

0 comments on commit 8d042fc

Please sign in to comment.