|
| 1 | +# How to Add a New Language in an ASP.NET Zero Angular Application |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +This document provides step-by-step instructions for adding a new localization (language) in your ASP.NET Zero Angular application. Localization allows users to interact with the application in their preferred language, thereby improving usability and accessibility. For more details on localization in ASP.NET Boilerplate, refer to the official documentation: [ASP.NET Boilerplate Localization](https://aspnetboilerplate.com/Pages/Documents/Localization). |
| 6 | + |
| 7 | +## Steps to Add a New Language |
| 8 | + |
| 9 | +### 1. Register the New Language in the Backend |
| 10 | + |
| 11 | +To support a new language in the backend, you need to register it in the `DefaultLanguagesCreator.cs` file located in the `YourProjectName.Core` project. |
| 12 | + |
| 13 | +#### Modify DefaultLanguagesCreator.cs |
| 14 | + |
| 15 | +In this step, you need to add the new language to the list of initial languages registered in the system. This ensures that the application recognizes the new language and makes it available for selection. Modify the `GetInitialLanguages` method to include the new language code, display name, and flag icon. |
| 16 | + |
| 17 | +**DefaultLanguagesCreator.cs** |
| 18 | +```c# |
| 19 | +public class DefaultLanguagesCreator |
| 20 | +{ |
| 21 | + public static List<ApplicationLanguage> InitialLanguages => GetInitialLanguages(); |
| 22 | + |
| 23 | + private readonly AbpZeroTemplateDbContext _context; |
| 24 | + |
| 25 | + private static List<ApplicationLanguage> GetInitialLanguages() |
| 26 | + { |
| 27 | + var tenantId = AbpZeroTemplateConsts.MultiTenancyEnabled ? null : (int?)1; |
| 28 | + return new List<ApplicationLanguage> |
| 29 | + { |
| 30 | + new ApplicationLanguage(tenantId, "en", "English", "famfamfam-flags us"), |
| 31 | + new ApplicationLanguage(tenantId, "pl", "Polski", "famfamfam-flags pl"), |
| 32 | + //Other languages |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + //... |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +In the code above: |
| 41 | + |
| 42 | +- "pl" is the language code for Polish. |
| 43 | +- "Polski" is the display name. |
| 44 | +- "famfamfam-flags pl" refers to the flag icon for Polish. |
| 45 | + |
| 46 | +#### Create the Localization XML File |
| 47 | + |
| 48 | +The localization XML file contains key-value pairs for translating text within the application. Each language has its own XML file where you define translations for UI elements, messages, and labels. |
| 49 | +This file ensures that the application can display content in the selected language. Create a new localization file in the **Localization** folder of the `YourProjectName.Core` project. |
| 50 | + |
| 51 | +**YourProjectName-pl.xml** |
| 52 | +```xml |
| 53 | +<?xml version="1.0" encoding="utf-8" ?> |
| 54 | +<localizationDictionary culture="pl"> |
| 55 | + <texts> |
| 56 | + <text name="HomePage">Strona główna</text> |
| 57 | + <text name="Logout">Wyloguj się</text> |
| 58 | + <!-- Add other translations here --> |
| 59 | + </texts> |
| 60 | +</localizationDictionary> |
| 61 | +``` |
| 62 | + |
| 63 | +### 2. Add Language to Angular |
| 64 | + |
| 65 | +To fully support the new language in the Angular UI, you need to update various localization settings, including date picker localization. |
| 66 | + |
| 67 | +#### Configure Date Localization in ngx-bootstrap |
| 68 | + |
| 69 | +To ensure that the ngx-bootstrap date picker supports the new language, update the `ngx-bootstrap-datepicker-config.service.ts` file. This configuration allows the date picker to display dates in the correct format for the selected language. |
| 70 | + |
| 71 | +**Import the Locale** |
| 72 | + |
| 73 | +Add the new language locale to the imports from `ngx-bootstrap/chronos`: |
| 74 | + |
| 75 | +```javascript |
| 76 | +import { |
| 77 | + enGbLocale, |
| 78 | + plLocale, // Add import for the new language here |
| 79 | + //Other locales |
| 80 | +} from 'ngx-bootstrap/chronos'; |
| 81 | +``` |
| 82 | + |
| 83 | +**Update the `registerNgxBootstrapDatePickerLocales` Method** |
| 84 | + |
| 85 | +Modify the method to include the new language inside the locales object: |
| 86 | + |
| 87 | +```javascript |
| 88 | +static registerNgxBootstrapDatePickerLocales(): Promise<boolean> { |
| 89 | + if (abp.localization.currentLanguage.name === 'en') { |
| 90 | + return Promise.resolve(true); |
| 91 | + } |
| 92 | + |
| 93 | + const locales: { [key: string]: any } = { |
| 94 | + 'en': enGbLocale, |
| 95 | + 'pl': plLocale, // Add new language here |
| 96 | + //Other languages |
| 97 | + }; |
| 98 | + |
| 99 | + let localeKey = abp.localization.currentLanguage.name.toLowerCase(); |
| 100 | + |
| 101 | + if (locales[localeKey]) { |
| 102 | + defineLocale(localeKey, locales[localeKey]); |
| 103 | + return Promise.resolve(true); |
| 104 | + } |
| 105 | + |
| 106 | + return Promise.reject(`Locale ${localeKey} not found`); |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +This method ensures that the correct locale is loaded based on the currently selected language in the application. If the language is not found in the locales object, an error is returned. |
| 111 | + |
| 112 | +### 3. Verify and Test the Localization Changes |
| 113 | +After implementing the necessary changes, follow these steps to verify that the new language is properly integrated into the application: |
| 114 | + |
| 115 | +- **Restart the Application** |
| 116 | + - Restart both the backend and frontend to ensure that all localization changes take effect. |
| 117 | +- **Select the New Language** |
| 118 | + - In your app, select the newly added language from the language selection drop-down menu and verify that user interface elements such as menus and labels are updated appropriately. |
| 119 | +- **Test Date Localization** |
| 120 | + - Open any date pickers and confirm that dates are displayed in the correct format for the selected language. |
| 121 | + |
| 122 | +> Note: The newly added language and its XML content are stored in the database when the application runs. |
| 123 | +
|
| 124 | +## Conclusion |
| 125 | +By following these steps, you have successfully added support for a new language in your ASP.NET Zero Angular application. The localization process ensures that both backend and frontend components, including UI elements and date pickers, properly reflect the new language. |
| 126 | + |
| 127 | +For more information, refer to the official documentation: |
| 128 | +- [ASP.NET Boilerplate Localization](https://aspnetboilerplate.com/Pages/Documents/Localization). |
| 129 | +- [ASP.NET Zero Language Management](https://aspnetboilerplate.com/Pages/Documents/Zero/Language-Management). |
0 commit comments