@transifex/native 1.0.2 → 2.0.0-rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -15
- package/dist/browser.native.js +2 -2
- package/dist/browser.native.js.map +1 -1
- package/dist/node.native.js +2 -2
- package/dist/node.native.js.map +1 -1
- package/package.json +4 -4
- package/src/TxNative.js +18 -5
- package/src/utils.js +19 -0
package/README.md
CHANGED
|
@@ -4,19 +4,23 @@ A general purpose Javascript library for localizing web apps using [Transifex Na
|
|
|
4
4
|
|
|
5
5
|
Requires a Transifex Native Project Token.
|
|
6
6
|
|
|
7
|
-
Supported Node.js versions >= `
|
|
7
|
+
Supported Node.js versions >= `12.x.x`
|
|
8
8
|
|
|
9
9
|
Related packages:
|
|
10
10
|
* [@transifex/react](https://www.npmjs.com/package/@transifex/react)
|
|
11
11
|
* [@transifex/cli](https://www.npmjs.com/package/@transifex/cli)
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
# Upgrade to v2
|
|
14
|
+
|
|
15
|
+
If you are upgrading from the `1.x.x` version, please read this [migration guide](https://github.com/transifex/transifex-javascript/blob/HEAD/UPGRADE_TO_V2.md), as there are breaking changes in place.
|
|
16
|
+
|
|
17
|
+
# Quick starting guide
|
|
14
18
|
|
|
15
19
|
Install the library using:
|
|
16
20
|
|
|
17
21
|
```npm install @transifex/native --save```
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
## Webpack
|
|
20
24
|
|
|
21
25
|
```js
|
|
22
26
|
import { tx, t } from '@transifex/native';
|
|
@@ -52,7 +56,7 @@ async function main() {
|
|
|
52
56
|
main();
|
|
53
57
|
```
|
|
54
58
|
|
|
55
|
-
|
|
59
|
+
## Node.js
|
|
56
60
|
|
|
57
61
|
```js
|
|
58
62
|
const { tx, t } = require('@transifex/native');
|
|
@@ -65,7 +69,7 @@ tx.init({
|
|
|
65
69
|
...
|
|
66
70
|
```
|
|
67
71
|
|
|
68
|
-
|
|
72
|
+
## Browser
|
|
69
73
|
|
|
70
74
|
```html
|
|
71
75
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@transifex/native/dist/browser.native.min.js"></script>
|
|
@@ -88,9 +92,9 @@ tx.init({
|
|
|
88
92
|
</script>
|
|
89
93
|
```
|
|
90
94
|
|
|
91
|
-
|
|
95
|
+
# API
|
|
92
96
|
|
|
93
|
-
|
|
97
|
+
## Initialize library
|
|
94
98
|
|
|
95
99
|
```js
|
|
96
100
|
tx.init({
|
|
@@ -117,7 +121,7 @@ tx.init({
|
|
|
117
121
|
})
|
|
118
122
|
```
|
|
119
123
|
|
|
120
|
-
|
|
124
|
+
## Languages
|
|
121
125
|
|
|
122
126
|
Fetches list of project languages from CDS, useful for creating a language picker.
|
|
123
127
|
|
|
@@ -144,7 +148,7 @@ Get a list of available locales based on CDS.
|
|
|
144
148
|
tx.getLocales(): Promise(['code', 'code',...])
|
|
145
149
|
```
|
|
146
150
|
|
|
147
|
-
|
|
151
|
+
### Set current translation language
|
|
148
152
|
|
|
149
153
|
Fetches translations from the CDS and stores them in cache. When the
|
|
150
154
|
promise returns, all content will be translated to that language.
|
|
@@ -158,7 +162,7 @@ tx.setCurrentLocale('el').
|
|
|
158
162
|
catch(err => console.log(err))
|
|
159
163
|
```
|
|
160
164
|
|
|
161
|
-
|
|
165
|
+
### Get current translation language
|
|
162
166
|
|
|
163
167
|
Returns the currently selected language code.
|
|
164
168
|
|
|
@@ -169,7 +173,7 @@ tx.getCurrentLocale(): String(localeCode)
|
|
|
169
173
|
console.log(tx.getCurrentLocale())
|
|
170
174
|
```
|
|
171
175
|
|
|
172
|
-
|
|
176
|
+
## Content translation
|
|
173
177
|
|
|
174
178
|
Returns the translation of the passed source string based on the
|
|
175
179
|
currenly selected language. If the translation is not found, the returned
|
|
@@ -212,12 +216,12 @@ console.log(
|
|
|
212
216
|
// "Hello <b>Joe</b>"
|
|
213
217
|
```
|
|
214
218
|
|
|
215
|
-
|
|
219
|
+
## Escaping translations
|
|
216
220
|
|
|
217
221
|
Using the translation as is from the `t` function inside HTML is dangerous for
|
|
218
222
|
XSS attacks. The translation must be escaped based on two scenarios.
|
|
219
223
|
|
|
220
|
-
|
|
224
|
+
### Escaping text translations
|
|
221
225
|
|
|
222
226
|
```js
|
|
223
227
|
import { t, escape } from '@transifex/native';
|
|
@@ -226,7 +230,7 @@ const translation = escape(t('Hello {username}', { username }));
|
|
|
226
230
|
// translation is safe to include in HTML
|
|
227
231
|
```
|
|
228
232
|
|
|
229
|
-
|
|
233
|
+
### Escaping HTML source
|
|
230
234
|
|
|
231
235
|
HTML source content cannot be globally escaped. In that case, we can just escape
|
|
232
236
|
the ICU variables using the `_escapeVars` parameter.
|
|
@@ -240,7 +244,7 @@ const html = t('<b>Hello {username}</b>', {
|
|
|
240
244
|
});
|
|
241
245
|
```
|
|
242
246
|
|
|
243
|
-
|
|
247
|
+
## Events
|
|
244
248
|
|
|
245
249
|
Library for listening to various async events.
|
|
246
250
|
|