-
Notifications
You must be signed in to change notification settings - Fork 80
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
Support an array of locales to set the current locale #202
Comments
Hey @cupcakearmy 👋
Not sure I'm following 100% 😅 Can you exemplify why you need this logic in the app? |
Sure! So immagine a site Currently, as the lib only supports one language, the webpage would try to load locales for So we need to write additional code to handle this behavior.
Hope this clears up the confusion :) |
Got it, thanks! Changing the
It may be a bit weird to assign it to an array and have a |
Yes, would not enforce it as array, only as an accepted value, as it's not the most common use case. Maybe one could add a |
Thought about it too. Would still be a breaking change, though 😅
I meant accepting an array as an input, but the output would always be a single string. However, while I was typing this I thought that maybe typing a store in such a way is not possible 🙈 Anyway, my point is that it seems the "array" is only useful as an input to define the initial locale, correct? I wouldn't want to complicate the usage of the We could change how we recommend changing the locale. Right now, one should modify the |
I agree on every point :) It's def. an initialization thing. So a helper function would be also enough, to choose the most approriate locale, and leave the whole I think it might be possible with proxies, but yes, introduces unecessary "magic" |
I'm still exploring modifying the Using your example, given that there are messages registered for these three languages, we can decide which one to "set" as the init({
fallbackLocale: 'en',
});
register('en', () => Promise.resolve({ foo: 'Foo' }));
register('de', () => Promise.resolve({ foo: 'Foo' }));
register('es', () => Promise.resolve({ foo: 'Foo' }));
expect(get($locale)).toBe('en');
await $locale.set(['it', 'es']);
expect(get($locale)).toBe('es'); What should happen with the value of I'm inclined to leave it as |
I've released an alpha version |
I may have missed this in the tread, but what about the fallbackLocale as an Object or Array instead? vue-i18n has something like this: fallbackLocale: {
'en-ie': ['en-gb'],
pap: ['nl'],
es: ['en-gb'],
ase: ['en-us'],
fcs: ['fr-ca'],
lsb: ['fr'],
'pt-br': ['pt', 'es'],
no: ['en-us'],
default: ['en-us', 'en']
} I have to support many languages and having a graceful fallback can be helpful. Of course the example above is complex but gives the idea of a cascade type fallback hierarchy. |
Is your feature request related to a problem? Please describe.
This lib has the notion of a single locale store. From there possible variations are extrapolated.
svelte-i18n/src/runtime/stores/dictionary.ts
Line 36 in 0516bb4
However in the real world both the
Accept-Language
headers andnavigator.languages
expose multiple languages, in order of user preference. This is a very useful feature as they can be used as a fallback if a specific is not available, before falling back to the globalfallbackLocale
The issue is that right now this logic is not exposed and needs to be handled by the developer by iterating the available languages, creating subsets (
en
,en-US
, etc.) and then match them agains the$locales
available in the lib.Describe the solution you'd like
Since the language matching feature is already in the codabase and does basically that one could:
$locale
store to also, beside a string, give a list of strings.Personally I'd say options 2 is the way to go
Describe alternatives you've considered
Write the logic in every project, for every dev. def. duable but it's a pity as most of the logic, as stated above, is alrady in the lib.
How important is this feature to you?
Not dealbreaking but it would def. be a welcome addition.
Additional context
The text was updated successfully, but these errors were encountered: