Cannot format a message without first setting the initial locale when open _error route

See original GitHub issue

Describe the bug When I try open _error route with some translate in it or in _layout, library throw error

Logs image

To Reproduce

  1. Clone i18n example repo
  2. Add some translate in _error OR _layout ({$_('title.index', { default: 'Sapper project template!' })}, for example)
  3. Open dont exist page (http://localhost:3000/ds)

Information about your project:

  • Browser: Chrome 82.0.4067.0

  • OS: Windows 10

  • svelte-i18n version: latest

  • Bundler: Rollup

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
tw1t611commented, Jan 21, 2021

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",
};
4reactions
kevinXmichaelcommented, Dec 13, 2021

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}
Read more comments on GitHub >

github_iconTop Results From Across the Web

that's some error regarding svelte-i18n follow this instructions ...
Cannot format a message without first setting the initial locale. :( that's some error regarding svelte-i18n follow this instructions to make ...
Read more >
Spring framework: No message found under code for locale
In general such issue appears not because of non-existence locale, but because MessageBundle is configured improperly.
Read more >
How to Localize a Svelte App with svelte-i18n | Phrase
Walkthrough of the starter project code; Installing and setting up the svelte-i18n library; Dynamic, per-locale translation file loading ...
Read more >
Locale settings are not right. How can I reset them? - Ask Ubuntu
Using the fallback 'C' locale. The locale command produces error messages locale: Cannot set LC_CTYPE to default locale: No such file or directory...
Read more >
Svelte i18n Internationalization: how to localize a Svelte app
This Svelte localization tutorial explains how to setup and utilize ... an error that it cannot load without an initial locale being set....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found