Giter VIP home page Giter VIP logo

typesafe-i18n-demo-svelte's Introduction

typesafe-i18n Svelte

This is a small project demonstrating a typesafe-i18n integration with Svelte.

LIVE-DEMO: https://typesafe-i18n-demo-svelte.vercel.app

This is a clone from the Vite Svelte Typescript starter template

Get started

Start Vite in development mode:

npm run dev

Navigate to http://localhost:3000. You should see the example app running.


Overview

Configure typesafe-i18n for an existing Svelte project

Initialize typesafe-i18n by running

npx typesafe-i18n --setup-auto

You could configure your development script to run the generator in parallel to vite by using npm-run-all.

{
   "scripts": {
      "dev": "npm-run-all --parallel vite typesafe-i18n",
      "vite": "vite",
      "typesafe-i18n": "typesafe-i18n",
   }
}

The generator will create some custom Svelte stores inside i18n-svelte.ts that you can use inside your components.

Then inside your root-component, you need to load your locales and call setLocale in order to setup all stores.

<script lang="ts">
   import { setLocale } from './i18n/i18n-svelte'

   // TODO: load locales (https://github.com/ivanhofer/typesafe-i18n#loading-locales)

   setLocale()
</script>

<!-- HTML markup -->

That's it. You can then start using typesafe-i18n inside your Svelte application.

<script lang="ts">
   import LL from './i18n/i18n-svelte'
</script>

<h1>{$LL.HELLO({ name: 'Svelte' })}</h1> <!-- "Hello Svelte!" -->

Stores

When running the generator, the file i18n-svelte.ts will export following functions and readable stores:

initI18n

Call it inside your root Svelte component in order to setup the stores:

<script>
   import { initI18n } from './i18n/i18n-svelte'

   initI18n('en')
</script>

LL

The default export of the generated file will be the store you can use to translate your app. You can use it with subscriptions ($LL) or as a regular JavaScript object (LL).

<script>
   import LL from './i18n/i18n-svelte'

   const showMessage = () => {
      alert(LL.SOME_MESSAGE())
   }
</script>

{$LL.HELLO({ name: 'world' })}

<button on:click={showMessage}>click me</button>

locale

This Svelte store will contain the current selected locale.

<script>
   import { locale } from './i18n/i18n-svelte'
</script>

<div>
   your language is: {$locale}
</div>

setLocale

If you want to change the locale, you need to call setLocale with the locale as an argument.

<script>
   import { setLocale } from './i18n/i18n-svelte'
</script>

<div id="language-picker">

   Choose language:

   <button on:click={() => setLocale('en')}>
      english
   </button>

   <button on:click={() => setLocale('de')}>
      deutsch
   </button>

   <button on:click={() => setLocale('it')}>
      italiano
   </button>

</div>

isLocaleLoading

Svelte store that returns a boolean. It can be used to wait for the locale to be loaded.

<script>
   import { isLocaleLoading } from './i18n/i18n-svelte'
</script>

{#if $isLocaleLoading}
   loading...
{:else}

   <!-- your app code goes here  -->

{/if}

SvelteKit

See here

Sapper

For your Sapper projects, you should call the initI18n function inside preload in your root routes/_layout.svelte file:

<script context="module">
   import { initI18n } from '../i18n/i18n-svelte'

   export async function preload(page, session) {
      // detect locale of user (see https://github.com/ivanhofer/typesafe-i18n#locale-detection)
      const locale = 'en'
      await initI18n(locale)
   }
</script>

For more information about the stores you can use, see the Svelte section.



For more information about typesafe-i18n, take a look at the main repository.

typesafe-i18n-demo-svelte's People

Contributors

ivanhofer avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

sunnypol92 gevera

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.