Internationalization (i18n)
Relevant source files
The following files were used as context for generating this wiki page:
- crowdin.yml
- packages/admin-web-angular/src/assets/i18n/en.json
- packages/shop-mobile-ionic/src/assets/i18n/en-US.json
- packages/admin-web-angular/src/polyfills.ts
- packages/admin-web-angular/src/app/pages/+customers/+customer/ea-customer-info/ea-customer-info.stories.ts
- packages/shop-mobile-flutter/README.md
- README.md
The Ever Demand platform is designed with multi-language support as a core feature, enabling its various applications to be translated and presented in different languages and cultural formats. The internationalization (i18n) strategy is centralized around file-based translations managed by the Crowdin localization platform. This ensures a consistent translation workflow across the entire monorepo, from the backend API to the various frontend applications.
The primary approach involves using JSON files for storing translation strings. Each package within the platform that requires translation contains its own set of language files. In Angular-based applications, the @ngx-translate library is the main tool for loading and displaying these translations.
Sources: README.md:43, crowdin.yml
Translation Management with Crowdin
The project uses Crowdin to manage the translation workflow. The configuration is defined in the root crowdin.yml file, which orchestrates how source files are found and where translated files are placed.
Sources: crowdin.yml
Crowdin Configuration
The crowdin.yml file specifies the source language files (typically English) and the pattern for outputting translated files. The system is configured to preserve the directory hierarchy, meaning translations are stored alongside the source files within each package.
| Parameter | Value | Description |
|---|---|---|
api_key_env | CROWDIN_API_KEY | The environment variable used to store the Crowdin API key for authentication. |
base_path | . | The root of the repository is the base path for all file operations. |
preserve_hierarchy | true | Ensures that the directory structure of the source files is maintained for the translated files. |
Sources: crowdin.yml:2-5
Translation File Mapping
The configuration defines a set of source files and their corresponding translation destinations. This pattern is repeated for all translatable packages in the monorepo.
This diagram illustrates the flow of translation files from the repository to Crowdin and back.
Below is a summary of the file mappings:
| Source File Path | Translation File Path Pattern |
|---|---|
/packages/admin-web-angular/src/assets/i18n/en.json | /packages/admin-web-angular/src/assets/i18n/%locale%.json |
/packages/shop-mobile-ionic/src/assets/i18n/en-US.json | /packages/shop-mobile-ionic/src/assets/i18n/%locale%.json |
/packages/carrier-mobile-ionic/src/assets/i18n/en-US.json | /packages/carrier-mobile-ionic/src/assets/i18n/%locale%.json |
/packages/merchant-tablet-ionic/src/assets/i18n/en-US.json | /packages/merchant-tablet-ionic/src/assets/i18n/%locale%.json |
/packages/shop-web-angular/src/assets/i18n/en.json | /packages/shop-web-angular/src/assets/i18n/%locale%.json |
/packages/core/src/i18n/en.json | /packages/core/src/i18n/%language%.json |
Sources: crowdin.yml:7-26
Frontend Implementation (Angular)
The Angular applications, such as admin-web-angular, use the @ngx-translate library suite to handle i18n. This involves initializing the Angular localization framework, configuring the translation module, and loading the appropriate language files.
Initialization
To enable i18n in Angular templates, the @angular/localize package is initialized within the application's polyfills. This is a prerequisite for Angular's built-in i18n features.
// packages/admin-web-angular/src/polyfills.ts:60-63
/******************************************************************
* Load `$localize` - used if i18n tags appear in Angular templates.
*/
import '@angular/localize/init';
Sources: packages/admin-web-angular/src/polyfills.ts:63
Module Configuration
The TranslateModule is imported into the application, often via a shared I18nModule. It is configured with a loader, TranslateHttpLoader, which is responsible for fetching the JSON translation files from a server endpoint.
The following sequence diagram shows how a component retrieves a translated string.
A typical setup for the TranslateModule in a feature module or Storybook story is shown below. This example demonstrates how the TranslateHttpLoader is provided to fetch files from a /i18n/ path.
// packages/admin-web-angular/src/app/pages/+customers/+customer/ea-customer-info/ea-customer-info.stories.ts:47-49
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, '/i18n/', '.json');
}
// packages/admin-web-angular/src/app/pages/+customers/+customer/ea-customer-info/ea-customer-info.stories.ts:62-63
// ...
imports: [
//...
I18nModule,
//...
],
// ...
Translation File Structure
The translation files are simple key-value JSON objects. Keys are typically structured hierarchically using dot notation to group related translations, which helps in organizing the text for different parts of the application.
Example from the admin web application:
// packages/admin-web-angular/src/assets/i18n/en.json
{
"MENU": {
"DASHBOARD": "Dashboard",
"USERS": "Users",
"CUSTOMERS": "Customers"
},
"BUTTONS": {
"ADD": "Add",
"SAVE": "Save",
"CANCEL": "Cancel"
}
}
Sources: packages/admin-web-angular/src/assets/i18n/en.json
Other Applications
The same file-based i18n strategy is applied to other applications in the platform, ensuring consistency.
Ionic Mobile Apps
The Ionic-based mobile apps (shop-mobile-ionic, carrier-mobile-ionic, merchant-tablet-ionic) also use JSON files for translations. The structure is identical to the web applications, though the specific library used to consume them may differ.
Example from the shop mobile app:
// packages/shop-mobile-ionic/src/assets/i18n/en-US.json
{
"HOME": {
"WELCOME": "Welcome to Ever",
"SELECT_YOUR_CITY": "Select your city"
},
"AUTH": {
"LOGIN": "Login",
"SIGN_UP": "Sign Up"
}
}
Sources: packages/shop-mobile-ionic/src/assets/i18n/en-US.json
Flutter Mobile App
The Flutter version of the shop mobile app also follows this pattern. Its project structure includes an assets/i18n directory, indicating that it loads translation strings from asset files at runtime.
// packages/shop-mobile-flutter/README.md:32
-> assets : here we have fonts, i18n, colors and images
Sources: packages/shop-mobile-flutter/README.md:32
Summary
The internationalization system in Ever Demand is robust and scalable, built on a foundation of file-based JSON translations. By leveraging Crowdin for translation management and standard libraries like @ngx-translate for implementation, the platform can efficiently support multiple languages across its diverse set of web and mobile applications. This centralized approach simplifies the process of adding new languages and maintaining existing ones, providing a consistent user experience for a global audience.