-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for loadersList and formattersList
- Loading branch information
1 parent
904a995
commit 70f8407
Showing
13 changed files
with
379 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* @adonisjs/i18n | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { RuntimeException } from '@poppinss/utils' | ||
|
||
import { IcuFormatter } from './icu_messages_formatter.js' | ||
import type { I18nConfig, TranslationsFormattersList } from '../types/main.js' | ||
|
||
class FormattersList { | ||
/** | ||
* List of registered formatter | ||
*/ | ||
list: Partial<TranslationsFormattersList> = { | ||
icu: () => new IcuFormatter(), | ||
} | ||
|
||
/** | ||
* Extend formatter collection and add a custom | ||
* formatter to it. | ||
*/ | ||
extend<Name extends keyof TranslationsFormattersList>( | ||
name: Name, | ||
factoryCallback: TranslationsFormattersList[Name] | ||
): this { | ||
this.list[name] = factoryCallback | ||
return this | ||
} | ||
|
||
/** | ||
* Creates the formatter instance with config | ||
*/ | ||
create<Name extends keyof TranslationsFormattersList>(name: Name, i18nConfig: I18nConfig) { | ||
const formatterFactory = this.list[name] | ||
if (!formatterFactory) { | ||
throw new RuntimeException( | ||
`Unknown i18n formatter "${String(name)}". Make sure the formatter is registered` | ||
) | ||
} | ||
|
||
return formatterFactory(i18nConfig) | ||
} | ||
} | ||
|
||
export default new FormattersList() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* @adonisjs/i18n | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { RuntimeException } from '@poppinss/utils' | ||
|
||
import { FsLoader } from './fs_loader.js' | ||
import type { I18nConfig, TranslationsLoadersList } from '../types/main.js' | ||
|
||
class LoadersList { | ||
/** | ||
* List of registered loaders | ||
*/ | ||
list: Partial<TranslationsLoadersList> = { | ||
fs: (config) => new FsLoader(config), | ||
} | ||
|
||
/** | ||
* Extend loaders collection and add a custom | ||
* loaders to it. | ||
*/ | ||
extend<Name extends keyof TranslationsLoadersList>( | ||
name: Name, | ||
factoryCallback: TranslationsLoadersList[Name] | ||
): this { | ||
this.list[name] = factoryCallback | ||
return this | ||
} | ||
|
||
/** | ||
* Creates the loaders instance with config | ||
*/ | ||
create<Name extends keyof TranslationsLoadersList>( | ||
name: Name, | ||
config: Parameters<TranslationsLoadersList[Name]>[0], | ||
i18nConfig: I18nConfig | ||
) { | ||
const loaderFactory = this.list[name] | ||
if (!loaderFactory) { | ||
throw new RuntimeException( | ||
`Unknown i18n loader "${String(name)}". Make sure the loader is registered` | ||
) | ||
} | ||
|
||
return loaderFactory(config as any, i18nConfig) | ||
} | ||
} | ||
|
||
export default new LoadersList() |
Oops, something went wrong.