locon 1.4.6 → 1.5.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/dist/index.d.ts CHANGED
@@ -1,19 +1,196 @@
1
1
  import * as react from 'react';
2
- import { ReactElement, PropsWithChildren } from 'react';
3
- import LoconContext$1 from './types/LoconContext.js';
4
- export { default as LText } from './components/LText.js';
5
- export { default as useLocon } from './hooks/useLocon.js';
6
- import './types/Assets.js';
7
- import 'react-native';
2
+ import { ComponentProps, ReactElement, PropsWithChildren } from 'react';
3
+ import { Text } from 'react-native';
4
+
5
+ type Assets = Record<string, Record<string, string>>;
6
+
7
+ /**
8
+ * Per-call options for `l()`, `lIn()` and `<LText>`.
9
+ *
10
+ * All four are optional and independent: `params` interpolates, `count`
11
+ * selects a plural form, and `locale` renders one call in a language other
12
+ * than the current one (used when an app exports a document in a language
13
+ * the interface is not currently showing). `fallback` is returned only when
14
+ * no locale resolves the input.
15
+ */
16
+ interface TranslateOptions {
17
+ /** Values for `{token}` placeholders inside the resolved string. */
18
+ params?: Record<string, string | number>;
19
+ /** Selects `key_one` / `key_few` / `key_other` … via `Intl.PluralRules`. */
20
+ count?: number;
21
+ /** Resolve against this locale instead of the current one. */
22
+ locale?: string;
23
+ /** Human-readable fallback when no locale resolves the key/value. */
24
+ fallback?: string;
25
+ }
26
+
27
+ interface LoconContext$1 {
28
+ /** Localizes a key — or a phrase in the project language. */
29
+ l: (assetKey: string, options?: TranslateOptions) => string;
30
+ /** Localizes into an explicit locale, whatever the current one is. */
31
+ lIn: (locale: string, assetKey: string, options?: Omit<TranslateOptions, 'locale'>) => string;
32
+ assets: Assets;
33
+ /** Every locale present in `assets`. */
34
+ locales: string[];
35
+ currentLocale: string;
36
+ defaultLocale: string;
37
+ projectLocale: string;
38
+ /** Locale detected from the device, or `null` when none matched. */
39
+ systemLocale: string | null;
40
+ /** Whether `currentLocale` is written right-to-left. */
41
+ isRTL: boolean;
42
+ autodetect: boolean;
43
+ /** Switches locale. Pass `null` to go back to following the device. */
44
+ setLocale: (locale: string | null) => void;
45
+ }
46
+
47
+ interface LTextProps extends ComponentProps<typeof Text> {
48
+ children: string;
49
+ assetKey?: string;
50
+ /** Values for `{token}` placeholders. */
51
+ params?: Record<string, string | number>;
52
+ /** Selects a plural form via `Intl.PluralRules`. */
53
+ count?: number;
54
+ /** Renders in this locale instead of the current one. */
55
+ locale?: string;
56
+ }
57
+ declare function LText({ children, assetKey, params, count, locale, ...props }: LTextProps): ReactElement;
58
+
59
+ declare function useLocon(): LoconContext$1;
60
+
61
+ /**
62
+ * The resolver behind `l()`, usable without React.
63
+ *
64
+ * The provider builds one of these per render; export pipelines (PDF, XLSX,
65
+ * e-mail) build their own with an explicit locale, so a document can be
66
+ * produced in a language the interface is not currently showing.
67
+ */
68
+
69
+ interface TranslatorConfig {
70
+ assets: Assets;
71
+ locale: string;
72
+ defaultLocale?: string;
73
+ projectLocale?: string;
74
+ }
75
+ type Translator = (assetKey: string, options?: TranslateOptions) => string;
76
+ declare function createTranslator({ assets, locale, defaultLocale, projectLocale }: TranslatorConfig): Translator;
77
+
78
+ /**
79
+ * Best available locale for this device, or `null` when none of the device's
80
+ * languages is one the app ships.
81
+ */
82
+ declare function getSystemLanguage(availableLocales?: string[]): string | null;
83
+
84
+ /**
85
+ * Decides which locale to display.
86
+ *
87
+ * The provider uses this, and so can anything running outside React — a
88
+ * background task building a notification, an export pipeline, a native
89
+ * bridge. Keeping the precedence in one place is the point: an app that
90
+ * re-derives "explicit choice, else device, else default" on its own will
91
+ * eventually disagree with the provider about what language it is in.
92
+ */
93
+
94
+ interface ResolveLocaleConfig {
95
+ assets: Assets;
96
+ /** An explicit choice, `null` to follow the device, `undefined` for neither. */
97
+ currentLocale?: string | null;
98
+ defaultLocale?: string;
99
+ autodetect?: boolean;
100
+ /**
101
+ * A device locale already detected by the caller.
102
+ *
103
+ * Pass it to skip a second detection pass; omit it to let this function
104
+ * detect. `null` states that detection ran and matched nothing.
105
+ */
106
+ systemLocale?: string | null;
107
+ }
108
+ declare function resolveLocale({ assets, currentLocale, defaultLocale, autodetect, systemLocale, }: ResolveLocaleConfig): string;
109
+
110
+ /**
111
+ * Builds a BCP-47 tag carrying Unicode extensions, for handing to `Intl`.
112
+ *
113
+ * `locon` has no opinion on which calendar or numbering system your app
114
+ * should use — a Persian journal app wants Jalali dates, a Persian payroll
115
+ * export usually does not. That is a product decision. What the library can
116
+ * do is get the tag syntax right, which naive concatenation does not:
117
+ * appending `-u-ca-gregory` to a locale that already reads `de-u-nu-latn`
118
+ * yields a tag with two `-u-` singletons, and `Intl` throws on it.
119
+ */
120
+ interface IntlExtensions {
121
+ /** Calendar system, e.g. `gregory`, `islamic`, `persian`. */
122
+ calendar?: string;
123
+ /** Numbering system, e.g. `latn`, `arab`, `deva`. */
124
+ numbering?: string;
125
+ }
126
+ /**
127
+ * Returns `locale` with the given Unicode extensions applied, merging into an
128
+ * existing `-u-` section rather than adding a second one. Keys already present
129
+ * in the tag are left alone, so an explicit choice by the caller wins.
130
+ */
131
+ declare function intlLocale(locale: string, extensions?: IntlExtensions): string;
132
+
133
+ /**
134
+ * Right-to-left helpers.
135
+ *
136
+ * This is a writing-direction table, not a platform support table: it answers
137
+ * "is this locale written right-to-left" per CLDR, regardless of whether Apple
138
+ * or Google happen to offer that language as a system UI language.
139
+ *
140
+ * `locon` does not flip your layout on its own — React Native can only change
141
+ * writing direction through `I18nManager`, and that change needs an app
142
+ * restart to take effect. So the library tells you *whether* a locale is RTL
143
+ * and gives you one explicit call to apply it; when to restart stays yours.
144
+ */
145
+ /**
146
+ * Language subtags whose default script is right-to-left, per CLDR.
147
+ *
148
+ * Deliberately excludes languages that merely *have* an RTL historical
149
+ * orthography — Hausa is Latin-script (Boko) in CLDR and therefore LTR, even
150
+ * though Ajami exists.
151
+ */
152
+ declare const RTL_LANGUAGES: Set<string>;
153
+ /**
154
+ * Right-to-left ISO 15924 script subtags.
155
+ *
156
+ * Checked before the language table so that explicitly scripted tags resolve
157
+ * correctly in both directions: `pa-Arab` (Shahmukhi Punjabi) is RTL although
158
+ * plain `pa` is not, and `ku-Latn` is LTR although `ku-Arab` is not.
159
+ */
160
+ declare const RTL_SCRIPTS: Set<string>;
161
+ /** True when the locale is written right-to-left. */
162
+ declare function isRtlLocale(locale?: string | null): boolean;
163
+ /**
164
+ * Aligns React Native's layout direction with `locale`.
165
+ *
166
+ * Returns `true` when the direction actually changed, which means the running
167
+ * UI is now inconsistent and the app must restart for the new direction to
168
+ * render. Callers typically prompt the user, or reload via `expo-updates`.
169
+ *
170
+ * No-ops (returning `false`) outside React Native, so calling it on web or in
171
+ * tests is safe.
172
+ */
173
+ declare function applyRTL(locale: string): boolean;
8
174
 
