1.0.2 • Published 1 year ago

react-stupid-i18n v1.0.2

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

🌏 react-stupid-i18n

NPM JavaScript Style Guide Bundle Size

  • 🤘 100% type-safe, infering all locales keys.
  • 🤩 No Provider wrapper needed, powered by preact signals;
  • 🏆 Better Developer Experience (DX);
  • 🗜 Small bundle;

Install

npm i react-stupid-i18n

Setup

import { createInstance } from 'react-stupid-i18n';

// must have same structure
import en_US from './locales/en_US';
import pt_BR from './locales/pt_BR';

export const {
  t, setLanguage, language, avaliableLanguages,
} = createInstance({
  en_US,
  pt_BR,
}, 'en_US');

Your locale files:

./locales/en_US.ts:

{
  "hello": "Hi {user}",
  "greetings": {
    "morning": "Good morning {user}",
    "evening": "Good evening {user}",
    "casual": {
      "morning": "Yo, happy morning",
      "afternoon": "Yolo"
    }
  }
}

In your app:

import React from 'react';
import { t } from './i18n';

function App() {
  return (
    <div className="App">
      {t('greetings.evening', { user: 'Eduardo' })}
    </div>
  );
}

Using JSON locale files

You can use JSON files and be 100% type-safe;

// must have same structure
import en_US from './locales/en_US.json';
import pt_BR from './locales/pt_BR.json';

You just need to enable resolveJsonModule in your tsconfig.json file:

{
  ...
  "compilerOptions": {
    "resolveJsonModule": true,
  },
  ...
}
1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago