Git Product home page Git Product logo

Comments (15)

tw1t611 avatar tw1t611 commented on May 21, 2024 9

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.

kevinXmichael avatar kevinXmichael commented on May 21, 2024 5

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.

kaisermann avatar kaisermann commented on May 21, 2024 4

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.

kaisermann avatar kaisermann commented on May 21, 2024 3

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.

jarda-svoboda avatar jarda-svoboda commented on May 21, 2024 2

i was getting the same error until i used https://github.com/kaisermann/svelte-i18n/blob/master/docs/Locale.md#isloading

from svelte-i18n.

amzu-dev avatar amzu-dev commented on May 21, 2024 2

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.

inytar avatar inytar commented on May 21, 2024 1

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.

shijiezhou1 avatar shijiezhou1 commented on May 21, 2024 1

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.

zed-wong avatar zed-wong commented on May 21, 2024 1

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.

AlexanderBelkevich avatar AlexanderBelkevich commented on May 21, 2024

I also get the same error...

from svelte-i18n.

AlexanderBelkevich avatar AlexanderBelkevich commented on May 21, 2024

@kaisermann I have this error without sapper, on clear svelte

from svelte-i18n.

kaisermann avatar kaisermann commented on May 21, 2024

Would you be able to provide a repro project?

from svelte-i18n.

wootcake avatar wootcake commented on May 21, 2024

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.

juliabr avatar juliabr commented on May 21, 2024

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.

albertms10 avatar albertms10 commented on May 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.