-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Custom locale loader #35
Conversation
I've refined this further to instead accept This allows me to use these settings: settings: {
'vue-i18n': {
locale: 'en',
messages: (() => {
const _ = require('lodash')
const glob = require('glob')
const messages = {}
glob.sync('./resources/lang/**/*.json').forEach(file => {
_.set(messages, file.match(/resources\/lang\/(.*)\.json/)[1].replace(/\//g, '.'), require(file))
})
return messages // {en: {...}, ko: {...}}
})()
},
}, The This still lacks multiple locale support, but this seems closer. What do you think? |
ec9ea44
to
1ad0dd3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your PR!
great! 👍
the ability to load it programmatically by user is great,
I think it would be nice to have some directory pattern settings, such as i18n-ally
.
https://github.com/antfu/i18n-ally#-directory-structure
/cc @antfu
lib/utils/index.js
Outdated
function getLocaleMessages (context) { | ||
const settings = getSettings(context) | ||
|
||
if (settings.locale && settings.messages) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need settings.locale
?
We need to cover everything all locales resources.
In about configuration name of settings.message
, I think this feature is for loading resources with complicated structure. For this reason, the name should be intuitive, such as loadLocaleMessages
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this design was because I misunderstood how the library currently supports multiple locales, I've provided more details below.
@kazupon I only just appreciated how your library supports multiple locales – in that it'll currently assume that every json file must define every key (because its assumed there'll be one per locale). I'll update my design. I went with I can't speak for general usage but I certainly adapted my vue-i18n usage to support eslint-plugin-vue-i18n (I switched to json; as well as making other compromises). I figured terse language might make sense here, breaking up i18n into multiple files probably won't be a poweruser activity once it's supported, I'd expect many want to do it, translations get unwieldy fast otherwise. |
@kazupon okay how about with these amends, the syntax for settings would now be like this, similar to the settings: {
'vue-i18n': {
locales: (() => {
const _ = require('lodash')
const glob = require('glob')
const locales = {}
glob.sync('./resources/lang/**/*.json').forEach(file => {
_.set(locales, file.match(/resources\/lang\/(.*)\.json/)[1].replace(/\//g, '.'), require(file))
})
return _.toPairs(locales).map(locale => {
return { name: locale[0], messages: locale[1] }
})
})()
},
}, AKA simplified: settings: {
'vue-i18n': {
locales: [
{ name: 'en', messages: {...} },
{ name: 'fr', messages: {...} },
],
},
}, The |
@kazupon ping |
sorry, I'm busy to work and to support Vue 3. 🙇 |
Looking forward to use this enhancement. |
@kazupon any update? shall I rebase this? |
Hi @stevelacey. Can you still continue to work on this PR? |
I close this PR because it is not active. |
Yeah I'd love to get this in but it needs entirely rewriting now given you've switched to TypeScript, I'm still using my forked branch on a couple of projects and it works great. |
Adds the ability to swap out
loadLocaleMessages
for your own implementation.This particular loader I've written allows me to load a structure like this, where the sub directories contain json that is flattened, the tree is implied by the path.
I'll add tests if you're happy with the implementation / naming / return values - let me know if there are any tweaks I should make.
A future work that would be great is to wrap in multiple locale check support. I'm not sure how to even approach it – but it would certainly related to the loader function in the end.
I should note that this also addresses the issue I created last year #32 – by supplying the ability to use a custom loader function users could load js based locales, or any format they want.