@fisharmy100/react-auto-i18n 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,199 @@
1
+ # react-auto-i18n
2
+
3
+ A React internationalization (i18n) library with automatic translation support. Wrap your app in a provider, mark strings with `__t()`, and let the CLI generate your translation database.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install react-auto-i18n
11
+ ```
12
+
13
+ ---
14
+
15
+ ## Quick Start
16
+
17
+ ### 1. Wrap your app with `I18nProvider`
18
+
19
+ ```tsx
20
+ import { StrictMode } from 'react'
21
+ import { createRoot } from 'react-dom/client'
22
+ import App from './App.tsx'
23
+ import { I18nProvider } from 'react-auto-i18n'
24
+ import db from './assets/translations.json'
25
+
26
+ createRoot(document.getElementById('root')!).render(
27
+ <I18nProvider defaultLang="eng_Latn" defaultDatabase={db}>
28
+ <App />
29
+ </I18nProvider>
30
+ )
31
+ ```
32
+
33
+ ### 2. Mark strings for translation
34
+
35
+ ```tsx
36
+ import { __t } from 'react-auto-i18n'
37
+
38
+ function MyComponent() {
39
+ return <p>{__t("greeting", "Hello, world!")}</p>
40
+ }
41
+ ```
42
+
43
+ ### 3. Generate translations with the CLI
44
+
45
+ ```bash
46
+ npx auto-i18n-cli -i "./src/**/*.ts" -o "./assets/translations.json" \
47
+ -l spa_Latn -s eng_Latn -b azure --azureKey "YOUR_KEY"
48
+ ```
49
+
50
+ This scans your source files for all `__t()` and `__tv()` calls and outputs a JSON translation database.
51
+
52
+ ---
53
+
54
+ ## Translation Functions
55
+
56
+ ### `__t(key, message, args?)` — Simple translation
57
+
58
+ ```ts
59
+ const msg = __t("welcome", "Welcome to our app!")
60
+ // In Spanish: "¡Bienvenido a nuestra aplicación!"
61
+ ```
62
+
63
+ **Escape code:** Wrap text in `{{...}}` to prevent it from being translated:
64
+
65
+ ```ts
66
+ const msg = __t("info", "This is powered by {{'react-auto-i18n'}}")
67
+ // The library name is preserved as-is
68
+ ```
69
+
70
+ ### `__tv(key, messages, args)` — Plural / variant translation
71
+
72
+ Use this when the message varies based on a runtime value:
73
+
74
+ ```ts
75
+ const msg = __tv("apple.count", [
76
+ ["You have one apple", ({ count }) => count === 1],
77
+ ["You have {{$count}} apples", ({ count }) => count > 1],
78
+ "You have no apples"
79
+ ], { count: appleCount })
80
+ ```
81
+
82
+ `{{$count}}` is a variable placeholder — it's replaced at runtime with the value from `args` and is never translated.
83
+
84
+ ---
85
+
86
+ ## Changing the Locale at Runtime
87
+
88
+ Use the `useI18n` hook inside any component wrapped by `I18nProvider`:
89
+
90
+ ```tsx
91
+ import { useI18n, __t } from 'react-auto-i18n'
92
+
93
+ function LanguageSwitcher() {
94
+ const i18n = useI18n()
95
+
96
+ return (
97
+ <button onClick={() => i18n.setLocale("fra_Latn")}>
98
+ Switch to French
99
+ </button>
100
+ )
101
+ }
102
+ ```
103
+
104
+ The `useI18n()` hook also exposes:
105
+
106
+ | Property | Description |
107
+ |---|---|
108
+ | `locale` | The currently active `LangScriptCode` |
109
+ | `setLocale(locale)` | Update the active locale |
110
+ | `database` | The currently loaded `I18nDatabase` |
111
+ | `setDatabase(db)` | Swap out the translation database |
112
+ | `getLocales()` | List all locales present in the database |
113
+ | `getLocaleObj()` | Get a `LangScriptObj` wrapper for the current locale |
114
+
115
+ ---
116
+
117
+ ## Language & Script Codes
118
+
119
+ Locales are identified by a `LangScriptCode` in the format `{lang}_{Script}`, e.g.:
120
+
121
+ | Code | Language |
122
+ |---|---|
123
+ | `eng_Latn` | English (Latin) |
124
+ | `spa_Latn` | Spanish (Latin) |
125
+ | `cmn_Hans` | Mandarin (Simplified) |
126
+ | `arb_Arab` | Arabic |
127
+ | `jpn_Jpan` | Japanese |
128
+
129
+ The library supports 200+ language-script combinations. Utility functions are provided to work with these codes:
130
+
131
+ ```ts
132
+ import { getLangCode, getScriptCode, getEnglishLangName, getCountryCode } from 'react-auto-i18n'
133
+
134
+ getLangCode("eng_Latn") // "eng"
135
+ getScriptCode("eng_Latn") // "Latn"
136
+ getEnglishLangName("spa") // "Spanish"
137
+ getCountryCode("eng") // "US"
138
+ ```
139
+
140
+ You can also use the `LangScriptObj` class for an OOP-style interface:
141
+
142
+ ```ts
143
+ import { LangScriptObj } from 'react-auto-i18n'
144
+
145
+ const lang = new LangScriptObj("jpn_Jpan")
146
+ lang.getLangCode() // "jpn"
147
+ lang.getScriptCode() // "Jpan"
148
+ lang.getEnglishName() // "Japanese"
149
+ lang.getCountry() // "JP"
150
+ lang.getCountryFlag() // <CountryFlag country="JP" />
151
+ ```
152
+
153
+ ---
154
+
155
+ ## Translation Database Format
156
+
157
+ The database is a JSON object keyed by `LangScriptCode`:
158
+
159
+ ```json
160
+ {
161
+ "eng_Latn": {
162
+ "greeting": "Hello!",
163
+ "apple.count": ["You have one apple", "You have {{$count}} apples", "You have no apples"]
164
+ },
165
+ "spa_Latn": {
166
+ "greeting": "¡Hola!",
167
+ "apple.count": ["Tienes una manzana", "Tienes {{$count}} manzanas", "No tienes manzanas"]
168
+ }
169
+ }
170
+ ```
171
+
172
+ The type `I18nDatabase` is defined as `Partial<Record<LangScriptCode, LanguageTranslations>>`.
173
+
174
+ ---
175
+
176
+ ## Low-Level API
177
+
178
+ These functions operate on the global translation state directly. Prefer the React hooks when possible.
179
+
180
+ ```ts
181
+ import { setI18nDatabaseRaw, setCurrentLocalRaw, getCurrentLocalRaw, getI18nDatabaseRaw } from 'react-auto-i18n'
182
+
183
+ setI18nDatabaseRaw(db)
184
+ setCurrentLocalRaw("deu_Latn")
185
+ ```
186
+
187
+ ---
188
+
189
+ ## Validation Helpers
190
+
191
+ Safely parse untrusted strings into typed codes (returns `null` if invalid):
192
+
193
+ ```ts
194
+ import { stringToLangCode, stringToScriptCode, stringToLangScriptCode } from 'react-auto-i18n'
195
+
196
+ stringToLangCode("eng") // "eng"
197
+ stringToLangCode("not_a_lang") // null
198
+ stringToLangScriptCode("fra_Latn") // "fra_Latn"
199
+ ```