Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@translation/angular
Advanced tools
Add this package to localize your Angular application (see Installation).
Use the official Angular i18n syntax in your components.
Write only the source text in your Angular application, and keep it synchronized with your translators on Translation.io.
Need help? contact@translation.io
Mark the text in a HTML element as translatable by using the i18n
attribute in your components' templates.
<h1 i18n>Welcome to our Angular app!</h1>
<p i18n>This is a first paragraph.</p>
Mark the attributes of HTML elements as translatable by using i18n-{attribute_name}
attributes.
// Marking an image's title attribute as translatable
<img [src]="example-image" i18n-title title="Example image title" />
Mark text (literal strings) as translatable in your component code using $localize
and surrounding the text with backticks ( ` ).
// Marking a literal string as translatable
$localize `Hello, we hope you will enjoy this app.`;
To explore the syntax more in details (using IDs, specifying plurals and interpolations), please check out the section Localization syntax in details below.
Make sure that you have Angular's localize package installed, or install it.
ng add @angular/localize
Configure the i18n options in the angular.json
file at the root of your project.
@translation/angular
packageRun the following command at the root of your project to install our package:
# NPM
npm install @translation/angular
# Yarn
yarn add @translation/angular
Sign in to our platform and create your new project from the UI, selecting the appropriate source and target locales.
tio.config.json
file to the root of your application.This configuration file should look like this:
{
"api_key": "abcdefghijklmnopqrstuvwxyz123456",
"source_locale": "en",
"target_locales": ["fr", "es", "it"]
}
To make your life easier, add these lines to the package.json
at the root of your application:
{
"scripts": {
"translation:init": "npm run extract && tio init",
"translation:sync": "npm run extract && tio sync"
}
}
To push your source keys and existing translations (if any) to Translation.io, run the following command:
# NPM
npm run translation:init
# YARN
yarn translation:init
To push new translatable source keys/strings and get translations from Translation.io, simply run:
# NPM
npm run translation:sync
# YARN
yarn translation:sync
To retrieve translations without pushing new source keys, you can run:
# NPM
npm run translation:sync -- --readonly
# YARN
yarn translation:sync -- --readonly
If you need to remove unused source keys/strings from Translation.io, using your current local application as reference, run the following command:
# NPM
npm run translation:sync -- --purge
# YARN
yarn translation:sync -- --purge
Warning: all source keys/strings that are not present in your current local branch will be permanently deleted from Translation.io.
You can add or remove a locale by updating "target_locales": []
in your
tio.config.json
file, and syncing your project again.
If you want to add a new locale with existing translations (for instance if you already have a translated XLF file in your project), you will need to create a new empty project on Translation.io and init your project for them to appear.
To edit existing locales while keeping their translations (e.g. changing from en
to en-US
):
tio.config.json
file with the new API key and correct locales.Since you created a new project, the translation history and tags will unfortunately be lost.
The text in a HTML element can be marked as translatable by using the i18n
attribute in the components' templates.
<h1 i18n>Welcome to our Angular app!</h1>
<p i18n>This is a first paragraph.</p>
The attributes of HTML elements can also be marked as translatable by using i18n-{attribute_name}
attributes.
// Marking the title attribute of an image as translatable
<img [src]="example-image" i18n-title title="Example image title" />
Literal strings in your component code can also be marked as translatable using $localize
and surrounding the source text with backticks ( ` ).
// Marking a literal string as translatable
$localize `Hello, we hope you will enjoy this app.`;
The official Angular documentation for the syntax can be found here.
You can use metadata as the value of the i18n attribute to specify a custom ID, a meaning and a description.
The syntax for the metadata is the following: {meaning}|{description}@@{custom_id}
<h1 i18n="Welcome message|Message used on the homepage@@home-welcome-message">Welcome to our Angular app!</h1>
Metadata can also be used with $localize
, but it must then be formatted as follows: {meaning}:{description}@@{custom_id}:{source_text}
.
let welcomeText = $localize `Welcome message:Message used on the homepage@@home-welcome-message:Welcome to our Angular app!`;
The official Angular documentation for optional metadata can be found here.
The "unicity" of a source key is determined by its source text, its custom ID (if any) and its meaning (if any). The description plays no role in this unicity.
If you choose to use custom IDs, make sure that your IDs are unique (or that you always use the same source text with the same ID), otherwise only the first source text will be associated with the ID (see official documentation).
To avoid any problems, we strongly recommend that you opt for the use of "meanings" instead of IDs.
Note: If you use a meaning without a description, make sure to add a pipe (|
) after the meaning, otherwise it will be considered as a description.
// Good use cases:
/*
1. The meaning helps distinguish between two keys with the same source text
-> This will result in two distinct source keys
*/
<span i18n="Numbered day in a calendar|">Date</span> // Meaning only
<span i18n="Social meeting with someone|">Date</span> // Meaning only
/*
2. Adding a description after the meaning will be useful to translators
-> This will result in two distinct source keys
*/
<span i18n="Verb|Text on a button used to report a problem">Report</span>
<span i18n="Noun|Title of the Report section in the app">Report</span>
// Bad use cases:
/*
1. Using only descriptions, without meanings (note the missing pipe | )
-> This will result in only one source key
*/
<label i18n="Label for the datepicker">Date</label>
...
<option i18n="Type of event in a dropdown">Date</option>
/*
2. Using the same ID with two different source texts
-> This will result in only one source key (the first one)
*/
<h2 i18n="@@section-title">First section</h2>
<h2 i18n="@@section-title">Second section</h2>
Pluralization rules may vary from one locale to another, and it is recommended to use the plural syntax in your code to facilitate translation. This syntax is expressed as follows: { component_property, plural, pluralization_categories }
.
<span i18n>{catsCount, plural, =0 {There are no cats in the room} =1 {There is one cat in the room} other {There are {{catsCount}} cats in the room}}</span>
The official Angular documentation for plurals can be found here.
The select clause allows to display alternate text depending on the value of a variable. This syntax is expressed as follows: { component_property, select, selection_categories }
.
<span i18n>The user is {gender, select, male {a man} female {a woman} other { other }}.</span>
The official Angular documentation for select clauses can be found here.
To facilitate the work of translators, try to avoid complicated or nested expressions.
Implementations were usually started by contributors for their own projects. The following are officially supported by Translation.io and are well documented.
We are thankful to all contributors for their hard work!
Officially supported on https://translation.io/rails
Credits: @aurels, @michaelhoste
Officially supported on https://translation.io/laravel
Credits: @armandsar, @michaelhoste
Officially supported on https://translation.io/lingui
Translation.io is directly integrated in the great Lingui internationalization project.
Officially supported on https://translation.io/angular
If you want to create a new client for your favorite language or framework, please read our Create a Translation.io Library guide and use the special init and sync endpoints.
You can also use the more traditional API.
Feel free to contact us on contact@translation.io if you need some help or if you want to share your library.
The MIT License (MIT). Please see License File for more information.
FAQs
Translation.io client for Angular applications
The npm package @translation/angular receives a total of 24 weekly downloads. As such, @translation/angular popularity was classified as not popular.
We found that @translation/angular demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.