Fix StartOS 0.4 TypeScript packaging to match SDK API
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Internationalization (i18n) utilities for StartOS packages.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // In package's i18n/index.ts:
|
||||
* import { setupI18n } from '@start9labs/start-sdk'
|
||||
* import defaultDict, { DEFAULT_LANG } from './dictionaries/default'
|
||||
* import translations from './dictionaries/translations'
|
||||
*
|
||||
* export const i18n = setupI18n(defaultDict, translations, DEFAULT_LANG)
|
||||
* ```
|
||||
*/
|
||||
type ParamValue = string | number | Date;
|
||||
/**
|
||||
* Creates a typed i18n function for a package.
|
||||
*
|
||||
* @param defaultDict - The default language dictionary mapping strings to numeric indices
|
||||
* @param translations - Translation dictionaries for each supported locale
|
||||
* @param defaultLang - The default language code (e.g., 'en_US')
|
||||
* @returns A typed i18n function that accepts dictionary keys and optional parameters
|
||||
*/
|
||||
export declare function setupI18n<Dict extends Record<string, number>, Translations extends Record<string, Record<number, string>>>(defaultDict: Dict, translations: Translations, defaultLang: string): (key: keyof Dict, params?: Record<string, ParamValue>) => string;
|
||||
export {};
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
"use strict";
|
||||
/**
|
||||
* Internationalization (i18n) utilities for StartOS packages.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // In package's i18n/index.ts:
|
||||
* import { setupI18n } from '@start9labs/start-sdk'
|
||||
* import defaultDict, { DEFAULT_LANG } from './dictionaries/default'
|
||||
* import translations from './dictionaries/translations'
|
||||
*
|
||||
* export const i18n = setupI18n(defaultDict, translations, DEFAULT_LANG)
|
||||
* ```
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.setupI18n = setupI18n;
|
||||
/**
|
||||
* Creates a typed i18n function for a package.
|
||||
*
|
||||
* @param defaultDict - The default language dictionary mapping strings to numeric indices
|
||||
* @param translations - Translation dictionaries for each supported locale
|
||||
* @param defaultLang - The default language code (e.g., 'en_US')
|
||||
* @returns A typed i18n function that accepts dictionary keys and optional parameters
|
||||
*/
|
||||
function setupI18n(defaultDict, translations, defaultLang) {
|
||||
const lang = process.env.LANG?.replace(/\.UTF-8$/, '') || defaultLang;
|
||||
// Convert locale format from en_US to en-US for Intl APIs
|
||||
const intlLocale = lang.replace('_', '-');
|
||||
function getTranslation() {
|
||||
if (lang === defaultLang)
|
||||
return null;
|
||||
const availableLangs = Object.keys(translations);
|
||||
const match = availableLangs.find((l) => l === lang) ??
|
||||
availableLangs.find((l) => String(l).startsWith(lang.split('_')[0] + '_'));
|
||||
return match ? translations[match] : null;
|
||||
}
|
||||
const translation = getTranslation();
|
||||
function formatValue(value) {
|
||||
if (typeof value === 'number') {
|
||||
return new Intl.NumberFormat(intlLocale).format(value);
|
||||
}
|
||||
if (value instanceof Date) {
|
||||
return new Intl.DateTimeFormat(intlLocale).format(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
return function i18n(key, params) {
|
||||
let result = translation
|
||||
? translation[defaultDict[key]]
|
||||
: key;
|
||||
if (params) {
|
||||
for (const [paramName, value] of Object.entries(params)) {
|
||||
result = result.replace(`\${${paramName}}`, formatValue(value));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../package/lib/i18n/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;AAYH,8BAiDC;AAzDD;;;;;;;GAOG;AACH,SAAgB,SAAS,CAGvB,WAAiB,EAAE,YAA0B,EAAE,WAAmB;IAClE,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,WAAW,CAAA;IAErE,0DAA0D;IAC1D,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IAEz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