https://github.com/marcmartinezdev/fast-react-i18n
https://github.com/marcmartinezdev/fast-react-i18n
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/marcmartinezdev/fast-react-i18n
- Owner: MarcMartinezDev
- Created: 2025-07-15T08:34:41.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-15T09:04:02.000Z (11 months ago)
- Last Synced: 2025-07-15T19:14:45.096Z (11 months ago)
- Language: TypeScript
- Size: 21.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π fast-react-i18n
Minimalist internationalization library for React with support for language detection, fallback, in-memory/localStorage caching, and dynamic loading of translations from JSON files.
---
## β¨ Characteristics
- β
Dynamic loading of translation files (/es.json, /en.json, etc.)
- π Automatic detection of the userβs language (?lang, cookies, browser)
- πΎ In-memory cache and optional persistence via localStorage
- π Automatic fallback if the language is not found
- β‘ Quick access hooks: useTranslation, useForceReloadTranslations, etc.
- π§½ Cache clearing function: clearTranslationsCache
- π Loading available languages via locales.json
---
## π Instalation
```bash
npm install fast-react-i18n
```
## π¦ Expected translations structure
```bash
public/
βββ locales/
βββ en.json
βββ es.json
βββ locales.json β ["en", "es"...]
```
## π§© Basic usage
1. Envolve your App
```bash
import { I18nProvider } from "fast-react-i18n";
```
2. Access translations
```bash
import { useTranslation } from "fast-react-i18n";
export const Home = () => {
const { translations } = useTranslation();
return
{translations.home?.title}
;
};
```
## π API
```bash
| Propiedad | Tipo | DescripciΓ³n |
|---------------------|-----------|---------------------------------------------------------------------------|
| `initialLocale` | `string?` | Initial Lang (`"en"`, `"es"`... default `"auto"`) |
| `fallbackLocale` | `string?` | Alternative language if the primary one fails |
| `translationsPath` | `string` | Base path to your translation files (e.g., /locales) |
| `persist` | `boolean?`| Whether to save in localStorage (default: false) |
```
## Functions
### useForceReloadTranslations()
Manually reloads the current language.
### detectUserLocale()
Detects the userβs preferred language, in this order:
- Query param ?lang=
- Cookie lang=
- navigator.language
- "en" (by default)
### clearTranslationsCache()
Deletes from localStorage all translations stored under keys starting with i18n\_.
### loadAvailableLocales(path: string): Promise
Loads an array with the languages defined in locales.json.
## π‘ Best practices
Use nested objects for your translations ({ home: { title: "" } })
Control the language with setLocale if you need to change it manually
Clear the cache if translations change dynamically (e.g., from a CMS)
Use fallbackLocale to avoid errors if a file is missing