intor 2.2.8 → 2.2.9

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.
@@ -645,6 +645,23 @@ function useTranslator2(preKey) {
645
645
  };
646
646
  }
647
647
 
648
+ // src/client/shared/utils/get-initial-locale.ts
649
+ function getInitialLocale(config, cookieName = config.cookie.name) {
650
+ const { defaultLocale, supportedLocales } = config;
651
+ const match = document.cookie.match(
652
+ new RegExp(
653
+ `(?:^|; )${cookieName.replaceAll(/([.$?*|{}()[\]\\/+^])/g, String.raw`\$1`)}=([^;]*)`
654
+ )
655
+ );
656
+ const cookieLocale = match ? decodeURIComponent(match[1]) : null;
657
+ const browserLocale = navigator.languages?.[0] || navigator.language;
658
+ const locale = cookieLocale || browserLocale || defaultLocale;
659
+ const normalized = locale.toLowerCase();
660
+ const matched = supportedLocales.find((l) => l.toLowerCase() === normalized);
661
+ return matched ?? defaultLocale;
662
+ }
663
+
648
664
  exports.IntorProvider = IntorProvider;
649
665
  exports.TranslateHandlersProvider = TranslateHandlersProvider;
666
+ exports.getInitialLocale = getInitialLocale;
650
667
  exports.useTranslator = useTranslator2;
@@ -208,4 +208,15 @@ type TranslatorInstance<M, PK extends string | undefined = undefined> = {
208
208
  declare function useTranslator<CK extends GenConfigKeys = "__default__">(): TranslatorInstance<GenMessages<CK>>;
209
209
  declare function useTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(preKey: IfGen<PK, string>): TranslatorInstance<GenMessages<CK>, PK>;
210
210
 
211
- export { IntorProvider, type IntorProviderProps, TranslateHandlersProvider, type TranslateHandlersProviderProps, useTranslator };
211
+ /**
212
+ * Get the initial locale from cookie, browser, or default.
213
+ *
214
+ * - This function is intended for client-side use only.
215
+ *
216
+ * @param {IntorResolvedConfig} config - The intor configuration.
217
+ * @param {string} [cookieName=config.cookie.name] - Optional cookie name to check.
218
+ * @returns {string} The matched locale or the default locale.
219
+ */
220
+ declare function getInitialLocale(config: IntorResolvedConfig, cookieName?: string): string;
221
+
222
+ export { IntorProvider, type IntorProviderProps, TranslateHandlersProvider, type TranslateHandlersProviderProps, getInitialLocale, useTranslator };
@@ -208,4 +208,15 @@ type TranslatorInstance<M, PK extends string | undefined = undefined> = {
208
208
  declare function useTranslator<CK extends GenConfigKeys = "__default__">(): TranslatorInstance<GenMessages<CK>>;
209
209
  declare function useTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(preKey: IfGen<PK, string>): TranslatorInstance<GenMessages<CK>, PK>;
210
210
 
211
- export { IntorProvider, type IntorProviderProps, TranslateHandlersProvider, type TranslateHandlersProviderProps, useTranslator };
211
+ /**
212
+ * Get the initial locale from cookie, browser, or default.
213
+ *
214
+ * - This function is intended for client-side use only.
215
+ *
216
+ * @param {IntorResolvedConfig} config - The intor configuration.
217
+ * @param {string} [cookieName=config.cookie.name] - Optional cookie name to check.
218
+ * @returns {string} The matched locale or the default locale.
219
+ */
220
+ declare function getInitialLocale(config: IntorResolvedConfig, cookieName?: string): string;
221
+
222
+ export { IntorProvider, type IntorProviderProps, TranslateHandlersProvider, type TranslateHandlersProviderProps, getInitialLocale, useTranslator };
@@ -619,4 +619,20 @@ function useTranslator2(preKey) {
619
619
  };
620
620
  }
621
621
 
622
- export { IntorProvider, TranslateHandlersProvider, useTranslator2 as useTranslator };
622
+ // src/client/shared/utils/get-initial-locale.ts
623
+ function getInitialLocale(config, cookieName = config.cookie.name) {
624
+ const { defaultLocale, supportedLocales } = config;
625
+ const match = document.cookie.match(
626
+ new RegExp(
627
+ `(?:^|; )${cookieName.replaceAll(/([.$?*|{}()[\]\\/+^])/g, String.raw`\$1`)}=([^;]*)`
628
+ )
629
+ );
630
+ const cookieLocale = match ? decodeURIComponent(match[1]) : null;
631
+ const browserLocale = navigator.languages?.[0] || navigator.language;
632
+ const locale = cookieLocale || browserLocale || defaultLocale;
633
+ const normalized = locale.toLowerCase();
634
+ const matched = supportedLocales.find((l) => l.toLowerCase() === normalized);
635
+ return matched ?? defaultLocale;
636
+ }
637
+
638
+ export { IntorProvider, TranslateHandlersProvider, getInitialLocale, useTranslator2 as useTranslator };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intor",
3
- "version": "2.2.8",
3
+ "version": "2.2.9",
4
4
  "description": "A modular and extensible i18n core designed for TypeScript and JavaScript projects. Intor enables custom translation logic with support for both frontend and backend environments, featuring runtime configuration, caching, adapters, and message loaders.",
5
5
  "author": "Yiming Liao",
6
6
  "license": "MIT",