Skip to content

Commit bc1e314

Browse files
committed
chore(i18n): implement dynamic loading of translation bundles and add TranslationBundleConstants
1 parent b1ab08b commit bc1e314

File tree

7 files changed

+75
-20
lines changed

7 files changed

+75
-20
lines changed

packages/i18n/.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../.editorconfig
1+
../../.editorconfig

packages/i18n/.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/dist
22
/build
33
/node_modules
4-
/coverage
4+
/coverage

packages/i18n/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
"publishConfig": {
5858
"access": "public"
5959
}
60-
}
60+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
import {en_US} from '../translations';
20+
21+
/**
22+
* Constants related to internationalization (i18n) translation bundles.
23+
*
24+
* @example
25+
* ```typescript
26+
* // Using default locale
27+
* const locale = TranslationBundleConstants.FALLBACK_LOCALE;
28+
*
29+
* // Using supported locales
30+
* const locales = TranslationBundleConstants.DEFAULT_LOCALES;
31+
* ```
32+
*/
33+
const TranslationBundleConstants = {
34+
/**
35+
* Default locale code used as fallback when no specific locale is provided.
36+
*/
37+
FALLBACK_LOCALE: en_US.metadata.localeCode,
38+
39+
/**
40+
* List of default locales bundles with the SDKs.
41+
*
42+
* Current default locales:
43+
* - `en-US` - English (United States)
44+
*/
45+
DEFAULT_LOCALES: [en_US.metadata.localeCode],
46+
} as const;
47+
48+
export default TranslationBundleConstants;

packages/i18n/src/translations/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,4 @@
1616
* under the License.
1717
*/
1818

19-
// IMPORTANT: Only include default bundled languages here.
20-
// Current List:
21-
// 1. en-US
22-
2319
export {default as en_US} from './en-US';

packages/i18n/src/utils/getDefaultI18nBundles.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,34 @@
1616
* under the License.
1717
*/
1818

19-
import * as i18n from '../translations';
19+
import TranslationBundleConstants from '../constants/TranslationBundleConstants';
20+
import {I18nBundle} from '../models/i18n';
21+
import * as translations from '../translations';
2022

2123
/**
2224
* Get the default i18n bundles.
25+
* Dynamically builds the bundles collection by iterating through supported locales
26+
* and importing their corresponding translation modules.
2327
*
24-
* @param locale - The locale to get the bundle for (defaults to 'en-US')
25-
* @returns The i18n bundle for the specified locale
28+
* @returns The collection of all default i18n bundles
2629
*/
27-
const getDefaultI18nBundles = () => {
28-
return i18n;
30+
const getDefaultI18nBundles = (): Record<string, I18nBundle> => {
31+
const bundles: Record<string, I18nBundle> = {};
32+
33+
// Iterate through supported locales and build bundles dynamically
34+
TranslationBundleConstants.DEFAULT_LOCALES.forEach(localeCode => {
35+
// Convert locale code to translation module key (e.g., 'en-US' -> 'en_US')
36+
const moduleKey = localeCode.replace('-', '_') as keyof typeof translations;
37+
38+
// Get the translation bundle from the translations module
39+
const bundle = translations[moduleKey] as I18nBundle;
40+
41+
if (bundle && bundle.metadata?.localeCode) {
42+
bundles[bundle.metadata.localeCode] = bundle;
43+
}
44+
});
45+
46+
return bundles;
2947
};
3048

3149
export default getDefaultI18nBundles;

packages/i18n/tsconfig.eslint.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
{
22
"extends": "./tsconfig.json",
3-
"include": [
4-
"**/.*.js",
5-
"**/.*.cjs",
6-
"**/.*.ts",
7-
"**/*.js",
8-
"**/*.cjs",
9-
"**/*.ts",
10-
]
3+
"include": ["**/.*.js", "**/.*.cjs", "**/.*.ts", "**/*.js", "**/*.cjs", "**/*.ts"]
114
}

0 commit comments

Comments
 (0)