0.1.4 • Published 3 years ago

svelte-simple-i18n v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

:globe_with_meridians: svelte-simple-i18n

Light internationalization support for svelte.

npm

:book: Documentation

Install

yarn add svelte-simple-i18n
# or
npm i svelte-simple-i18n

Usage

  1. Prepare a language object.
// lang.js

export const enUS = 'en-US';
export const hiIn = 'hi-IN';
export const esMX = 'es-MX';

export const lang = {
  [enUS]: {
    onlyHere: 'only exists in english',
    foo: {
      baz: 'Translation in English',
    },
  },
  [hiIn]: {
    foo: {
      baz: 'हिंदी में अनुवाद',
    },
  },
  [esMX]: {
    foo: {
      baz: 'Traducción en español',
    },
  },
};
  1. Create i18n module.
// i18n.js

import { lang, hiIN, enUS } from './lang';

export const { t, locale } = createI18n({
  messages: lang,
  locale: hiIN,
  fallbackLocale: enUS,
});
  1. Use in you components.
<script>
  import { t, locale } from './i18n';
  import { esMX } from './lang';

  function changeLocale() {
    $locale = esMX;
  }
</script>

<div>
  {$locale}
  <!--= hi-IN -->

  {$t('foo.baz')}
  <!--= हिंदी में अनुवाद -->

  <!-- fallback -->
  {$t('onlyHere')}
  <!--= only exists in english -->

  <button on:click="{changeLocale}">Change Locale</button>
</div>
0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago