inline-i18n-multi 0.11.0 → 0.12.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 CHANGED
@@ -82,6 +82,7 @@ See "Hello" in your app? Just search for "Hello" in your codebase. **Done.**
82
82
  - **Locale Display Names** - Get human-readable locale names using `Intl.DisplayNames` (`getLocaleDisplayName('ko', 'en')` → `"Korean"`)
83
83
  - **Translation Key Listing** - `getTranslationKeys(locale?, namespace?)` returns all loaded translation keys
84
84
  - **Missing Translation Tracker** - Runtime collection of missing translation keys (`trackMissingKeys(true/false)`, `getMissingKeys()`, `clearMissingKeys()`)
85
+ - **Locale Change Event** — Subscribe to locale changes with `onLocaleChange()` (v0.12.0)
85
86
 
86
87
  ---
87
88
 
@@ -898,6 +899,23 @@ Useful for discovering untranslated content during development and testing.
898
899
 
899
900
  ---
900
901
 
902
+ ## Locale Change Event
903
+
904
+ Subscribe to locale changes with `onLocaleChange()` to run custom logic when the locale switches:
905
+
906
+ ```ts
907
+ import { setLocale, onLocaleChange } from 'inline-i18n-multi'
908
+
909
+ const unsubscribe = onLocaleChange((newLocale, previousLocale) => {
910
+ console.log(`Locale changed: ${previousLocale} → ${newLocale}`)
911
+ })
912
+
913
+ setLocale('ko') // logs: "Locale changed: en → ko"
914
+ unsubscribe() // stop listening
915
+ ```
916
+
917
+ ---
918
+
901
919
  ## Configuration
902
920
 
903
921
  Configure global settings for fallback behavior and warnings:
@@ -997,6 +1015,8 @@ Available helpers:
997
1015
  | `trackMissingKeys(enabled)` | Enable or disable missing translation key tracking |
998
1016
  | `getMissingKeys()` | Get all tracked missing translation keys |
999
1017
  | `clearMissingKeys()` | Clear the tracked missing keys list |
1018
+ | `onLocaleChange(callback)` | Subscribe to locale changes, returns unsubscribe function |
1019
+ | `clearLocaleListeners()` | Remove all locale change listeners |
1000
1020
 
1001
1021
  ### Custom Formatters
1002
1022
 
package/dist/index.d.mts CHANGED
@@ -97,6 +97,14 @@ declare function it(ko: string, en: string, vars?: TranslationVars): string;
97
97
  */
98
98
  declare function it(translations: Translations, vars?: TranslationVars): string;
99
99
 
100
+ type LocaleChangeCallback = (locale: Locale, previousLocale: Locale) => void;
101
+ /**
102
+ * Subscribe to locale changes.
103
+ * Returns an unsubscribe function.
104
+ */
105
+ declare function onLocaleChange(callback: LocaleChangeCallback): () => void;
106
+ /** Remove all locale change listeners (for testing) */
107
+ declare function clearLocaleListeners(): void;
100
108
  declare function setLocale(locale: Locale): void;
101
109
  declare function getLocale(): Locale;
102
110
  /**
@@ -387,4 +395,4 @@ interface RichTextSegment {
387
395
  */
388
396
  declare function parseRichText(template: string, componentNames: string[]): RichTextSegment[];
389
397
 
390
- export { type Config, type CustomFormatter, type DebugModeOptions, type DetectLocaleOptions, type DetectSource, type Dictionaries, type Dictionary, type Locale, type PluralRules, type RichTextSegment, type TranslationVars, type TranslationWarning, type Translations, type WarningHandler, __i18n_lookup, clearDictionaries, clearFormatters, clearICUCache, clearMissingKeys, configure, createScope, detectLocale, en_de, en_es, en_fr, en_ja, en_zh, getConfig, getDictionary, getLoadedLocales, getLoadedNamespaces, getLocale, getLocaleDisplayName, getMissingKeys, getTranslationKeys, hasTranslation, isLoaded, it, it_de, it_es, it_fr, it_ja, it_zh, ja_es, ja_zh, loadAsync, loadDictionaries, loadDictionary, parseRichText, registerFormatter, resetConfig, restoreLocale, setLocale, t, trackMissingKeys, zh_es };
398
+ export { type Config, type CustomFormatter, type DebugModeOptions, type DetectLocaleOptions, type DetectSource, type Dictionaries, type Dictionary, type Locale, type PluralRules, type RichTextSegment, type TranslationVars, type TranslationWarning, type Translations, type WarningHandler, __i18n_lookup, clearDictionaries, clearFormatters, clearICUCache, clearLocaleListeners, clearMissingKeys, configure, createScope, detectLocale, en_de, en_es, en_fr, en_ja, en_zh, getConfig, getDictionary, getLoadedLocales, getLoadedNamespaces, getLocale, getLocaleDisplayName, getMissingKeys, getTranslationKeys, hasTranslation, isLoaded, it, it_de, it_es, it_fr, it_ja, it_zh, ja_es, ja_zh, loadAsync, loadDictionaries, loadDictionary, onLocaleChange, parseRichText, registerFormatter, resetConfig, restoreLocale, setLocale, t, trackMissingKeys, zh_es };
package/dist/index.d.ts CHANGED
@@ -97,6 +97,14 @@ declare function it(ko: string, en: string, vars?: TranslationVars): string;
97
97
  */
