@oxog/i18n 0.1.1 → 0.1.2

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.
Files changed (2) hide show
  1. package/README.md +242 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,242 @@
1
+ # @oxog/i18n
2
+
3
+ A modern, lightweight, and framework-agnostic internationalization library with TypeScript support.
4
+
5
+ ## Features
6
+
7
+ - 🚀 **Lightweight**: Small bundle size with zero dependencies
8
+ - 🔥 **Fast**: Optimized performance with memoization
9
+ - 🌐 **Framework Agnostic**: Works with any JavaScript framework
10
+ - 📦 **TypeScript First**: Full TypeScript support with type safety
11
+ - 🔌 **Extensible**: Plugin system for custom functionality
12
+ - 🎯 **Simple API**: Easy to learn and use
13
+ - 💾 **Flexible Storage**: Support for multiple locale loading strategies
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @oxog/i18n
19
+ # or
20
+ pnpm add @oxog/i18n
21
+ # or
22
+ yarn add @oxog/i18n
23
+ ```
24
+
25
+ ## Quick Start
26
+
27
+ ```typescript
28
+ import { createI18n } from '@oxog/i18n';
29
+
30
+ // Create i18n instance
31
+ const i18n = createI18n({
32
+ locale: 'en',
33
+ fallbackLocale: 'en',
34
+ messages: {
35
+ en: {
36
+ greeting: 'Hello, {name}!',
37
+ messages: {
38
+ welcome: 'Welcome to our app'
39
+ }
40
+ },
41
+ es: {
42
+ greeting: '¡Hola, {name}!',
43
+ messages: {
44
+ welcome: 'Bienvenido a nuestra aplicación'
45
+ }
46
+ }
47
+ }
48
+ });
49
+
50
+ // Use translations
51
+ console.log(i18n.t('greeting', { name: 'World' })); // Hello, World!
52
+ console.log(i18n.t('messages.welcome')); // Welcome to our app
53
+
54
+ // Change locale
55
+ i18n.setLocale('es');
56
+ console.log(i18n.t('greeting', { name: 'Mundo' })); // ¡Hola, Mundo!
57
+ ```
58
+
59
+ ## API Reference
60
+
61
+ ### `createI18n(options)`
62
+
63
+ Creates a new i18n instance.
64
+
65
+ #### Options
66
+
67
+ - `locale`: Current locale (default: 'en')
68
+ - `fallbackLocale`: Fallback locale when translation is missing
69
+ - `messages`: Translation messages object
70
+ - `plugins`: Array of plugins to use
71
+ - `interpolation`: Custom interpolation options
72
+ - `missing`: Custom missing translation handler
73
+
74
+ ### Instance Methods
75
+
76
+ #### `t(key, params?, options?)`
77
+
78
+ Get a translation for the given key.
79
+
80
+ ```typescript
81
+ i18n.t('greeting', { name: 'Alice' });
82
+ i18n.t('user.profile.title');
83
+ ```
84
+
85
+ #### `setLocale(locale)`
86
+
87
+ Change the current locale.
88
+
89
+ ```typescript
90
+ i18n.setLocale('fr');
91
+ ```
92
+
93
+ #### `getLocale()`
94
+
95
+ Get the current locale.
96
+
97
+ ```typescript
98
+ const currentLocale = i18n.getLocale(); // 'en'
99
+ ```
100
+
101
+ #### `hasTranslation(key, locale?)`
102
+
103
+ Check if a translation exists.
104
+
105
+ ```typescript
106
+ if (i18n.hasTranslation('feature.title')) {
107
+ // Translation exists
108
+ }
109
+ ```
110
+
111
+ #### `addMessages(locale, messages)`
112
+
113
+ Add translations for a locale.
114
+
115
+ ```typescript
116
+ i18n.addMessages('de', {
117
+ greeting: 'Hallo, {name}!'
118
+ });
119
+ ```
120
+
121
+ ## Advanced Features
122
+
123
+ ### Interpolation
124
+
125
+ ```typescript
126
+ // Basic interpolation
127
+ i18n.t('greeting', { name: 'World' }); // Hello, World!
128
+
129
+ // Custom interpolation pattern
130
+ const i18n = createI18n({
131
+ interpolation: {
132
+ prefix: '{{',
133
+ suffix: '}}'
134
+ }
135
+ });
136
+ ```
137
+
138
+ ### Pluralization
139
+
140
+ ```typescript
141
+ const messages = {
142
+ items: {
143
+ zero: 'No items',
144
+ one: 'One item',
145
+ other: '{count} items'
146
+ }
147
+ };
148
+
149
+ i18n.t('items', { count: 0 }); // No items
150
+ i18n.t('items', { count: 1 }); // One item
151
+ i18n.t('items', { count: 5 }); // 5 items
152
+ ```
153
+
154
+ ### Plugins
155
+
156
+ ```typescript
157
+ import { createI18n, ICUPlugin } from '@oxog/i18n';
158
+
159
+ const i18n = createI18n({
160
+ plugins: [ICUPlugin()],
161
+ messages: {
162
+ en: {
163
+ greeting: 'Hello, {name}! You have {count, plural, =0 {no messages} =1 {one message} other {# messages}}.'
164
+ }
165
+ }
166
+ });
167
+ ```
168
+
169
+ ### Missing Translations
170
+
171
+ ```typescript
172
+ const i18n = createI18n({
173
+ missing: (key, locale) => {
174
+ console.warn(`Missing translation: ${key} for locale: ${locale}`);
175
+ return key; // Return the key as fallback
176
+ }
177
+ });
178
+ ```
179
+
180
+ ## TypeScript Support
181
+
182
+ ```typescript
183
+ // Define your message schema
184
+ type MessageSchema = {
185
+ greeting: string;
186
+ user: {
187
+ profile: {
188
+ title: string;
189
+ description: string;
190
+ };
191
+ };
192
+ };
193
+
194
+ // Create typed i18n instance
195
+ const i18n = createI18n<MessageSchema>({
196
+ locale: 'en',
197
+ messages: {
198
+ en: {
199
+ greeting: 'Hello!',
200
+ user: {
201
+ profile: {
202
+ title: 'Profile',
203
+ description: 'User profile page'
204
+ }
205
+ }
206
+ }
207
+ }
208
+ });
209
+
210
+ // Now you get full type safety
211
+ i18n.t('greeting'); // ✅ Valid
212
+ i18n.t('user.profile.title'); // ✅ Valid
213
+ i18n.t('invalid.key'); // ❌ TypeScript error
214
+ ```
215
+
216
+ ## Performance
217
+
218
+ The library is optimized for performance with:
219
+
220
+ - Memoization of translation lookups
221
+ - Efficient message traversal
222
+ - Minimal bundle size
223
+ - No runtime dependencies
224
+
225
+ ## Browser Support
226
+
227
+ - Modern browsers (Chrome, Firefox, Safari, Edge)
228
+ - Node.js 18+
229
+
230
+ ## License
231
+
232
+ MIT © Ersin Koç
233
+
234
+ ## Contributing
235
+
236
+ Contributions are welcome! Please read our [Contributing Guide](https://github.com/ersinkoc/i18n/blob/main/CONTRIBUTING.md) for details.
237
+
238
+ ## Links
239
+
240
+ - [GitHub Repository](https://github.com/ersinkoc/i18n)
241
+ - [NPM Package](https://www.npmjs.com/package/@oxog/i18n)
242
+ - [Documentation](https://github.com/ersinkoc/i18n#readme)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxog/i18n",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Modern, lightweight, and framework-agnostic internationalization library",
5
5
  "author": "Ersin Koç",
6
6
  "license": "MIT",