Comments (15)
For everyone coming across this issue using svelte@next. Here is my solution for now.
Note that this is not prerendered at build-time.
routes/$layout.svelte
<script context="module">
// is not working atm for routes/* and on reload with snowpack
// export async function preload() {
// return waitLocale();
// }
</script>
<script>
import NavBar from "../components/navbar/Navbar.svelte";
import {
register,
t,
init,
getLocaleFromNavigator,
isLoading,
} from "svelte-i18n";
// note it's ending is .json, while the filename in static/ is .json.js
register("en", () => import("/lang/en.json"));
init({ initialLocale: getLocaleFromNavigator(), fallbackLocale: "en" });
</script>
{#if $isLoading}
Please wait...
{:else}
{$t("test")}
<NavBar />
<main>
<slot />
</main>
{/if}
routes/index.svelte
<script>
import { t } from "svelte-i18n";
</script>
{$t("test")
static/lang/en.json.js
export default {
test: "test",
};
from svelte-i18n.
For me it was a problem that I use i18n-keys before they where actually initialized, so I needed to await the result:
<script>
import {
register,
init,
_
} from 'svelte-i18n'
async function setup() {
register('en', () => import('@/i18n/en.json'))
return await Promise.allSettled([
// TODO: add some more stuff you want to init ...
init({ initialLocale: 'en', fallbackLocale: 'en' })
])
}
const setupResult = setup()
</script>
{#await setupResult}
<!-- <p>...show loading animation</p> -->
{:then}
<h1>{$_('setup.max_players')}</h1>
{:catch error}
<!-- <p style="color: red">{error.message}</p> -->
{/await}
from svelte-i18n.
As @jayk09 stated, the current solution is to use the isLoading
store to check if the library is still loading the messages while we don't have preload
support in error pages. You can also wrap your _layout.svelte
content with a loading state.
from svelte-i18n.
Hey @aksenovdev and @AlexanderBelkevich π It seems that when going to an error page, the _layout.svelte
preload
method is not accounted for. This is happening because the locale lazy loading is ending AFTER the error page is rendered. Will try to get back to you if I find some solution to this. I'm not sure if this is a known sapper behavior or a bug.
from svelte-i18n.
i was getting the same error until i used https://github.com/kaisermann/svelte-i18n/blob/master/docs/Locale.md#isloading
from svelte-i18n.
I had the same issue, adding this worked for me:
import { dictionary, locale, _, init} from 'svelte-i18n';
init({
fallbackLocale: 'en',
initialLocale: 'en',
})
Note that I have only one locale for now.
from svelte-i18n.
I'm seeing the same issue, with sapper, on first load of a page. The first time I open the app in my browser I get this error, when reloading or even closing the browser & reoping the page everything works as expected.
Can't reproduce in dev on a local machine, only when running as a google App Engine.
from svelte-i18n.
For everyone coming across this issue using svelte@next. Here is my solution for now.
Note that this is not prerendered at build-time.routes/$layout.svelte
<script context="module"> // is not working atm for routes/* and on reload with snowpack // export async function preload() { // return waitLocale(); // } </script> <script> import NavBar from "../components/navbar/Navbar.svelte"; import { register, t, init, getLocaleFromNavigator, isLoading, } from "svelte-i18n"; // note it's ending is .json, while the filename in static/ is .json.js register("en", () => import("/lang/en.json")); init({ initialLocale: getLocaleFromNavigator(), fallbackLocale: "en" }); </script> {#if $isLoading} Please wait... {:else} {$t("test")} <NavBar /> <main> <slot /> </main> {/if}routes/index.svelte
<script> import { t } from "svelte-i18n"; </script> {$t("test")static/lang/en.json.js
export default { test: "test", };
Can you explain why can't we load it async instead of waiting for all the translations to be ready at this point?
from svelte-i18n.
For me it was a problem that I use i18n-keys before they where actually initialized, so I needed to await the result:
<script> import { register, init, _ } from 'svelte-i18n' async function setup() { register('en', () => import('@/i18n/en.json')) return await Promise.allSettled([ // TODO: add some more stuff you want to init ... init({ initialLocale: 'en', fallbackLocale: 'en' }) ]) } const setupResult = setup() </script> {#await setupResult} <!-- <p>...show loading animation</p> --> {:then} <h1>{$_('setup.max_players')}</h1> {:catch error} <!-- <p style="color: red">{error.message}</p> --> {/await}
The best solution for me. Otherwise it will have too much duplicated code.
from svelte-i18n.
I also get the same error...
from svelte-i18n.
@kaisermann I have this error without sapper, on clear svelte
from svelte-i18n.
Would you be able to provide a repro project?
from svelte-i18n.
Same issue here, this also happens in the included example in the repo. If you run the example and go to a non existent page, you will find the error popping up in the console.
[svelte-i18n] Cannot format a message without first setting the initial locale.
from svelte-i18n.
Same issue here. Had to remove all translated $_ from the error page to fix it temporarily. Maybe someone has a better option.
from svelte-i18n.
I had the same issue, adding this worked for me:
import { dictionary, locale, _, init} from 'svelte-i18n'; init({ fallbackLocale: 'en', initialLocale: 'en', })
Note that I have only one locale for now.
Adding this code in the <script>
tag of _error.svelte
doesnβt seem to make it work for meβ¦ π
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.