-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
feat: support external configuration file for vue-i18n options #605
feat: support external configuration file for vue-i18n options #605
Conversation
Codecov Report
@@ Coverage Diff @@
## master #605 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 3 3
Lines 120 120
Branches 31 31
=====================================
Hits 120 120 Continue to review full report at Codecov.
|
Added support for specifying path to local file for options passed to vue-i18n (the `vueI18n` key of `nuxt-i18n` configuration). This allows for configuration some options that have function type which previously would fail due to stringifying and lack of possibility to import stuff. Resolves #585, resolves #237
How to use it? I'm not actually quite sure how to implement it:
|
@janswist See the comment above the If you are looking for an explanation of how to use pluralizationRules then it's not strictly related to this feature. Refer to its documentation: https://kazupon.github.io/vue-i18n/guide/pluralization.html#custom-pluralization |
I created mentioned plugin like this: // ~/plugins/vue-i18n.js
export default context => {
return {
pluralizationRules: {
// example: years: 'rok | lata | lat'
'pl': function (choice, choicesLength) {
if (choice === 0)
return 2 // 0 lat
else if (choice === 1)
return 0 // 1 rok
else if (choice > 1 && choice < 5)
return 1 // 2, 3 lub 4 lata
else if (choice >= 5)
return 2 // 5 lat
}
}
}
} and tried adding: // nuxt.config.js
export default {
// tried this
plugins: [ '~/plugins/vue-i18n.js' ],
// ...and this (separately)
i18n: {
plugins: [ '~/plugins/vue-i18n.js' ],
}
} What am I doing wrong there? |
You are supposed to assign the file name to the property where you would normally put object with // nuxt.config.js
export default {
i18n: {
// other nuxt-i18n settings
vueI18n: '~/plugins/vue-i18n.js',
} And I would not call the file a "plugin" but rather a config and put it in a |
Thanks! Works perfectly now. Also thanks for a good tip - put it in the |
Here is my working solution for czech lang. // nuxt.config.js
{
// https://i18n.nuxtjs.org/options/
i18n: {
locales: [
{ code: 'cs', name: "CZ", file: 'cs.js' },
{ code: 'en', name: "EN", file: 'en.js' }
],
defaultLocale: 'cs',
lazy: true,
langDir: "~/i18n/",
detectBrowserLanguage: {
onlyOnRoot: true,
useCookie: true,
},
vueI18n: '~/config/i18n.js',
}
} // config/i18n.js
export default (context) => {
return {
fallbackLocale: "cs",
pluralizationRules: {
"cs": function (choice, choicesLength) {
if (choice === 0) {
return 0;
}
if (choice === 1) {
return 1;
}
if (choice > 1 && choice <= 4) {
return 2;
}
return 3;
}
}
}
} // i18n/cs.js
export default async () => {
return {
general: {
yes: "Ano",
no: "Ne"
}
}
} |
@f3l1x haha! I had the same issue, couldn't get czech to pluralize properly! Thanks for finding a solution! |
Glad to help. ;-) |
not working for me in 2023
|
When I use this solution I'm getting
What am I doing wrong? I tried to create this config in the plugins folder and tried to use js instead of ts but I'm still getting this error |
Same here... |
export default defineI18nConfig(() => ({
fallbackLocale: 'ru',
pluralRules: {
ru: (choice, choicesLength, orgRule) => {
if (choice === 0 || choice === 1) {
return choice
}
if (choice > 1 && choice < 5) {
return 2
}
return 0
}
}
})) try this |
Added support for specifying path to local file for options passed to
vue-i18n (the
vueI18n
key ofnuxt-i18n
configuration). This allowsfor configuration some options that have function type which previously
would fail due to stringifying and lack of possibility to import stuff.
Resolves #585, resolves #237