Skip to content

Commit

Permalink
Added: new configuration for i18xs
Browse files Browse the repository at this point in the history
  • Loading branch information
3adel-bassiony committed Dec 26, 2023
1 parent c6a16e4 commit bfbe2f8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "i18xs",
"version": "0.7.0",
"version": "0.8.0",
"description": "A lightweight and simple package for i18n and localization for node.js",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
48 changes: 37 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ export default class I18XS {
*/
protected _fallbackLocale: string = 'en'

/**
* Indicates whether the missing identifier message should be shown.
*/
protected _showMissingIdentifierMessage: boolean = false

/**
* The message to be displayed when a localization identifier is missing.
*/
protected _missingIdentifierMessage: string = 'Missing_Localization_Identifier'

/**
* Array of locales that use right-to-left (RTL) writing direction.
*/
Expand All @@ -42,13 +52,15 @@ export default class I18XS {

/**
* Creates an instance of the I18XS class.
* @param {I18XSConfig} config - The configuration object for the I18XS instance.
* @param {I18XSConfig} config - The configuration options for the I18XS instance.
* @example
* const i18n = new I18XS({
* supportedLocales: ['en', 'fr'],
* currentLocale: 'en',
* fallbackLocale: 'en',
* rtlLocales: ['ar', 'he'],
* showMissingIdentifierMessage: true,
* missingIdentifierMessage: 'Missing_Localization_Identifier',
* rtlLocales: ['ar', 'he', 'fa'],
* localesDir: './locales',
* localization: {
* en: {
Expand All @@ -67,6 +79,8 @@ export default class I18XS {
supportedLocales = ['en'],
currentLocale = 'en',
fallbackLocale = 'en',
showMissingIdentifierMessage = false,
missingIdentifierMessage = 'Missing_Localization_Identifier',
rtlLocales = ['ar', 'he', 'fa', 'ur', 'ps', 'ckb', 'syr', 'dv', 'ug'],
localesDir = null,
localization = null,
Expand All @@ -75,6 +89,8 @@ export default class I18XS {
this._supportedLocales = supportedLocales
this._currentLocale = currentLocale
this._fallbackLocale = fallbackLocale
this._showMissingIdentifierMessage = showMissingIdentifierMessage
this._missingIdentifierMessage = missingIdentifierMessage
this._rtlLocales = rtlLocales
this._localesDir = localesDir
this._enableDebug = enableDebug
Expand Down Expand Up @@ -194,6 +210,8 @@ export default class I18XS {
* supportedLocales: ['en', 'fr'],
* currentLocale: 'en',
* fallbackLocale: 'en',
* showMissingIdentifierMessage: true,
* missingIdentifierMessage: 'Missing_Localization_Identifier',
* rtlLocales: ['ar', 'he', 'fa'],
* localesDir: '/path/to/locales',
* localization: {
Expand All @@ -213,6 +231,8 @@ export default class I18XS {
supportedLocales = ['en'],
currentLocale = 'en',
fallbackLocale = 'en',
showMissingIdentifierMessage = false,
missingIdentifierMessage = 'Missing_Localization_Identifier',
rtlLocales = ['ar', 'he', 'fa', 'ur', 'ps', 'ckb', 'syr', 'dv', 'ug'],
localesDir = null,
localization = null,
Expand All @@ -221,6 +241,8 @@ export default class I18XS {
this._supportedLocales = supportedLocales
this._currentLocale = currentLocale
this._fallbackLocale = fallbackLocale
this._showMissingIdentifierMessage = showMissingIdentifierMessage
this._missingIdentifierMessage = missingIdentifierMessage
this._rtlLocales = rtlLocales
this._localesDir = localesDir
this._enableDebug = enableDebug
Expand Down Expand Up @@ -394,24 +416,28 @@ export default class I18XS {
}

/**
* Formats a localized message based on the given identifier and data.
* If the message is not found, it returns 'Missing_Localization_Identifier'.
* If the message is an object, it handles pluralization based on the count in the data object.
* Otherwise, it simply replaces the data in the message and returns the formatted string.
*
* @param identifier - The identifier of the message to be localized.
* Formats a localized message based on the provided identifier and data.
* If the message is not found, it returns the identifier itself.
* If the message is a pluralization case, it selects the appropriate form based on the count in the data object.
* @param identifier - The identifier of the message to be formatted.
* @param data - Optional data object used for replacing placeholders in the message.
* @returns The formatted localized message.
* @returns The formatted message.
*
* @example
* const i18n = new I18nService();
* const message = i18n.formatMessage('welcome_message', { name: 'John' });
* const message = i18n.formatMessage('welcome', { name: 'John' });
* console.log(message); // Output: "Welcome, John!"
*/
formatMessage(identifier: string, data?: LocalizationData): string {
const message = this.searchForLocalization(identifier)

if (!message) return 'Missing_Localization_Identifier'
if (!message) {
if (this._showMissingIdentifierMessage) {
return this._missingIdentifierMessage
}

return identifier
}

// If message is an object, it's a pluralization case
if (typeof message === 'object') {
Expand Down
9 changes: 8 additions & 1 deletion src/tests/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('I18XS', () => {
expect(i18xs.isLTR).toBe(false)
expect(i18xs.isDebugEnabled).toBe(true)
expect(i18xs.t('Hello_World')).toBe('مرحبا بالعالم')
expect(i18xs.t('Old_Hello_World')).toBe('Missing_Localization_Identifier')
expect(i18xs.t('Old_Hello_World')).toBe('Old_Hello_World')
})

test('It should get the fallback locale', async () => {
Expand Down Expand Up @@ -245,6 +245,13 @@ describe('I18XS', () => {
const dir = `${process.cwd()}/src/tests/data/locales`
const i18xs = new I18XS({ currentLocale: 'en', localesDir: dir })

expect(i18xs.t('Key_Not_Exist')).toBe('Key_Not_Exist')
})

test('It should return a fallback message if the key not found and show missing identifier is true', async () => {
const dir = `${process.cwd()}/src/tests/data/locales`
const i18xs = new I18XS({ currentLocale: 'en', localesDir: dir, showMissingIdentifierMessage: true })

expect(i18xs.t('Key_Not_Exist')).toBe('Missing_Localization_Identifier')
})

Expand Down
2 changes: 2 additions & 0 deletions src/types/I18XSConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type I18XSConfig = {
supportedLocales?: string[]
currentLocale?: string
fallbackLocale?: string
showMissingIdentifierMessage?: boolean
missingIdentifierMessage?: string
rtlLocales?: string[]
localization?: Localization | null
localesDir?: string | null
Expand Down

0 comments on commit bfbe2f8

Please sign in to comment.