@nice2dev/i18n 1.0.3
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/dist/index.cjs +24 -0
- package/dist/index.d.ts +439 -0
- package/dist/index.mjs +5765 -0
- package/package.json +57 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nice2dev/i18n
|
|
3
|
+
* Shared internationalization infrastructure for Nice2Dev UI libraries.
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - NiceI18nProvider for React context-based translations
|
|
7
|
+
* - Built-in dictionaries for 20 languages
|
|
8
|
+
* - ICU MessageFormat with pluralization, gender, ordinals, and select
|
|
9
|
+
* - Browser language detection
|
|
10
|
+
* - Custom translation function support (e.g. react-i18next)
|
|
11
|
+
*
|
|
12
|
+
* @packageDocumentation
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { default as default_2 } from 'react';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Check common RTL issues in styles.
|
|
19
|
+
* This is a static analysis helper for development.
|
|
20
|
+
*/
|
|
21
|
+
export declare function analyzeStylesForRTL(styles: Record<string, string | number>): string[];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Create a locale-aware currency formatter.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createCurrencyFormatter(lang: NiceSupportedLang, currency?: string): NiceCurrencyFormatter;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Create a locale-aware date formatter.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createDateFormatter(lang: NiceSupportedLang, style?: DateFormatStyle): NiceDateFormatter;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Create a translator function that supports ICU MessageFormat.
|
|
35
|
+
*/
|
|
36
|
+
export declare function createICUTranslator(dict: Record<string, string>, lang?: NiceSupportedLang): (key: string, defaultValue: string, values?: Record<string, string | number | Date>) => string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Create a locale-aware number formatter.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createNumberFormatter(lang: NiceSupportedLang): NiceNumberFormatter;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a locale-aware time formatter.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createTimeFormatter(lang: NiceSupportedLang, style?: DateFormatStyle): NiceTimeFormatter;
|
|
47
|
+
|
|
48
|
+
/** Get a translation function without React context (for non-component code). */
|
|
49
|
+
export declare function createTranslator(lang?: NiceSupportedLang, overrides?: Record<string, string>): NiceTranslateFn;
|
|
50
|
+
|
|
51
|
+
export declare type DateFormatStyle = 'short' | 'medium' | 'long' | 'full';
|
|
52
|
+
|
|
53
|
+
/** Get user's preferred language from browser. */
|
|
54
|
+
export declare function detectBrowserLanguage(): NiceSupportedLang;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Find fuzzy matches in existing translations for reuse.
|
|
58
|
+
* Compares the English value to find similar existing translations.
|
|
59
|
+
*/
|
|
60
|
+
export declare function findSimilarTranslation(englishValue: string, lang: NiceSupportedLang, threshold?: number): {
|
|
61
|
+
key: string;
|
|
62
|
+
english: string;
|
|
63
|
+
translated: string;
|
|
64
|
+
similarity: number;
|
|
65
|
+
} | undefined;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Flip a horizontal alignment for RTL.
|
|
69
|
+
* start ↔ end, left ↔ right
|
|
70
|
+
*/
|
|
71
|
+
export declare function flipAlignment(align: string, isRTL: boolean): string;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Flip a horizontal position for RTL.
|
|
75
|
+
* left ↔ right
|
|
76
|
+
*/
|
|
77
|
+
export declare function flipHorizontal(position: 'left' | 'right'): 'left' | 'right';
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Format an ICU MessageFormat string with values.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* // Simple placeholder
|
|
84
|
+
* formatICU('Hello, {name}!', { name: 'World' }) // 'Hello, World!'
|
|
85
|
+
*
|
|
86
|
+
* // Plural
|
|
87
|
+
* formatICU('{count, plural, one{# item} other{# items}}', { count: 5 }, { lang: 'en' }) // '5 items'
|
|
88
|
+
*
|
|
89
|
+
* // Select (gender)
|
|
90
|
+
* formatICU('{gender, select, male{He} female{She} other{They}} liked it', { gender: 'female' }) // 'She liked it'
|
|
91
|
+
*
|
|
92
|
+
* // Ordinal
|
|
93
|
+
* formatICU('{pos, selectordinal, one{#st} two{#nd} few{#rd} other{#th}}', { pos: 3 }, { lang: 'en' }) // '3rd'
|
|
94
|
+
*
|
|
95
|
+
* // Number formatting
|
|
96
|
+
* formatICU('Price: {price, number, currency}', { price: 99.99 }) // 'Price: $99.99'
|
|
97
|
+
*
|
|
98
|
+
* // Date formatting
|
|
99
|
+
* formatICU('Date: {date, date, long}', { date: new Date() }) // 'Date: January 15, 2025'
|
|
100
|
+
*/
|
|
101
|
+
export declare function formatICU(template: string, values?: Record<string, string | number | Date>, options?: ICUFormatOptions): string;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* ICU MessageFormat-lite interpolation.
|
|
105
|
+
* Supports basic {placeholder} and {plural, count, ...} patterns.
|
|
106
|
+
*/
|
|
107
|
+
export declare function formatMessage(template: string, values?: Record<string, string | number>): string;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Generate a coverage report in markdown format.
|
|
111
|
+
*/
|
|
112
|
+
export declare function generateCoverageReport(): string;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Generate TypeScript code for translations to be added to the dictionary.
|
|
116
|
+
*/
|
|
117
|
+
export declare function generateTranslationCode(lang: NiceSupportedLang, translations: TranslationResult[]): string;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Get translation statistics for all languages.
|
|
121
|
+
*/
|
|
122
|
+
export declare function getAllTranslationStats(): TranslationStats[];
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Get the current document direction.
|
|
126
|
+
*/
|
|
127
|
+
export declare function getDocumentDirection(): 'ltr' | 'rtl';
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Get all glossary entries for a specific language.
|
|
131
|
+
*/
|
|
132
|
+
export declare function getGlossaryForLanguage(lang: NiceSupportedLang): Array<{
|
|
133
|
+
en: string;
|
|
134
|
+
translation: string;
|
|
135
|
+
}>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Get the BCP 47 locale tag for a NiceSupportedLang code.
|
|
139
|
+
*/
|
|
140
|
+
export declare function getLocaleTag(lang: NiceSupportedLang): string;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Get logical property names based on physical ones.
|
|
144
|
+
* Useful for generating RTL-safe CSS.
|
|
145
|
+
*/
|
|
146
|
+
export declare function getLogicalProperty(physical: string): string;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Find missing translations for a specific language.
|
|
150
|
+
*/
|
|
151
|
+
export declare function getMissingTranslations(lang: NiceSupportedLang): MissingTranslation[];
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Get ordinal category for a number in a given language.
|
|
155
|
+
* e.g., English: 1st, 2nd, 3rd, 4th...
|
|
156
|
+
*/
|
|
157
|
+
export declare function getOrdinalCategory(n: number, lang: NiceSupportedLang): OrdinalCategory;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Get plural category for a number in a given language.
|
|
161
|
+
* Implements CLDR plural rules for supported languages.
|
|
162
|
+
*/
|
|
163
|
+
export declare function getPluralCategory(n: number, lang: NiceSupportedLang): PluralCategory;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Generate CSS custom property values for RTL support.
|
|
167
|
+
*/
|
|
168
|
+
export declare function getRTLCSSVars(isRTL: boolean): Record<string, string>;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Get all keys from the English dictionary (source of truth).
|
|
172
|
+
*/
|
|
173
|
+
export declare function getSourceKeys(): string[];
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Get text direction for a language.
|
|
177
|
+
*/
|
|
178
|
+
export declare function getTextDirection(lang: string): 'ltr' | 'rtl';
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Get translation statistics for a language.
|
|
182
|
+
*/
|
|
183
|
+
export declare function getTranslationStats(lang: NiceSupportedLang): TranslationStats;
|
|
184
|
+
|
|
185
|
+
export declare interface GlossaryEntry {
|
|
186
|
+
en: string;
|
|
187
|
+
translations: Partial<Record<NiceSupportedLang, string>>;
|
|
188
|
+
context?: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export declare interface ICUFormatOptions {
|
|
192
|
+
lang?: NiceSupportedLang;
|
|
193
|
+
locale?: string;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Check if a language uses RTL text direction.
|
|
198
|
+
*/
|
|
199
|
+
export declare function isRTLLanguage(lang: string): boolean;
|
|
200
|
+
|
|
201
|
+
/** Check if a language is supported. */
|
|
202
|
+
export declare function isSupported(lang: string): lang is NiceSupportedLang;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Look up a term in the glossary for a given language.
|
|
206
|
+
* Returns the approved translation or undefined.
|
|
207
|
+
*/
|
|
208
|
+
export declare function lookupGlossary(englishTerm: string, lang: NiceSupportedLang): string | undefined;
|
|
209
|
+
|
|
210
|
+
export declare interface MissingTranslation {
|
|
211
|
+
key: string;
|
|
212
|
+
englishValue: string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export declare const NICE_GLOSSARY: GlossaryEntry[];
|
|
216
|
+
|
|
217
|
+
export declare const NICE_I18N_DICTIONARIES: Record<NiceSupportedLang, Record<string, string>>;
|
|
218
|
+
|
|
219
|
+
export declare const NICE_SUPPORTED_LANGS: readonly ["en", "pl", "de", "fr", "es", "it", "pt", "nl", "sv", "no", "da", "fi", "cs", "sk", "hu", "ro", "bg", "uk", "ja", "ko", "zh", "ar"];
|
|
220
|
+
|
|
221
|
+
export declare interface NiceCurrencyFormatter {
|
|
222
|
+
format: (value: number) => string;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export declare interface NiceDateFormatter {
|
|
226
|
+
format: (date: Date | number) => string;
|
|
227
|
+
formatRange: (start: Date | number, end: Date | number) => string;
|
|
228
|
+
formatRelative: (date: Date | number) => string;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Provider for plugging in your own translation function (e.g. from react-i18next)
|
|
233
|
+
* OR using built-in dictionaries via the `lang` prop.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* // Mode 1 — Built-in dictionaries (20 languages out of the box):
|
|
237
|
+
* <NiceI18nProvider lang="pl">
|
|
238
|
+
* <MyApp />
|
|
239
|
+
* </NiceI18nProvider>
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* // Mode 2 — Custom translation function (e.g. react-i18next):
|
|
243
|
+
* import { useTranslation } from 'react-i18next';
|
|
244
|
+
* function App() {
|
|
245
|
+
* const { t } = useTranslation();
|
|
246
|
+
* return (
|
|
247
|
+
* <NiceI18nProvider t={t}>
|
|
248
|
+
* <MyPage />
|
|
249
|
+
* </NiceI18nProvider>
|
|
250
|
+
* );
|
|
251
|
+
* }
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* // Mode 3 — No provider, English defaults work out of the box:
|
|
255
|
+
* <NiceButton>Click me</NiceButton>
|
|
256
|
+
*/
|
|
257
|
+
export declare const NiceI18nProvider: default_2.FC<NiceI18nProviderProps>;
|
|
258
|
+
|
|
259
|
+
export declare interface NiceI18nProviderProps {
|
|
260
|
+
/** Custom translation function — takes priority over `lang`. */
|
|
261
|
+
t?: NiceTranslateFn;
|
|
262
|
+
/** Built-in language code. Uses bundled dictionaries for 20 languages. */
|
|
263
|
+
lang?: NiceSupportedLang;
|
|
264
|
+
/** Extra translations to merge on top of built-in dictionary. */
|
|
265
|
+
overrides?: Record<string, string>;
|
|
266
|
+
children: default_2.ReactNode;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export declare interface NiceNumberFormatter {
|
|
270
|
+
format: (value: number) => string;
|
|
271
|
+
formatCompact: (value: number) => string;
|
|
272
|
+
formatPercent: (value: number) => string;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export declare type NiceSupportedLang = (typeof NICE_SUPPORTED_LANGS)[number];
|
|
276
|
+
|
|
277
|
+
export declare interface NiceTimeFormatter {
|
|
278
|
+
format: (date: Date | number) => string;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Translation function type.
|
|
283
|
+
* Accepts a key and a default/fallback value, returns the translated string.
|
|
284
|
+
*/
|
|
285
|
+
export declare type NiceTranslateFn = (key: string, defaultValue: string) => string;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Type-safe ordinal helper.
|
|
289
|
+
*/
|
|
290
|
+
export declare function ordinal(n: number, forms: Partial<Record<OrdinalCategory, string>> & {
|
|
291
|
+
other: string;
|
|
292
|
+
}, lang?: NiceSupportedLang): string;
|
|
293
|
+
|
|
294
|
+
export declare type OrdinalCategory = 'one' | 'two' | 'few' | 'other';
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Type-safe plural helper.
|
|
298
|
+
* Returns the appropriate form based on count.
|
|
299
|
+
*/
|
|
300
|
+
export declare function plural(count: number, forms: Partial<Record<PluralCategory, string>> & {
|
|
301
|
+
other: string;
|
|
302
|
+
}, lang?: NiceSupportedLang): string;
|
|
303
|
+
|
|
304
|
+
export declare type PluralCategory = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* CSS custom properties for RTL-aware components.
|
|
308
|
+
* Use these as fallbacks in CSS-in-JS or CSS modules.
|
|
309
|
+
*/
|
|
310
|
+
export declare const RTL_CSS_VARS: {
|
|
311
|
+
readonly dirMultiplier: "--nice-dir-multiplier";
|
|
312
|
+
readonly inlineStart: "--nice-inline-start";
|
|
313
|
+
readonly inlineEnd: "--nice-inline-end";
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Languages that use right-to-left text direction.
|
|
318
|
+
* Currently not in NICE_SUPPORTED_LANGS but planned for future.
|
|
319
|
+
*/
|
|
320
|
+
export declare const RTL_LANGUAGES: readonly ["ar", "he", "fa", "ur", "yi", "ps"];
|
|
321
|
+
|
|
322
|
+
export declare interface RTLContextValue {
|
|
323
|
+
/** Current text direction */
|
|
324
|
+
dir: 'ltr' | 'rtl';
|
|
325
|
+
/** Whether current direction is RTL */
|
|
326
|
+
isRTL: boolean;
|
|
327
|
+
/** Flip alignment helper */
|
|
328
|
+
flip: (align: string) => string;
|
|
329
|
+
/** Get RTL-aware value */
|
|
330
|
+
value: <T>(values: {
|
|
331
|
+
ltr: T;
|
|
332
|
+
rtl: T;
|
|
333
|
+
}) => T;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export declare type RTLLanguage = typeof RTL_LANGUAGES[number];
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Provider for RTL context.
|
|
340
|
+
* Automatically detects RTL languages and provides helpers.
|
|
341
|
+
*/
|
|
342
|
+
export declare const RTLProvider: default_2.FC<RTLProviderProps>;
|
|
343
|
+
|
|
344
|
+
export declare interface RTLProviderProps {
|
|
345
|
+
/** Language code to determine direction */
|
|
346
|
+
lang: string;
|
|
347
|
+
/** Whether to automatically set document direction */
|
|
348
|
+
autoSetDocument?: boolean;
|
|
349
|
+
children: default_2.ReactNode;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Get the appropriate value based on text direction.
|
|
354
|
+
*/
|
|
355
|
+
export declare function rtlValue<T>(values: {
|
|
356
|
+
ltr: T;
|
|
357
|
+
rtl: T;
|
|
358
|
+
}, isRTL: boolean): T;
|
|
359
|
+
|
|
360
|
+
export declare interface RTLVerificationResult {
|
|
361
|
+
component: string;
|
|
362
|
+
issues: string[];
|
|
363
|
+
recommendations: string[];
|
|
364
|
+
isRTLReady: boolean;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Type-safe select helper (for gender, etc.).
|
|
369
|
+
*/
|
|
370
|
+
export declare function select<T extends string>(value: T, forms: Partial<Record<T, string>> & {
|
|
371
|
+
other: string;
|
|
372
|
+
}): string;
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Set the document's text direction and lang attribute.
|
|
376
|
+
* Updates both <html> attributes for proper RTL support.
|
|
377
|
+
*/
|
|
378
|
+
export declare function setDocumentDirection(lang: string): void;
|
|
379
|
+
|
|
380
|
+
export declare interface TranslationResult {
|
|
381
|
+
key: string;
|
|
382
|
+
original: string;
|
|
383
|
+
translated: string;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export declare interface TranslationStats {
|
|
387
|
+
lang: NiceSupportedLang;
|
|
388
|
+
totalKeys: number;
|
|
389
|
+
translatedKeys: number;
|
|
390
|
+
missingKeys: number;
|
|
391
|
+
coverage: number;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Hook that returns true if current layout is RTL.
|
|
396
|
+
*/
|
|
397
|
+
export declare function useIsRTL(): boolean;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* React hook: locale-aware currency formatting.
|
|
401
|
+
*/
|
|
402
|
+
export declare function useNiceCurrencyFormat(lang: NiceSupportedLang, currency?: string): NiceCurrencyFormatter;
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* React hook: locale-aware date formatting.
|
|
406
|
+
*/
|
|
407
|
+
export declare function useNiceDateFormat(lang: NiceSupportedLang, style?: DateFormatStyle): NiceDateFormatter;
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* React hook: locale-aware number formatting.
|
|
411
|
+
*/
|
|
412
|
+
export declare function useNiceNumberFormat(lang: NiceSupportedLang): NiceNumberFormatter;
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* React hook: locale-aware time formatting.
|
|
416
|
+
*/
|
|
417
|
+
export declare function useNiceTimeFormat(lang: NiceSupportedLang, style?: DateFormatStyle): NiceTimeFormatter;
|
|
418
|
+
|
|
419
|
+
/** Internal hook used by Nice2Dev UI components. */
|
|
420
|
+
export declare function useNiceTranslation(): {
|
|
421
|
+
t: NiceTranslateFn;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Hook to access RTL context and helpers.
|
|
426
|
+
*/
|
|
427
|
+
export declare function useRTL(): RTLContextValue;
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Validate translations against the glossary.
|
|
431
|
+
* Returns entries where the translation doesn't match the glossary.
|
|
432
|
+
*/
|
|
433
|
+
export declare function validateAgainstGlossary(lang: NiceSupportedLang): Array<{
|
|
434
|
+
key: string;
|
|
435
|
+
currentTranslation: string;
|
|
436
|
+
glossaryTranslation: string;
|
|
437
|
+
}>;
|
|
438
|
+
|
|
439
|
+
export { }
|