This project was generated with Angular CLI version 16.0.2.
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The application will automatically reload if you change any of the source files.
This Angular project demonstrates how to implement a translation system within an Angular application using a service, a custom pipe, and a language selection component.
-
Translation Service (
TranslateService
): Provides methods to retrieve the current language and set a new language. It also offers a translation function that is used throughout the project. -
Custom Pipe (
TranslatePipe
): Allows translating content in HTML templates. It is used to translate words and phrases based on the selected language. -
Language Selection Component (
LanguageSelectComponent
): A component that allows users to select their preferred language for the application. Changing the current language affects real-time translation.
-
Clone or download this repository to your local machine.
-
Configure the translation files in
language.interfaces.ts
andtranslate.service.ts
to add your own words and phrases in different languages. -
In your Angular application, import and set up the
TranslateService
and theTranslatePipe
as shown in the provided example files. -
Add the language selection component (
LanguageSelectComponent
) to the part of the application where you want users to select their language. -
Use the
TranslatePipe
in your HTML templates to translate content based on the selected language. -
Run the Angular application and observe how the content translates in real-time when changing the language.
For a complete example of how to use this translation system in Angular, refer to the app.component.ts
file. This file demonstrates how to integrate the service, pipe, and language selection component into an Angular application.
The translation dictionary is defined in the language.interfaces.ts
file. It contains key-value pairs for words and phrases in different languages. You can customize this dictionary to include your own translations.
Description:
This TypeScript file defines the TranslateService
class. It serves as the core service responsible for managing language preferences, translating content, and handling language changes. It utilizes the localStorage
to store and retrieve the user's selected language and provides methods to interact with the current language.
Description:
The TranslatePipe
is a custom Angular pipe that enables content translation within HTML templates. It relies on the TranslateService
to provide real-time translation based on the selected language. This pipe is used throughout the Angular application to translate words and phrases.
Description:
In this TypeScript file, essential language-related interfaces and enumerations are defined. It includes the LanguageDictionaryInterface
, which represents the translation dictionary, LanguageKeys
enumeration to define language keys, and the dictionary
constant that holds translations for various words and phrases in different languages.
Description:
This HTML file defines the template for the LanguageSelectComponent
. It contains an HTML select element that allows users to choose their preferred language. The selected language is used to update the application's language in real-time.
Description:
The LanguageSelectComponent
is an Angular component responsible for rendering the language selection user interface. It provides the logic to display available language options, handles language changes, and communicates with the TranslateService
to update the application's language.
These descriptions provide an overview of each file's purpose and relevance within the Angular translation example project. They can be included in your README.md to help users understand the role of each file in your project.
Contributions are welcome! If you find any issues or have ideas to enhance this example, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. Feel free to use and modify it according to your needs.
Created by Luis Mendez - Creation Date: 2023-09-10