Code Monkey home page Code Monkey logo

svelte-i18next's Introduction

svelte-i18next Tweet

CI npm version bundle size License

Svelte wrapper for i18next

npm i svelte-i18next i18next

Implementation

This library wraps an i18next instance in a Svelte Store to observe i18next events so your Svelte components re-render e.g. when language is changed or a new resource is loaded by i18next.

Quick Start

i18n.js:

import i18next from "i18next";
import { createI18nStore } from "svelte-i18next";

i18next.init({
 lng: 'en',
 resources: {
    en: {
      translation: {
        "key": "hello world"
      }
    }
  },
  interpolation: {
    escapeValue: false, // not needed for svelte as it escapes by default
  }
});

const i18n = createI18nStore(i18next);
export default i18n;

App.svelte:

<script>
  import i18n from './i18n.js';
</script>

<div>
    {$i18n.t('key')}
</div>

Usage with Sveltekit

Sveltekit shares stores across requests on server-side. This means that one users request could change the language setting of another users rendering if that is still ongoing. To avoid this issue, use setContext to create request-scoped store instances:

i18n.js:

import i18next from "i18next";
import { createI18nStore } from "svelte-i18next";

i18next.init({
 lng: 'en',
 resources: {
    en: {
      translation: {
        "key": "hello world"
      }
    }
  },
  interpolation: {
    escapeValue: false, // not needed for svelte as it escapes by default
  }
});

export default () => createI18nStore(i18next);

routes/+layout.svelte:

<script>
  import getI18nStore from "i18n.js";
  import { setContext } from "svelte";
  
  setContext('i18n', getI18nStore());
</script>

routes/+page.svelte:

<script>
  import { getContext } from "svelte";
  
  const i18n = getContext("i18n");
</script>

<div>
  <h1>{ $i18n.t("key") }</h1>
</div>

See full example project: Svelte example

svelte-i18next's People

Contributors

anttiviljami avatar hzpeng57 avatar lobidu avatar nishugoel avatar sapkra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

svelte-i18next's Issues

using dev (`vite dev`), every other page refresh.. `$isLoading` doesn't update from `true` to `false`

I found a workaround that seems to work OK..
just thought I'd drop a note, in case anyone wants to dig deeper and find a better fix.

The issue started when:

  • I added a 2nd namespace, with a JSON object having about 1000 keys

After doing so, any +page.svelte containing:

<pre>$isLoading = {$isLoading} ({typeof $isLoading})</pre>

{#if $isLoading === false}
  OK!
{/if}

..would finish loading about 50% to 70% of the time

My workaround was to modify how I initialize the libraries in my root layout:

import i18next from 'i18next'
import {createI18nStore, isLoading} from 'svelte-i18next'

i18next
  .init(
    {...},
    (error) => {
      isLoading.set(!!error)
    }
  )

feature idea: Trans component

It would be awesome to have also something like a Trans component, like in react-i18next.

That would help to interpolate components/html elements...

<Trans i18nKey="test.key">
    Hi from <a href="https://www.i18next.com" target="_blank" rel="noopener">i18next</a> ๐Ÿ‘‹
</Trans>

lang in url (with sveltekit)

Hi, thanks for this library!

I'm trying to use it with sveltekit. I have a path like /[lang]/about. Because I want to use i18n on all pages, and initialize it only once, I do so in /[lang]/+layout.svelte. How do I then access the i18n store in /[lang]/about/+page.svelte?

Extraction tool?

Can you recommend any extraction tool for creating i18next jsons that will work with svelte components?

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.