98
98
  declare function it(translations: Translations, vars?: TranslationVars): string;
99
99
 
100
+ type LocaleChangeCallback = (locale: Locale, previousLocale: Locale) => void;
101
+ /**
102
+ * Subscribe to locale changes.
103
+ * Returns an unsubscribe function.
104
+ */
105
+ declare function onLocaleChange(callback: LocaleChangeCallback): () => void;
106
+ /** Remove all locale change listeners (for testing) */
107
+ declare function clearLocaleListeners(): void;
100
108
  declare function setLocale(locale: Locale): void;
101
109
  declare function getLocale(): Locale;
102
110
  /**
@@ -387,4 +395,4 @@ interface RichTextSegment {
387
395
  */
388
396
  declare function parseRichText(template: string, componentNames: string[]): RichTextSegment[];
389
397
 
390
- export { type Config, type CustomFormatter, type DebugModeOptions, type DetectLocaleOptions, type DetectSource, type Dictionaries, type Dictionary, type Locale, type PluralRules, type RichTextSegment, type TranslationVars, type TranslationWarning, type Translations, type WarningHandler, __i18n_lookup, clearDictionaries, clearFormatters, clearICUCache, clearMissingKeys, configure, createScope, detectLocale, en_de, en_es, en_fr, en_ja, en_zh, getConfig, getDictionary, getLoadedLocales, getLoadedNamespaces, getLocale, getLocaleDisplayName, getMissingKeys, getTranslationKeys, hasTranslation, isLoaded, it, it_de, it_es, it_fr, it_ja, it_zh, ja_es, ja_zh, loadAsync, loadDictionaries, loadDictionary, parseRichText, registerFormatter, resetConfig, restoreLocale, setLocale, t, trackMissingKeys, zh_es };
398
+ export { type Config, type CustomFormatter, type DebugModeOptions, type DetectLocaleOptions, type DetectSource, type Dictionaries, type Dictionary, type Locale, type PluralRules, type RichTextSegment, type TranslationVars, type TranslationWarning, type Translations, type WarningHandler, __i18n_lookup, clearDictionaries, clearFormatters, clearICUCache, clearLocaleListeners, clearMissingKeys, configure, createScope, detectLocale, en_de, en_es, en_fr, en_ja, en_zh, getConfig, getDictionary, getLoadedLocales, getLoadedNamespaces, getLocale, getLocaleDisplayName, getMissingKeys, getTranslationKeys, hasTranslation, isLoaded, it, it_de, it_es, it_fr, it_ja, it_zh, ja_es, ja_zh, loadAsync, loadDictionaries, loadDictionary, onLocaleChange, parseRichText, registerFormatter, resetConfig, restoreLocale, setLocale, t, trackMissingKeys, zh_es };
package/dist/index.js CHANGED
@@ -105,6 +105,21 @@ function applyDebugFormat(output, debugInfo) {
105
105
 
106
106
  // src/context.ts
107
107
  var currentLocale = "en";
108
+ var listeners = /* @__PURE__ */ new Set();
109
+ function onLocaleChange(callback) {
110
+ listeners.add(callback);
111
+ return () => {
112
+ listeners.delete(callback);
113
+ };
114
+ }
115
+ function notifyListeners(locale, previousLocale) {
116
+ for (const cb of listeners) {
117
+ cb(locale, previousLocale);
118
+ }
119
+ }
120
+ function clearLocaleListeners() {
121
+ listeners.clear();
122
+ }
108
123
  function setCookie(name, value, days) {
109
124
  if (typeof document === "undefined") return;
110
125
  const expires = new Date(Date.now() + days * 864e5).toUTCString();
@@ -131,8 +146,12 @@ function persistLocaleToStorage(locale) {
131
146
  }
132
147
  }
133
148
  function setLocale(locale) {
149
+ const previousLocale = currentLocale;
134
150
  currentLocale = locale;
135
151
  persistLocaleToStorage(locale);
152
+ if (locale !== previousLocale) {
153
+ notifyListeners(locale, previousLocale);
154
+ }
136
155
  }
137
156
  function getLocale() {
138
157
  return currentLocale;
@@ -1178,6 +1197,7 @@ exports.__i18n_lookup = __i18n_lookup;
1178
1197
  exports.clearDictionaries = clearDictionaries;
1179
1198
  exports.clearFormatters = clearFormatters;
1180
1199
  exports.clearICUCache = clearICUCache;
1200
+ exports.clearLocaleListeners = clearLocaleListeners;
1181
1201
  exports.clearMissingKeys = clearMissingKeys;
1182
1202
  exports.configure = configure;
1183
1203
  exports.createScope = createScope;
@@ -1208,6 +1228,7 @@ exports.ja_zh = ja_zh;
1208
1228
  exports.loadAsync = loadAsync;
1209
1229
  exports.loadDictionaries = loadDictionaries;
1210
1230
  exports.loadDictionary = loadDictionary;
1231
+ exports.onLocaleChange = onLocaleChange;
1211
1232
  exports.parseRichText = parseRichText;
1212
1233
  exports.registerFormatter = registerFormatter;
1213
1234
  exports.resetConfig = resetConfig;