Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@translation/angular

Package Overview
Dependencies
Maintainers
3
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@translation/angular

Translation.io client for Angular applications

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42
decreased by-38.24%
Maintainers
3
Weekly downloads
 
Created
Source

Translation.io client for Angular

Software License Build Status

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.

Translation.io interface

Need help? contact@translation.io

Table of contents

Localization syntax overview

i18n attribute in templates

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" />

$localize for literal strings in code

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.

Installation

1. Install the localize package and set up your i18n options

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.

2. Install the @translation/angular package

Run the following command at the root of your project to install our package:

# NPM
npm install @translation/angular

# Yarn
yarn add @translation/angular

3. Create a new translation project

Sign in to our platform and create your new project from the UI, selecting the appropriate source and target locales.

4. Copy the generated 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"]
}

5. Add scripts to your package.json

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"
  }
}

6. Initialize your project

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

Usage

Sync

To push new translatable source keys/strings and get translations from Translation.io, simply run:

# NPM
npm run translation:sync

# YARN
yarn translation:sync

Read-only Sync

To retrieve translations without pushing new source keys, you can run:

# NPM
npm run translation:sync -- --readonly

# YARN
yarn translation:sync -- --readonly

Sync & Purge

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.

Manage locales

Add or remove locales

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.

Edit locales

To edit existing locales while keeping their translations (e.g. changing from en to en-US):

  1. Create a new project on Translation.io with the correct locales.
  2. Update the tio.config.json file with the new API key and correct locales.
  3. Adapt the locale codes in the XLF files' names.
  4. Initialize your new project and check that everything went fine.
  5. Invite your collaborators in the new project.
  6. Remove the old project.

Since you created a new project, the translation history and tags will unfortunately be lost.

Localization syntax in details

i18n attributes and $localize

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.

Optional metadata for translation

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.

Our recommendations for metadata

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>

ICU expressions (plural and select)

Pluralization

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.

Select clause

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.

Our recommendations for plural and select expressions

To facilitate the work of translators, try to avoid complicated or nested expressions.

List of clients for Translation.io

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!

Ruby on Rails (Ruby)

Officially supported on https://translation.io/rails

Credits: @aurels, @michaelhoste

Laravel (PHP)

Officially supported on https://translation.io/laravel

Credits: @armandsar, @michaelhoste

React, React Native and JavaScript

Officially supported on https://translation.io/lingui

Translation.io is directly integrated in the great Lingui internationalization project.

Angular

Officially supported on https://translation.io/angular

Others

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.

Licence

The MIT License (MIT). Please see License File for more information.

Keywords

FAQs

Package last updated on 26 Sep 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc