@adonisjs/i18n
Advanced tools
Comparing version
@@ -5,4 +5,2 @@ export { I18n } from './src/i18n.js'; | ||
export { I18nManager } from './src/i18n_manager.js'; | ||
export { defineConfig } from './src/define_config.js'; | ||
export { default as loadersList } from './src/loaders/main.js'; | ||
export { default as formattersList } from './src/formatters/main.js'; | ||
export { defineConfig, formatters, loaders } from './src/define_config.js'; |
@@ -13,4 +13,2 @@ /* | ||
export { I18nManager } from './src/i18n_manager.js'; | ||
export { defineConfig } from './src/define_config.js'; | ||
export { default as loadersList } from './src/loaders/main.js'; | ||
export { default as formattersList } from './src/formatters/main.js'; | ||
export { defineConfig, formatters, loaders } from './src/define_config.js'; |
@@ -1,4 +0,12 @@ | ||
import type { Edge } from 'edge.js'; | ||
import { I18nManager } from '../src/i18n_manager.js'; | ||
import type { ApplicationService } from '@adonisjs/core/types'; | ||
import '../src/types/extended.js'; | ||
import type { MissingTranslationEventPayload } from '../src/types.js'; | ||
declare module '@adonisjs/core/types' { | ||
interface EventsList { | ||
'i18n:missing:translation': MissingTranslationEventPayload; | ||
} | ||
interface ContainerBindings { | ||
i18n: I18nManager; | ||
} | ||
} | ||
/** | ||
@@ -12,6 +20,10 @@ * Registers a singleton instance of I18nManager to the container, | ||
/** | ||
* Returns edge when it's installed | ||
* Registers edge plugin when edge is installed | ||
*/ | ||
protected getEdge(): Promise<Edge | null>; | ||
protected registerEdgePlugin(i18nManager: I18nManager): Promise<void>; | ||
/** | ||
* Registers repl bindings | ||
*/ | ||
protected registerReplBindings(): Promise<void>; | ||
/** | ||
* Register i18n manager to the container | ||
@@ -18,0 +30,0 @@ */ |
@@ -9,3 +9,5 @@ /* | ||
*/ | ||
import '../src/types/extended.js'; | ||
import { configProvider } from '@adonisjs/core'; | ||
import { RuntimeException } from '@poppinss/utils'; | ||
import { I18nManager } from '../src/i18n_manager.js'; | ||
/** | ||
@@ -21,14 +23,26 @@ * Registers a singleton instance of I18nManager to the container, | ||
/** | ||
* Returns edge when it's installed | ||
* Registers edge plugin when edge is installed | ||
*/ | ||
async getEdge() { | ||
async registerEdgePlugin(i18nManager) { | ||
let edge = null; | ||
try { | ||
const { default: edge } = await import('edge.js'); | ||
return edge; | ||
const edgeExports = await import('edge.js'); | ||
edge = edgeExports.default; | ||
} | ||
catch { | ||
return null; | ||
catch { } | ||
if (edge) { | ||
const { edgePluginI18n } = await import('../src/plugins/edge.js'); | ||
edge.use(edgePluginI18n(i18nManager)); | ||
} | ||
} | ||
/** | ||
* Registers repl bindings | ||
*/ | ||
async registerReplBindings() { | ||
if (this.app.getEnvironment() === 'repl') { | ||
const { registerReplBindings } = await import('../src/repl_bindings.js'); | ||
registerReplBindings(this.app, await this.app.container.make('repl')); | ||
} | ||
} | ||
/** | ||
* Register i18n manager to the container | ||
@@ -38,5 +52,8 @@ */ | ||
this.app.container.singleton('i18n', async (resolver) => { | ||
const { I18nManager } = await import('../src/i18n_manager.js'); | ||
const i18nConfigProvider = this.app.config.get('i18n'); | ||
const config = await configProvider.resolve(this.app, i18nConfigProvider); | ||
if (!config) { | ||
throw new RuntimeException('Invalid default export from "config/i18n.ts" file. Make sure to use defineConfig method'); | ||
} | ||
const emitter = await resolver.make('emitter'); | ||
const config = this.app.config.get('i18n', {}); | ||
return new I18nManager(emitter, config); | ||
@@ -57,18 +74,5 @@ }); | ||
await i18nManager.loadTranslations(); | ||
/** | ||
* Registering edge plugin | ||
*/ | ||
const edge = await this.getEdge(); | ||
if (edge) { | ||
const { edgePluginI18n } = await import('../src/edge_plugin_i18n.js'); | ||
edge.use(edgePluginI18n(i18nManager)); | ||
} | ||
/** | ||
* Register REPL bindings in the REPL environment | ||
*/ | ||
if (this.app.getEnvironment() === 'repl') { | ||
const { registerReplBindings } = await import('../src/repl_bindings.js'); | ||
registerReplBindings(this.app, await this.app.container.make('repl')); | ||
} | ||
await this.registerEdgePlugin(i18nManager); | ||
await this.registerReplBindings(); | ||
} | ||
} |
@@ -1,3 +0,3 @@ | ||
/// <reference types="@types/node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
declare const _default: import("util").DebugLogger; | ||
export default _default; |
@@ -1,5 +0,21 @@ | ||
import type { I18nConfig, I18nServiceConfig } from './types/main.js'; | ||
import type { ConfigProvider } from '@adonisjs/core/types'; | ||
import type { LoaderFactory, BaseI18nConfig, FormatterFactory, I18nManagerConfig, FsLoaderOptions } from './types.js'; | ||
/** | ||
* Define i18n config | ||
* Config helper to define i18n config | ||
*/ | ||
export declare function defineConfig(config: Partial<I18nServiceConfig>): I18nConfig; | ||
export declare function defineConfig(config: Partial<BaseI18nConfig> & { | ||
formatter: FormatterFactory | ConfigProvider<FormatterFactory>; | ||
loaders: (ConfigProvider<LoaderFactory> | LoaderFactory)[]; | ||
}): ConfigProvider<I18nManagerConfig>; | ||
/** | ||
* Config helper to configure a formatter for i18n | ||
*/ | ||
export declare const formatters: { | ||
icu: () => ConfigProvider<FormatterFactory>; | ||
}; | ||
/** | ||
* Config helper to configure loaders for i18n | ||
*/ | ||
export declare const loaders: { | ||
fs: (config: FsLoaderOptions) => ConfigProvider<LoaderFactory>; | ||
}; |
@@ -9,7 +9,6 @@ /* | ||
*/ | ||
import { configProvider } from '@adonisjs/core'; | ||
import { RuntimeException } from '@poppinss/utils'; | ||
import loadersList from './loaders/main.js'; | ||
import formattersList from './formatters/main.js'; | ||
/** | ||
* Define i18n config | ||
* Config helper to define i18n config | ||
*/ | ||
@@ -20,10 +19,44 @@ export function defineConfig(config) { | ||
} | ||
return { | ||
defaultLocale: 'en', | ||
...config, | ||
formatter: (i18Config) => formattersList.create(config.formatter, i18Config), | ||
loaders: (config.loaders || []).map((loaderConfig) => { | ||
return (i18nConfig) => loadersList.create(loaderConfig.driver, loaderConfig, i18nConfig); | ||
}), | ||
}; | ||
const { formatter, loaders, ...rest } = config; | ||
return configProvider.create(async (app) => { | ||
/** | ||
* Resolving formatter | ||
*/ | ||
const resolvedFormatter = typeof formatter === 'function' ? formatter : await formatter.resolver(app); | ||
/** | ||
* Resolving loaders. Each loader can be a factory or a | ||
* config provider. | ||
*/ | ||
const resolvedLoaders = await Promise.all(loaders.map((loader) => { | ||
return typeof loader === 'function' ? loader : loader.resolver(app); | ||
})); | ||
return { | ||
defaultLocale: 'en', | ||
formatter: resolvedFormatter, | ||
loaders: resolvedLoaders, | ||
...rest, | ||
}; | ||
}); | ||
} | ||
/** | ||
* Config helper to configure a formatter for i18n | ||
*/ | ||
export const formatters = { | ||
icu() { | ||
return configProvider.create(async () => { | ||
const { IcuFormatter } = await import('../src/messages_formatters/icu.js'); | ||
return () => new IcuFormatter(); | ||
}); | ||
}, | ||
}; | ||
/** | ||
* Config helper to configure loaders for i18n | ||
*/ | ||
export const loaders = { | ||
fs(config) { | ||
return configProvider.create(async () => { | ||
const { FsLoader } = await import('../src/loaders/fs.js'); | ||
return () => new FsLoader(config); | ||
}); | ||
}, | ||
}; |
import { DateTime } from 'luxon'; | ||
import type { TimeFormatOptions, NumberFormatOptions, CurrencyFormatOptions } from '../types/main.js'; | ||
import type { TimeFormatOptions, NumberFormatOptions, CurrencyFormatOptions } from '../types.js'; | ||
/** | ||
@@ -4,0 +4,0 @@ * Core formatter to format different values using the I18n API |
import type { Emitter } from '@adonisjs/core/events'; | ||
import type { I18nConfig, TranslationsFormatterContract, MissingTranslationEventPayload } from './types/main.js'; | ||
import type { I18nManagerConfig, TranslationsFormatterContract, MissingTranslationEventPayload } from './types.js'; | ||
import { I18n } from './i18n.js'; | ||
@@ -17,3 +17,3 @@ export declare class I18nManager { | ||
'i18n:missing:translation': MissingTranslationEventPayload; | ||
} & any>, config: I18nConfig); | ||
} & any>, config: I18nManagerConfig); | ||
/** | ||
@@ -20,0 +20,0 @@ * Returns an array of locales supported by the application. |
import type { Emitter } from '@adonisjs/core/events'; | ||
import type { I18nManager } from './i18n_manager.js'; | ||
import { Formatter } from './formatters/values_formatter.js'; | ||
import { I18nMessagesProvider } from './i18n_messages_provider.js'; | ||
import type { MissingTranslationEventPayload } from './types/main.js'; | ||
import type { MissingTranslationEventPayload } from './types.js'; | ||
import { I18nMessagesProvider } from './vine_i18n_messages_provider.js'; | ||
/** | ||
@@ -7,0 +7,0 @@ * I18n exposes the APIs to format values and translate messages |
@@ -11,3 +11,3 @@ /* | ||
import { Formatter } from './formatters/values_formatter.js'; | ||
import { I18nMessagesProvider } from './i18n_messages_provider.js'; | ||
import { I18nMessagesProvider } from './vine_i18n_messages_provider.js'; | ||
/** | ||
@@ -14,0 +14,0 @@ * I18n exposes the APIs to format values and translate messages |
{ | ||
"name": "@adonisjs/i18n", | ||
"version": "2.0.0-4", | ||
"version": "2.0.0-5", | ||
"description": "Internationalization for AdonisJS apps", | ||
@@ -21,3 +21,3 @@ "main": "build/index.js", | ||
"./i18n_provider": "./build/providers/i18n_provider.js", | ||
"./plugin_edge": "./build/src/edge_plugin_i18n.js", | ||
"./plugins/edge": "./build/src/plugins/edge.js", | ||
"./types": "./build/src/types/main.js" | ||
@@ -45,16 +45,16 @@ }, | ||
"devDependencies": { | ||
"@adonisjs/assembler": "^6.1.3-22", | ||
"@adonisjs/core": "^6.1.5-26", | ||
"@adonisjs/assembler": "^6.1.3-25", | ||
"@adonisjs/core": "^6.1.5-29", | ||
"@adonisjs/eslint-config": "^1.1.8", | ||
"@adonisjs/prettier-config": "^1.1.8", | ||
"@adonisjs/tsconfig": "^1.1.8", | ||
"@commitlint/cli": "^17.7.1", | ||
"@commitlint/config-conventional": "^17.7.0", | ||
"@japa/assert": "^2.0.0-1", | ||
"@japa/file-system": "^2.0.0-1", | ||
"@japa/runner": "^3.0.0-6", | ||
"@commitlint/cli": "^17.8.0", | ||
"@commitlint/config-conventional": "^17.8.0", | ||
"@japa/assert": "^2.0.0", | ||
"@japa/file-system": "^2.0.0", | ||
"@japa/runner": "^3.0.4", | ||
"@swc/core": "1.3.82", | ||
"@types/luxon": "^3.3.2", | ||
"@types/negotiator": "^0.6.1", | ||
"@types/node": "^20.5.9", | ||
"@types/luxon": "^3.3.3", | ||
"@types/negotiator": "^0.6.2", | ||
"@types/node": "^20.8.7", | ||
"@vinejs/vine": "^1.6.0", | ||
@@ -65,3 +65,3 @@ "c8": "^8.0.1", | ||
"edge.js": "^6.0.0-10", | ||
"eslint": "^8.16.0", | ||
"eslint": "^8.51.0", | ||
"github-label-sync": "^2.3.1", | ||
@@ -75,8 +75,8 @@ "husky": "^8.0.3", | ||
"dependencies": { | ||
"@poppinss/intl-formatter": "^3.0.0-3", | ||
"@poppinss/utils": "^6.5.0-6", | ||
"intl-messageformat": "^10.5.0", | ||
"@poppinss/intl-formatter": "^3.0.0", | ||
"@poppinss/utils": "^6.5.0", | ||
"intl-messageformat": "^10.5.4", | ||
"luxon": "^3.4.3", | ||
"negotiator": "^0.6.3", | ||
"yaml": "^2.3.2" | ||
"yaml": "^2.3.3" | ||
}, | ||
@@ -83,0 +83,0 @@ "author": "virk,adonisjs", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
62352
-3.95%39
-13.33%1681
-4.97%Updated
Updated
Updated