Comments (17)
The _
method will always return a localized string
. If you want iterate through some items and translate them, you need to have a separate object (like that list
of yours, loop through it and call the _
method with the id of each country. The dictionary of messages is not supposed to be something you would iterate over like that 😁.
Here's a simple REPL to demonstrate that: https://svelte.dev/repl/49412f11564b41a183f414177ffc52b9?version=3.18.2
from svelte-i18n.
It's not super practical, I literally have the identifier of all the countries to copy and paste 🤡.
Is it possible to get a version of $dictionary not shallow?
from svelte-i18n.
If anyone comes into this need, I did this
import { pickBy } from "lodash"
let dic = $dictionary[$locale];
let countries = [];
pickBy(dic, (v, k) => {
if (k.includes("countries.list")) {
const key = k.split(".")[2];
countries.push({ key, value: v });
}
});
It does the trick, but something native could be really cool 😉
from svelte-i18n.
Following what you wanted on the OP, what would$_("countries.list")
render? It's a POJO, svelte would just output [object Object]
.
Dictionaries are flatten since #39 and I understand that it's not the optimal solution to have an extra list, but there's currently no other way to do that. Having a deep dictionary only adds complexity to the code and it wasn't necessarily being useful. I still think that using the dictionary to loop through values isn't the right way of doing this, but I'm open to discussion.
(about your workaround: I'd manually import pickBy
and includes
so the rest can get tree-shaken.)
from svelte-i18n.
I don't want it to be rendered, I just want to get back an object to then use it for a dropdown.
The dropdown take care of extracting each key value from the object.
I'll change the import 👍.
from svelte-i18n.
Ok, so for some reason my workaround only works server side.
In the browser the $dictionary is undefined.
event tho I have that
<script context="module">
import { waitLocale } from "svelte-i18n";
export async function preload() {
return waitLocale();
}
</script>
from svelte-i18n.
Can you provide a repro?
from svelte-i18n.
https://svelte.dev/repl/5a7d490941a243e3baec71e7d024e88e?version=3.18.2
from svelte-i18n.
The async function preload() { ... }
only works in sapper. If you're just using svelte, you can use the $loading
store: https://github.com/kaisermann/svelte-i18n/blob/master/docs/Locale.md#loading
from svelte-i18n.
I'm using Sapper.
Also
'loading' is not exported by node_modules/svelte-i18n/dist/runtime.esm.js
In my repro I used $isLoading, as you saw the repro doesn't work anyway.
from svelte-i18n.
Oh okay, so please provide a sapper repro, not a svelte one 😁 A simple .zip helps a lot.
And my bad, outdated doc. It's called $isLoading
! Just updated the docs.
from svelte-i18n.
My point was, shouldn't the svelte one works? because it doesn't 😥.
I'll create a zip.
from svelte-i18n.
Move those references to $dictionary and $locale to inside the $:
block: https://svelte.dev/repl/5589264db1f8400faf5e6465e21071ab?version=3.18.2
from svelte-i18n.
Here you are
https://github.com/RemiKalbe/svelte-i18n-repro-sdb
from svelte-i18n.
Hey @RemiKalbe 👋 Sorry for taking so long to answer you. It seems that when you're getting the current dictionary via let dic = $dictionary[$locale];
, in the client, the $locale
is, in example, en-US
. However, since the server doesn't have a navigator object, it sets its $locale
to en
. That's the reason for the difference between them both.
I'm thinking if it makes sense to return a merged dictionary of, in example, en-us
and en
when getting the dictionary for the en-US
locale. However, this kind of direct access to the current locale (full) dictionary wasn't in the scope of the project and I'm not sure if it's useful for many cases apart from yours. Making it return a "full" dictionary will involve changing a bit how the lib resolve messages and I'd need to cache and invalidate it whenever the dictionary is altered.
from svelte-i18n.
What would be great is that if $dictionary[$locale] of a given $locale doesn't exist it fallback to the default one set in the init.
from svelte-i18n.
For now, I'll just to that, thank you for solving my issue 😉
let dic =
typeof $dictionary[$locale] === "undefined"
? $dictionary["en"]
: $dictionary[$locale];
from svelte-i18n.
Related Issues (20)
- Support Svelte 4 HOT 3
- Allow unit tests with ts-node esm HOT 1
- Sveltekit: Update translation outside of component
- Variable inside html isn't replaced HOT 1
- Inform about unused language keys HOT 1
- When I hover over a <a> the site language changes to the default language. HOT 1
- `intl-getcanocicallocales` is probably not a good idea HOT 2
- CJS / ESM conflict HOT 22
- Change log version comparison links are off
- Production build using svelte-i18n ^3.7.4 throws "Named export 'IntlMessageFormat' not found." HOT 6
- Problems with LIB check in cli extractor HOT 1
- Sapper reference in Getting Started guide
- Allow to restrict some parts of the i18n file to unauthorized users in SvelteKit. HOT 3
- Is it possible when registering several json files to overwrite the previous key contents with latest file?
- Docs on how to use variables in translation strings HOT 1
- How to disable AM/PM in $time formatter
- Leading zeros for day/month when formatting date values
- Add a register store?
- Build error when deploying in vercel
- Provide a Readable<string> instead of pure string
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 svelte-i18n.