@stacksjs/i18n 0.75.586 → 0.75.590
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/client.d.ts +5 -0
- package/dist/client.js +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/stx.d.ts +8 -0
- package/dist/stx.js +1 -0
- package/dist/translator.d.ts +13 -13
- package/dist/translator.js +1 -1
- package/dist/types.d.ts +1 -0
- package/package.json +31 -6
package/dist/client.d.ts
ADDED
package/dist/client.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"./translator";export*from"./formatter";export*from"./pluralization";export*from"./types";
|
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ export * from './formatter';
|
|
|
38
38
|
export * from './pluralization';
|
|
39
39
|
export * from './loader';
|
|
40
40
|
export * from './types';
|
|
41
|
+
export { useI18n } from './stx';
|
|
41
42
|
// Re-export the file-loading and type-generation surface from
|
|
42
43
|
// `@stacksjs/ts-i18n`. The library handles disk loading (YAML / TS /
|
|
43
44
|
// JSON), namespace resolution, and `.d.ts` codegen for translation
|
|
@@ -68,7 +69,6 @@ export {
|
|
|
68
69
|
hasTranslation,
|
|
69
70
|
I18n,
|
|
70
71
|
createI18n,
|
|
71
|
-
useI18n,
|
|
72
72
|
} from './translator';
|
|
73
73
|
export {
|
|
74
74
|
ensureLocalesLoaded,
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"./translator";export*from"./formatter";export*from"./pluralization";export*from"./loader";export*from"./types";export{loadTranslations as loadTranslationsFromDisk,generateTypes as generateI18nTypes,generateTypesFromModule as generateI18nTypesFromModule,generateSampleConfig as generateI18nSampleConfig,writeOutputs as writeI18nOutputs,createTranslator as createSimpleTranslator}from"@stacksjs/ts-i18n";export{t,trans,tc,te,tm,setLocale,getLocale,addTranslations,loadTranslations,getAvailableLocales,hasTranslation,I18n,createI18n
|
|
1
|
+
export*from"./translator";export*from"./formatter";export*from"./pluralization";export*from"./loader";export*from"./types";export{useI18n}from"./stx";export{loadTranslations as loadTranslationsFromDisk,generateTypes as generateI18nTypes,generateTypesFromModule as generateI18nTypesFromModule,generateSampleConfig as generateI18nSampleConfig,writeOutputs as writeI18nOutputs,createTranslator as createSimpleTranslator}from"@stacksjs/ts-i18n";export{t,trans,tc,te,tm,setLocale,getLocale,addTranslations,loadTranslations,getAvailableLocales,hasTranslation,I18n,createI18n}from"./translator";export{ensureLocalesLoaded,resolveRequestLocale,applyRequestLocale}from"./bootstrap";export{createLocaleSwitchResponse,localizePath,stripLocalePrefix}from"./locale-switch";export{formatDate,formatTime,formatDateTime,formatNumber,formatCurrency,formatPercent,formatRelativeTime,formatList,formatPlural}from"./formatter";
|
package/dist/stx.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { I18n } from './translator';
|
|
2
|
+
import type { I18nConfig } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* An isolated native STX translator. Reads inside bindings/effects track locale
|
|
5
|
+
* and message changes, so switching words preserves the mounted screen state.
|
|
6
|
+
* Import STX from its root: the template bundler shares this signal runtime.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useI18n(options?: Partial<I18nConfig>): I18n;
|
package/dist/stx.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{state}from"@stacksjs/stx";import{I18n}from"./translator";export function useI18n(options={}){const revision=state(0);let version=0;const instance=new I18n(options,()=>{revision()});instance.subscribe(()=>{revision.set(++version)});return instance}
|
package/dist/translator.d.ts
CHANGED
|
@@ -47,10 +47,6 @@ export declare function tm(key: string, locale?: string): TranslationMessages |
|
|
|
47
47
|
* Create a new i18n instance
|
|
48
48
|
*/
|
|
49
49
|
export declare function createI18n(options?: Partial<I18nConfig>): I18n;
|
|
50
|
-
/**
|
|
51
|
-
* Use the i18n composable from an STX component.
|
|
52
|
-
*/
|
|
53
|
-
export declare function useI18n(options?: Partial<I18nConfig>): I18nInstance;
|
|
54
50
|
/**
|
|
55
51
|
* Configure the global i18n instance
|
|
56
52
|
*/
|
|
@@ -67,19 +63,23 @@ export declare class I18n implements I18nInstance {
|
|
|
67
63
|
private _fallbackLocale;
|
|
68
64
|
private _messages;
|
|
69
65
|
private _config;
|
|
70
|
-
|
|
66
|
+
private listeners;
|
|
67
|
+
private track;
|
|
68
|
+
constructor(options?: Partial<I18nConfig>, track?: () => void);
|
|
71
69
|
get locale(): string;
|
|
72
70
|
set locale(value: string);
|
|
73
71
|
get fallbackLocale(): string;
|
|
74
72
|
get availableLocales(): string[];
|
|
75
|
-
t:
|
|
76
|
-
tc:
|
|
77
|
-
te:
|
|
78
|
-
tm:
|
|
79
|
-
setLocale:
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
73
|
+
t: I18nInstance['t'];
|
|
74
|
+
tc: I18nInstance['tc'];
|
|
75
|
+
te: I18nInstance['te'];
|
|
76
|
+
tm: I18nInstance['tm'];
|
|
77
|
+
setLocale: I18nInstance['setLocale'];
|
|
78
|
+
subscribe: I18nInstance['subscribe'];
|
|
79
|
+
private notify;
|
|
80
|
+
addTranslations: I18nInstance['addTranslations'];
|
|
81
|
+
d: I18nInstance['d'];
|
|
82
|
+
n: I18nInstance['n'];
|
|
83
83
|
private translate;
|
|
84
84
|
private translatePlural;
|
|
85
85
|
private hasTranslation;
|
package/dist/translator.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parsePluralForms,selectPluralForm}from"./pluralization";const defaultConfig={locale:"en",fallbackLocale:"en",availableLocales:["en"],messages:{},warnMissing:true,escapeValues:false,keySeparator:".",pluralSeparator:"|"};let currentLocale="en";let fallbackLocale="en";const translations={};let config={...defaultConfig};export class I18n{_locale;_fallbackLocale;_messages;_config;constructor(options={}){this._config={...defaultConfig,...options};this._locale=this._config.locale;this._fallbackLocale=this._config.fallbackLocale;this._messages=this._config.messages
|
|
1
|
+
import{parsePluralForms,selectPluralForm}from"./pluralization";const defaultConfig={locale:"en",fallbackLocale:"en",availableLocales:["en"],messages:{},warnMissing:true,escapeValues:false,keySeparator:".",pluralSeparator:"|"};let currentLocale="en";let fallbackLocale="en";const translations={};let config={...defaultConfig};export class I18n{track;_locale;_fallbackLocale;_messages;_config;listeners=new Set;constructor(options={},track=()=>{}){this.track=track;this._config={...defaultConfig,...options};this._locale=this._config.locale;this._fallbackLocale=this._config.fallbackLocale;this._messages={...this._config.messages}}get locale(){this.track();return this._locale}set locale(value){this.setLocale(value)}get fallbackLocale(){return this._fallbackLocale}get availableLocales(){this.track();return Object.keys(this._messages)}t=(key,values,locale)=>{return this.translate(key,values,locale)};tc=(key,count,values,locale)=>{return this.translatePlural(key,count,values,locale)};te=(key,locale)=>{return this.hasTranslation(key,locale)};tm=(key,locale)=>{return this.getMessage(key,locale??this.locale)};setLocale=(locale)=>{if(locale===this._locale)return;this._locale=locale;this.notify()};subscribe=(listener)=>{this.listeners.add(listener);return()=>{this.listeners.delete(listener)}};notify(){for(const listener of this.listeners)listener()}addTranslations=(locale,messages)=>{if(!this._messages[locale]){this._messages[locale]={}}this._messages[locale]=deepMerge(this._messages[locale],messages);this.notify()};d=(value,format)=>{const date=typeof value==="number"?new Date(value):value;const options=this._config.dateTimeFormats?.[this.locale]?.[format||"short"]||{};return new Intl.DateTimeFormat(this.locale,options).format(date)};n=(value,format)=>{const options=this._config.numberFormats?.[this.locale]?.[format||"decimal"]||{};return new Intl.NumberFormat(this.locale,options).format(value)};translate(key,values,locale){const targetLocale=locale||this.locale;let message=this.getMessage(key,targetLocale);if(message===undefined){const dash=targetLocale.indexOf("-");if(dash>0){const langOnly=targetLocale.slice(0,dash);if(langOnly!==this._fallbackLocale){message=this.getMessage(key,langOnly)}}}if(message===undefined&&targetLocale!==this._fallbackLocale){message=this.getMessage(key,this._fallbackLocale)}if(message===undefined){if(this._config.warnMissing){console.warn(`[i18n] Missing translation: "${key}" for locale "${targetLocale}"`)}if(this._config.missingHandler){const result=this._config.missingHandler(targetLocale,key);if(result!==undefined)return result}return key}if(typeof message!=="string"){return key}return this.interpolate(message,values)}translatePlural(key,count,values,locale){const targetLocale=locale||this.locale;let message=this.getMessage(key,targetLocale);if(message===undefined&&targetLocale.includes("-")){message=this.getMessage(key,targetLocale.split("-")[0])}if(message===undefined&&targetLocale!==this._fallbackLocale){message=this.getMessage(key,this._fallbackLocale)}if(message===undefined||typeof message!=="string"){if(this._config.warnMissing){console.warn(`[i18n] Missing plural translation: "${key}" for locale "${targetLocale}"`)}if(message===undefined&&this._config.missingHandler){const result=this._config.missingHandler(targetLocale,key);if(result!==undefined)return result}return key}const forms=parsePluralForms(message,this._config.pluralSeparator);const selectedForm=selectPluralForm(forms,count,targetLocale);const allValues={count,n:count,...values};return this.interpolate(selectedForm,allValues)}hasTranslation(key,locale){const targetLocale=locale||this.locale;return this.getMessage(key,targetLocale)!==undefined}getMessage(key,locale){this.track();const messages=this._messages[locale];if(!messages)return;const parts=key.split(this._config.keySeparator||".");let current=messages;for(const part of parts){if(current===undefined||typeof current==="string"){return}current=current[part]}return current}interpolate(message,values){if(!values)return message;return message.replace(/\{(\w+)\}/g,(match,key)=>{const value=values[key];if(value===undefined||value===null)return match;const str=String(value);return this._config.escapeValues?escapeHtml(str):str})}}export function setLocale(locale){currentLocale=locale}export function getLocale(){return currentLocale}export function setFallbackLocale(locale){fallbackLocale=locale}export function getAvailableLocales(){return Object.keys(translations)}export function addTranslations(locale,messages){if(!translations[locale]){translations[locale]={}}translations[locale]=deepMerge(translations[locale],messages)}export function loadTranslations(messages){for(const[locale,localeMessages]of Object.entries(messages)){addTranslations(locale,localeMessages)}}export function hasTranslation(key,locale){const targetLocale=locale||currentLocale;return getMessage(key,targetLocale)!==undefined}function getMessage(key,locale){const messages=translations[locale];if(!messages)return;const parts=key.split(config.keySeparator||".");let current=messages;for(const part of parts){if(current===undefined||typeof current==="string"){return}current=current[part]}return current}function interpolate(message,values){if(!values)return message;return message.replace(/\{(\w+)\}/g,(match,key)=>{const value=values[key];if(value===undefined||value===null)return match;return String(value)})}export function t(key,values,locale){const targetLocale=locale||currentLocale;let message=getMessage(key,targetLocale);if(message===undefined&&targetLocale!==fallbackLocale){message=getMessage(key,fallbackLocale)}if(message===undefined){if(config.warnMissing){console.warn(`[i18n] Missing translation: "${key}" for locale "${targetLocale}"`)}return key}if(typeof message!=="string"){return key}return interpolate(message,values)}export const trans=t;export function tc(key,count,values,locale){const targetLocale=locale||currentLocale;let message=getMessage(key,targetLocale);if(message===undefined&&targetLocale!==fallbackLocale){message=getMessage(key,fallbackLocale)}if(message===undefined||typeof message!=="string"){return key}const forms=parsePluralForms(message,config.pluralSeparator);const selectedForm=selectPluralForm(forms,count,targetLocale);const allValues={count,n:count,...values};return interpolate(selectedForm,allValues)}export function te(key,locale){return hasTranslation(key,locale)}export function tm(key,locale){return getMessage(key,locale||currentLocale)}export function createI18n(options={}){return new I18n(options)}export function configure(options){config={...config,...options};if(options.locale)currentLocale=options.locale;if(options.fallbackLocale)fallbackLocale=options.fallbackLocale;if(options.messages)loadTranslations(options.messages)}function deepMerge(target,source){const result={...target};for(const key of Object.keys(source)){const sourceValue=source[key];const targetValue=result[key];if(typeof sourceValue==="object"&&sourceValue!==null&&typeof targetValue==="object"&&targetValue!==null){result[key]=deepMerge(targetValue,sourceValue)}else{if(sourceValue!==undefined)result[key]=sourceValue}}return result}function escapeHtml(str){const htmlEscapes={"&":"&","<":"<",">":">",'"':""","'":"'"};return str.replace(/[&<>"']/g,(char)=>htmlEscapes[char]||char)}
|
package/dist/types.d.ts
CHANGED
|
@@ -144,6 +144,7 @@ export declare interface I18nInstance {
|
|
|
144
144
|
te: (key: string, locale?: string) => boolean
|
|
145
145
|
tm: (key: string, locale?: string) => TranslationMessages | string | undefined
|
|
146
146
|
setLocale: (locale: string) => void
|
|
147
|
+
subscribe: (listener: () => void) => () => void
|
|
147
148
|
addTranslations: (locale: string, messages: TranslationMessages) => void
|
|
148
149
|
d: (value: Date | number, format?: string) => string
|
|
149
150
|
n: (value: number, format?: string) => string
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/i18n",
|
|
3
|
-
"version": "0.75.
|
|
3
|
+
"version": "0.75.590",
|
|
4
4
|
"description": "Internationalization system for Stacks.js applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -20,6 +20,18 @@
|
|
|
20
20
|
"bun": "./dist/index.js",
|
|
21
21
|
"import": "./dist/index.js",
|
|
22
22
|
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./client": {
|
|
25
|
+
"types": "./dist/client.d.ts",
|
|
26
|
+
"bun": "./dist/client.js",
|
|
27
|
+
"import": "./dist/client.js",
|
|
28
|
+
"default": "./dist/client.js"
|
|
29
|
+
},
|
|
30
|
+
"./stx": {
|
|
31
|
+
"types": "./dist/stx.d.ts",
|
|
32
|
+
"bun": "./dist/stx.js",
|
|
33
|
+
"import": "./dist/stx.js",
|
|
34
|
+
"default": "./dist/stx.js"
|
|
23
35
|
}
|
|
24
36
|
},
|
|
25
37
|
"main": "./dist/index.js",
|
|
@@ -36,13 +48,14 @@
|
|
|
36
48
|
"prepublishOnly": "bun run build"
|
|
37
49
|
},
|
|
38
50
|
"dependencies": {
|
|
39
|
-
"@stacksjs/config": "0.75.
|
|
40
|
-
"@stacksjs/logging": "0.75.
|
|
41
|
-
"@stacksjs/path": "0.75.
|
|
42
|
-
"@stacksjs/ts-i18n": "^0.1.11"
|
|
51
|
+
"@stacksjs/config": "0.75.590",
|
|
52
|
+
"@stacksjs/logging": "0.75.590",
|
|
53
|
+
"@stacksjs/path": "0.75.590",
|
|
54
|
+
"@stacksjs/ts-i18n": "^0.1.11",
|
|
55
|
+
"@stacksjs/stx": "^0.2.872"
|
|
43
56
|
},
|
|
44
57
|
"devDependencies": {
|
|
45
|
-
"@stacksjs/development": "0.75.
|
|
58
|
+
"@stacksjs/development": "0.75.590"
|
|
46
59
|
},
|
|
47
60
|
"publishConfig": {
|
|
48
61
|
"exports": {
|
|
@@ -51,6 +64,18 @@
|
|
|
51
64
|
"bun": "./dist/index.js",
|
|
52
65
|
"import": "./dist/index.js",
|
|
53
66
|
"default": "./dist/index.js"
|
|
67
|
+
},
|
|
68
|
+
"./client": {
|
|
69
|
+
"types": "./dist/client.d.ts",
|
|
70
|
+
"bun": "./dist/client.js",
|
|
71
|
+
"import": "./dist/client.js",
|
|
72
|
+
"default": "./dist/client.js"
|
|
73
|
+
},
|
|
74
|
+
"./stx": {
|
|
75
|
+
"types": "./dist/stx.d.ts",
|
|
76
|
+
"bun": "./dist/stx.js",
|
|
77
|
+
"import": "./dist/stx.js",
|
|
78
|
+
"default": "./dist/stx.js"
|
|
54
79
|
}
|
|
55
80
|
}
|
|
56
81
|
}
|