Comments (14)
Hi @Zambiorix,
Thank you for your feedback. The locale system is actually very crude and does not include any "intelligence". It is somewhat intentional, as for our use cases the fallback is somewhat of a last resort if the key was not defined for the current locale.
To avoid putting to much into one plugin, It would probably make sense to have an additional complementary plugin that would determine what the ideal fallback locale is, based on the browser locale and the available locales in the store and then set the corresponding fallback locale for the vuex-i18n-plugin.
Would this make sense to you?
from vuex-i18n.
I'm fairly new to Vue :-)
Why would you want an extra plugin for that? If the i18v plugin would allow a handler (to define the actual locale) wouldn't that be enough?
from vuex-i18n.
It is basically about separation of concerns and there are two possible problems that I see with the scenario of determining a fallback locale:
- We need a sensible way to detect the users current locale. This is not necessarily straightforward especially for older browsers, and might lead to problems if the library is used with server side rendering.
- The fallback locale translations must be present in the browser in the vuex store. This would mean, that on page load, one would need to load all available locales, many of which would never actually be used.
Therefore I would suggest a workflow as follows:
- Fetching an array of available locales from the server. The vuex-i18n plugin would not handle this, as we do not want to impose any restriction on how people setup server communication or specify available locales.
- Use a separate vuex-i18n-localization plugin to determine what the best locale would be, depending on the users locale and the available locales.
- Load the respective locale from the server and set it as current locale (or as fallback)
Alternatively, one could also use a server side mechanism to detect the users locale and send the correct locale translation to the client on the first request.
from vuex-i18n.
- You could leave locale detection out of any plugin, and up to the developer. Indeed many scenario's are possible. A good x-platform locale detection plugin would be handy indeed, but totally disconnected from the i18n plugin.
- You could also leave locale loading up to the developer, I am doing this in my projects already.
It is possible to give some advice on how to implement those scenario's in the form of examples.
I often build my websites (and api's, native apps, etc ..) with maximum 3 locales loaded at any time.
For example the project I am working on now has the following pattern:
- get x-platform browser locale
- check if language cookie has been set, if yes take that locale (anonymous user can change language preference to override browser locale)
- check if user has signed in, if yes, get language preference from user profile and set that locale (known user can change language preference that overrides both anonymous user and browser locale)
- define locale list to load: examples:
nl-BE => load => nl-BE, nl, en
en-UK => load => en-UK, en - set locale and fallback locale
Usually, a region specific locale has much less content than a generic locale. Only the texts that are specific to that region. Between nl-BE and nl-NL, there is not much difference for example.
I always use "en" locale as final fallback in case not all texts are translated during development/staging. And english is generic enough for most translators ;-)
en => all texts during development
nl => all dutch texts
nl-BE => some texts that are flemish and differ to official dutch
That said, your plugin is perfect for the work it has to do. All the locale handling and loading is done by myself. It is, in my opinion, only missing one feature of (natural fallback order):
region locale => generic locale => fallback locale
nl-BE => nl => en
Now there are only 2 levels.
A simple solution that does not break current code is to:
when a text is requested:
- check if the text is available from the set locale, yes -> return, no -> next step
- check if the set locale is region specific, no -> step 5, yes -> next step
- make set locale generic (split on "-", take first part, normalise)
- check if text is available from the generic locale, yes -> return, no -> next step
- check if text is available in fallback locale, yes -> return, no -> not available
This way, there are no breaking changes and offers a solution for a 3 level fallback
from vuex-i18n.
Hmm.. That sounds quite feasible. I will take a swing at it and see if I can get this to work.
from vuex-i18n.
Currently, I organise my root language file as follows:
This way it is easier for translators to find out what all the texts mean, see the translations of a text together + with a good json editor you don't see one long list but collapsible blocks
When done, I flatten and split the file in a format that i18n can load
i18n.en.json =>
"all.account.toolbar-menu.links.sign-in": "Sign in",
...
not sure how to insert multiline json here :-/
{ "all": { "account": { "toolbar-menu": { "links": { "sign-in": { "en": "Sign in", "nl": "Aanmelden" }, "sign-up": { "en": "Sign up", "nl": "Registreren" } } },
from vuex-i18n.
Hi there,
there is a new version 1.6.0 that should implement the fallback locale as you proposed.
from vuex-i18n.
that was fast :-)
from vuex-i18n.
That sounds like a reasonable way to go about organization of language files. Do you already have some tooling available to split the files into language specific translations?
It would be nice to have sort of a command line interface tool to
a) gather all translations in the source files and generate – or even better update – the translation source files and
b) transform the translation source files into language specific files
from vuex-i18n.
I have some stuff done in golang for api projects, but I am looking into webpack plugin to create statics on the go based on my global file
from vuex-i18n.
the idea is to do this automatically on dev / deployment builds
from vuex-i18n.
My initial thought was also to use golang for this, as we are programming practically all of our server stuff in golang and I am though quite familiar with it.
But I agree with you that a webpack plugin would probably be easier to integrate into the build pipeline directly.
from vuex-i18n.
javascript / vue / webpack, it is all fairly new for me .. steep learning curve, it will take some time
I'm backend (golang/.NET) and native mobile app developer
from vuex-i18n.
it is working!
thank you!
from vuex-i18n.
Related Issues (20)
- Support for yarn HOT 1
- What is the current version? HOT 3
- The v1.10.7 introduced a couple of breaking changes: Comparison vs v1.10.5 HOT 5
- Not able to access i18 plugin from mutation in classic mode store in Nuxt application HOT 1
- Usage of special characters inside the translations ( ) HOT 2
- Testing the fallback language - Chai HOT 2
- Single File Component Children not updating HOT 3
- Hook custom logic for non existent keys in lang HOT 2
- nuxt example implementation HOT 2
- Improve typescript declarations HOT 3
- Improve plugin documentation
- Multiple modules HOT 2
- What about provide plugin for debugging content HOT 1
- Using square brackets in identifier does not work HOT 2
- Linked local messages HOT 1
- Set Fallback language File HOT 5
- How to translate a component attribute/property
- Unicode-Support?
- support change language key before render ?
- Option to suppress the `i18n: Not all placeholders found` console warnings HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuex-i18n.