|
1 |
| -# Transifex Native for Angular |
| 1 | +# Transifex Native Angular Projects |
| 2 | + |
| 3 | +## Transifex Native for Angular |
2 | 4 |
|
3 | 5 | Angular library for localizing Angular application using
|
4 | 6 | [Transifex Native](https://www.transifex.com/native/).
|
5 | 7 |
|
6 |
| -Related packages: |
7 |
| -- [@transifex/native](https://www.npmjs.com/package/@transifex/native) |
8 |
| -- [@transifex/cli](https://www.npmjs.com/package/@transifex/cli) |
9 |
| - |
10 |
| -## Table of Contents |
11 |
| -* [Installation](#installation) |
12 |
| -* [Usage](#usage) |
13 |
| - * [Initialization](#initialization) |
14 |
| - * [T Component](#t-component) |
15 |
| - * [UT Component](#ut-component) |
16 |
| - * [TranslationService service](#translationservice-service) |
17 |
| - * [@T Decorator](#@t-decorator) |
18 |
| - * [Language Picker Component](#language-picker-component) |
19 |
| -* [License](#license) |
20 |
| - |
21 |
| - |
22 |
| -## Installation |
23 |
| - |
24 |
| -Install the library and its dependencies using: |
25 |
| - |
26 |
| -```sh |
27 |
| -npm install @transifex/native @transifex/angular --save |
28 |
| -``` |
29 |
| - |
30 |
| -## Usage |
31 |
| - |
32 |
| -### Initialization |
33 |
| - |
34 |
| -In order to use the TX Native object globally, it is necessary to initialize |
35 |
| -the library in the angular application bootstrap, in two locations: |
36 |
| - |
37 |
| -- NgModule initialization |
38 |
| - |
39 |
| -```typescript |
40 |
| -@NgModule({ |
41 |
| - declarations: [ |
42 |
| - AppComponent, |
43 |
| - LoginComponent, |
44 |
| - TermsComponent, |
45 |
| - HomeComponent, |
46 |
| - PrivacyComponent |
47 |
| - ], |
48 |
| - imports: [ |
49 |
| - AppRoutingModule, |
50 |
| - BrowserModule, |
51 |
| - |
52 |
| - // TX Native module declaration |
53 |
| - TxNativeModule.forRoot(), |
54 |
| - ], |
55 |
| - providers: [, |
56 |
| - ], |
57 |
| - bootstrap: [AppComponent] |
58 |
| -}) |
59 |
| -``` |
60 |
| - |
61 |
| -- Application Boostrap |
62 |
| - |
63 |
| -```typescript |
64 |
| -import { Component } from '@angular/core'; |
65 |
| -import { TranslationService } from '@transifex/angular'; |
66 |
| - |
67 |
| -@Component({ |
68 |
| - selector: 'app-root', |
69 |
| - templateUrl: './app.component.html', |
70 |
| - styleUrls: ['./app.component.scss'] |
71 |
| -}) |
72 |
| -export class AppComponent { |
73 |
| - constructor(private translationService: TranslationService) { |
74 |
| - // TX Native library intialization |
75 |
| - translationService.init({ |
76 |
| - token: '----- here your TX Native token ------', |
77 |
| - }); |
78 |
| - } |
79 |
| - |
80 |
| - async ngOnInit() { |
81 |
| - await this.translationService.getLanguages(); |
82 |
| - await this.translationService.setCurrentLocale('el'); |
83 |
| - } |
84 |
| -} |
85 |
| -``` |
86 |
| - |
87 |
| -### `T` Component |
88 |
| - |
89 |
| -```html |
90 |
| - <p> |
91 |
| - <label> |
92 |
| - <T str="Password" key="label.password"></T> |
93 |
| - </label> |
94 |
| - <input type="password" name="password" /> |
95 |
| - </p> |
96 |
| - |
97 |
| -``` |
98 |
| - |
99 |
| -Available optional props: |
100 |
| - |
101 |
| -| Prop | Type | Description | |
102 |
| -|------------|---------|---------------------------------------------| |
103 |
| -| context | String | String context, affects key generation | |
104 |
| -| key | String | Custom string key | |
105 |
| -| comment | String | Developer comment | |
106 |
| -| charlimit | Number | Character limit instruction for translators | |
107 |
| -| tags | String | Comma separated list of tags | |
108 |
| -| escapeVars | Boolean | If escaping should be applied to ICU variables | |
109 |
| -| inline | Boolean | If should wrap the translation with `span` | |
110 |
| -| sanitize | Boolean | Safe render of the HTML result after translation takes place | |
111 |
| -| vars | Object | ICU variables to render in the string | |
112 |
| - |
113 |
| -The T component can sanitize the translated result if HTML is involved, using the parameter `sanitize`, ie this would be possible: |
114 |
| - |
115 |
| -```html |
116 |
| -<p> |
117 |
| - <T |
118 |
| - str="By proceeding you agree to the {terms_of_services} and {privacy_policy}." |
119 |
| - key="text.agree_message" |
120 |
| - [sanitize]=true |
121 |
| - [vars]="{ terms_of_services: '<a href=\'terms\'>' + terms + '</a>', |
122 |
| - privacy_policy: '<a href=\'privacy\'>' + privacy + '</a>' |
123 |
| - }"> |
124 |
| - </T> |
125 |
| - </p> |
126 |
| -``` |
127 |
| - |
128 |
| -This will render like this in English: |
129 |
| - |
130 |
| -```html |
131 |
| -<span>By proceeding you agree to the <a href="terms">terms of service</a> and <a href="privacy">privacy policy</a>.</span> |
132 |
| -``` |
133 |
| - |
134 |
| -And like this in Greek: |
135 |
| - |
136 |
| -```html |
137 |
| -<span>Συνεχίζοντας, αποδέχεστε τους <a href="terms">όροι χρήσης</a> και τους <a href="privacy">πολιτική απορρήτου</a>.</span> |
138 |
| -``` |
139 |
| - |
140 |
| -The same block without the `sanitize` option would be like this, for Greek: |
141 |
| - |
142 |
| -```html |
143 |
| -Συνεχίζοντας, αποδέχεστε τους <a href='terms'>όροι χρήσης</a> και τους <a href='privacy'>πολιτική απορρήτου</a>. |
144 |
| -``` |
145 |
| - |
146 |
| -The main thing to keep in mind is that the `str` property to the T component |
147 |
| -must **always** be a valid ICU message format template. |
148 |
| - |
149 |
| - |
150 |
| -### `UT` Component |
151 |
| - |
152 |
| -```html |
153 |
| - <p> |
154 |
| - <UT |
155 |
| - str="Copyright {year} by Transifex." |
156 |
| - key="text.copyright" |
157 |
| - [inline]=false |
158 |
| - comment="This is the current year" |
159 |
| - [vars]="{ year: '© 2020' }"> |
160 |
| - </UT> |
161 |
| - </p> |
162 |
| - |
163 |
| -``` |
164 |
| - |
165 |
| -`UT` has the same behaviour as `T`, but renders source string as HTML inside a |
166 |
| -`div` tag or a `span` tag if `inline` property is true. |
167 |
| - |
168 |
| -### `TranslationService` service |
169 |
| - |
170 |
| -This is the main service exposed from the SDK in order to intialize the TX Native object. |
171 |
| - |
172 |
| -In your bootstrap entry point in the Angular application, you should initialize the SDK, like this: |
173 |
| - |
174 |
| -```typescript |
175 |
| -import { Component } from '@angular/core'; |
176 |
| -import { TranslationService } from '@transifex/angular'; |
177 |
| - |
178 |
| -@Component({ |
179 |
| - selector: 'app-root', |
180 |
| - templateUrl: './app.component.html', |
181 |
| - styleUrls: ['./app.component.scss'] |
182 |
| -}) |
183 |
| -export class AppComponent { |
184 |
| - title = 'TX Native Angular Demo'; |
185 |
| - |
186 |
| - constructor(private translationService: TranslationService) { |
187 |
| - translationService.init({ |
188 |
| - token: '----- here your TX Native token ------', |
189 |
| - }); |
190 |
| - } |
191 |
| - |
192 |
| - async ngOnInit() { |
193 |
| - await this.translationService.getLanguages(); |
194 |
| - await this.translationService.setCurrentLocale('el'); |
195 |
| - } |
196 |
| -} |
197 |
| -``` |
198 |
| -The translation service is a `singleton` instance so the initialization will be shared across the whole application. |
199 |
| - |
200 |
| -Exposes the following methods and properties: |
201 |
| - |
202 |
| -| Method | Parameters | Description | |
203 |
| -|------------------|------------------|---------------------------------------------------| |
204 |
| -| init | config <sup>1</sup> | Initializes the TX Native object | |
205 |
| -| setCurrentLocale | none | Set the current locale in the TX Native object | |
206 |
| -| getCurrentLocale | none | Returns the current locale of the TX Native object| |
207 |
| -| getLanguages | none | Returns an array of available languages | |
208 |
| -| translate | translate params <sup>2</sup> | Returns the translation for a string with given translation params | |
209 |
| - |
210 |
| -<sup>(1)</sup> Initialization config |
211 |
| - |
212 |
| -```typescript |
213 |
| -export interface ITranslationServiceConfig { |
214 |
| - token: string; |
215 |
| - cdsHost?: string; |
216 |
| - filterTags?: string; |
217 |
| - cache?: () => void; |
218 |
| - missingPolicy?: () => void; |
219 |
| - errorPolicy?: () => void; |
220 |
| -} |
221 |
| -``` |
222 |
| -- `cache`, `missingPolicy` and `errorPolicy` are set by default by |
223 |
| -`@transifex/native` package but you can provide if you wish custom functions |
224 |
| -of your own, or use another policy provided by the `@transifex/native` package. |
225 |
| - |
226 |
| -Please check the documentation related to this on`@transifex/native` package [here](https://github.com/transifex/transifex-javascript/tree/master/packages/native). |
227 |
| - |
228 |
| -<sup>(2)</sup> Translation params |
229 |
| - |
230 |
| -```typescript |
231 |
| -str: string // string to be translated |
232 |
| -params: Record<string, unknown> // an object with the params and variables |
233 |
| -``` |
234 |
| - |
235 |
| -The params should follow the interface: |
236 |
| - |
237 |
| -```typescript |
238 |
| -export interface ITranslateParams { |
239 |
| - _context?: string; |
240 |
| - _comment?: string; |
241 |
| - _charlimit?: number; |
242 |
| - _tags?: string; |
243 |
| - _key?: string; |
244 |
| - _escapeVars?: boolean; |
245 |
| - _inline?: boolean; |
246 |
| - sanitize?: boolean; |
247 |
| -} |
248 |
| -``` |
249 |
| - |
250 |
| -### `@T` Decorator |
251 |
| - |
252 |
| -This is a decorator for using inside classes and components in order to have |
253 |
| -properties with the translation and used them in code and templates. |
254 |
| - |
255 |
| -An example of use is the following: |
256 |
| - |
257 |
| -```typescript |
258 |
| -import { Component, OnInit } from '@angular/core'; |
259 |
| -import { Router } from '@angular/router'; |
260 |
| -import { T, TranslationService } from '@transifex/angular'; |
261 |
| - |
262 |
| -@Component({ |
263 |
| - selector: 'app-login', |
264 |
| - templateUrl: './login.component.html', |
265 |
| - styleUrls: ['./login.component.scss'] |
266 |
| -}) |
267 |
| -export class LoginComponent implements OnInit { |
268 |
| - // Translations using decorator |
269 |
| - @T('Monday', { _key: 'text.monday' }) |
270 |
| - weekday: string; |
271 |
| - @T('terms of service', { _key: 'text.terms_of_service' }) |
272 |
| - terms: string; |
273 |
| - @T('privacy policy', { _key: 'text.privacy_policy' }) |
274 |
| - privacy: string; |
275 |
| - |
276 |
| - constructor( |
277 |
| - private translationService: TranslationService, |
278 |
| - private router: Router) {} |
279 |
| - |
280 |
| - login() { |
281 |
| - this.router.navigateByUrl('home'); |
282 |
| - } |
283 |
| -} |
284 |
| -``` |
285 |
| - |
286 |
| -and the use of the properties in the template: |
287 |
| - |
288 |
| -```html |
289 |
| - <p> |
290 |
| - <T |
291 |
| - str="By proceeding you agree to the {terms_of_services} and {privacy_policy}." |
292 |
| - key="text.agree_message" |
293 |
| - [sanitize]=true |
294 |
| - [vars]="{ terms_of_services: '<a href=\'#/terms\'>' + terms + '</a>', |
295 |
| - privacy_policy: '<a href=\'#/privacy\'>' + privacy + '</a>' |
296 |
| - }" |
297 |
| - ></T> |
298 |
| - </p> |
299 |
| -``` |
300 |
| - |
301 |
| -### Language Picker Component |
302 |
| - |
303 |
| -Renders a `<select>` tag that displays supported languages and switches the |
304 |
| -application's selected language on change. |
305 |
| -Uses `Translation Service` internally. |
306 |
| - |
307 |
| -The html selector is `tx-language-picker`. |
308 |
| - |
309 |
| -This is an example of use for the language picker component: |
310 |
| - |
311 |
| -```html |
312 |
| -<tx-language-picker |
313 |
| - className="placeBottomLeft" |
314 |
| - (localeChanged)="onLocaleChanged($event)"></tx-language-picker> |
315 |
| -``` |
316 |
| - |
317 |
| -and the event for locale changed inside the component could be: |
318 |
| - |
319 |
| -```typescript |
320 |
| -import { Component, OnInit } from '@angular/core'; |
321 |
| -import { Router } from '@angular/router'; |
322 |
| -import { T, TranslationService } from '@transifex/angular'; |
323 |
| - |
324 |
| -@Component({ |
325 |
| - selector: 'app-login', |
326 |
| - templateUrl: './login.component.html', |
327 |
| - styleUrls: ['./login.component.scss'] |
328 |
| -}) |
329 |
| -export class LoginComponent implements OnInit { |
330 |
| - onLocaleChanged(event) { |
331 |
| - // here we can do any action when locale changes |
332 |
| - } |
333 |
| -} |
334 |
| -``` |
335 |
| -Accepts properties: |
336 |
| - |
337 |
| -- `className`: The CSS class that will be applied to the `<select>` tag |
338 |
| - |
339 |
| -Returns: |
340 |
| - |
341 |
| -- `localeChanged`: event for handling the change of locale |
342 |
| - |
343 |
| -You always can implement a language picker of your choice, injecting |
344 |
| -the `TranslationService` and using the different methods provided, |
345 |
| -such as `getLanguages`. |
346 |
| - |
347 |
| - |
348 |
| -# License |
349 |
| - |
350 |
| -Licensed under Apache License 2.0, see [LICENSE](https://github.com/transifex/transifex-javascript/blob/HEAD/LICENSE) file. |
| 8 | +[Read more](https://github.com/transifex/transifex-javascript/tree/master/packages/angular/projects/tx-native-angular-sdk) |
0 commit comments