9
175
  declare const LoconContext: react.Context<LoconContext$1>;
10
- interface Props extends PropsWithChildren {
176
+ interface LoconProps extends PropsWithChildren {
11
177
  assets?: Record<string, Record<string, string>>;
12
- currentLocale?: string;
178
+ /**
179
+ * Locale to display.
180
+ *
181
+ * - a string selects that locale; later prop changes select the new value
182
+ * - `null` explicitly follows the device language
183
+ * - omitted leaves the locale uncontrolled after the initial detection
184
+ *
185
+ * The `null` form exists for apps that persist a user choice: storing
186
+ * "follow the system" as `null` and passing it straight through switches
187
+ * detection back on without an app restart.
188
+ */
189
+ currentLocale?: string | null;
13
190
  defaultLocale?: string;
14
191
  projectLocale?: string;
15
192
  autodetect?: boolean;
16
193
  }
17
- declare function Locon({ children, assets, currentLocale: currentLocaleProps, defaultLocale, projectLocale, autodetect, }: Props): ReactElement;
194
+ declare function Locon({ children, assets, currentLocale: currentLocaleProps, defaultLocale, projectLocale, autodetect, }: LoconProps): ReactElement;
18
195
 
19
- export { LoconContext, Locon as default };
196
+ export { type Assets, type IntlExtensions, LText, type LTextProps, LoconContext, type LoconContext$1 as LoconContextValue, type LoconProps, RTL_LANGUAGES, RTL_SCRIPTS, type ResolveLocaleConfig, type TranslateOptions, type Translator, type TranslatorConfig, applyRTL, createTranslator, Locon as default, getSystemLanguage, intlLocale, isRtlLocale, resolveLocale, useLocon };