|
1 | | -# react-l20n-u |
2 | | -Mozilla's L20n localization framework for React |
3 | | -Universal (isomorphic) works in Node (server) and Webpack (client). |
4 | | - |
5 | | -The l20n module dependancy uses default parameter value so using Node v6+ is recommended. |
6 | | -If you see an error like `SyntaxError: Unexpected token =` try updating to Node v6+. |
7 | | - |
8 | | -## Getting Started: |
9 | | - |
10 | | -### Install it from [npm](http://www.npmjs.org): |
11 | | -`npm i react-l20n-u` |
12 | | - |
13 | | -### Use it in your React App: |
14 | | - |
15 | | -```js |
16 | | -import L20n, { L20nElement } from 'react-l20n-u'; |
17 | | - |
18 | | -// Load an ftl file with localization string |
19 | | -L20n.load('en', require('./locales/en-US.ftl')) |
20 | | -// Or load localization string inline |
21 | | -L20n.load('fr', ` |
22 | | -product-name = React L20n |
23 | | -messages = {$count} messages |
24 | | -login-input = Default value |
25 | | - [html/placeholder] example@email.com |
26 | | - [html/aria-label] Login input value |
27 | | - [html/title] Type your login email |
28 | | -`) |
29 | | - |
30 | | -// Use L20n.get to retrieve localized strings by key |
31 | | -<div> |
32 | | - <h1>Product name: { L20n.get('product-name') }</h1> |
33 | | - <h2>Message count: { L20n.get('messages', { count: 2 }) }</h2> |
34 | | -</div> |
35 | | - |
36 | | -// Or use the L20nElement React component to render HTML elements |
37 | | -<div> |
38 | | - <L20nElement id="login-input" renderAs="input" /> |
39 | | -</div> |
40 | | -``` |
41 | | - |
42 | | -## Universal (isomorphic) support |
43 | | - |
44 | | -```js |
45 | | -// To load (require) ftl files with webpack use [raw-loader](https://github.com/webpack/raw-loader) |
46 | | -loaders: [ |
47 | | - { |
48 | | - test: /\.ftl?$/, |
49 | | - loader: 'raw-loader', |
50 | | - exclude: /node_modules/ |
51 | | - } |
52 | | -] |
53 | | - |
54 | | -// To load (require) ftl files in node add the ftl extension |
55 | | -require.extensions['.ftl'] = function(module, filename) { |
56 | | - var content = fs.readFileSync(filename, 'utf8'); |
57 | | - module.exports = content; |
58 | | -}; |
59 | | -``` |
60 | | - |
61 | | -## Methods |
62 | | - |
63 | | -### load(locale, ftl) |
64 | | -Load localized text strings from an ftl string. |
65 | | -User require to load from an ftl file (see Universal support above). |
66 | | - |
67 | | -### get(key, props, locale) |
68 | | -Get a localized text by key. |
69 | | -The returned value is a React span element. |
70 | | -HTML tags in your text will be rendered as HTML. |
71 | | -Optionally pass some properties or an explicit locale. |
72 | | -If no locale is given the default will be used. |
73 | | - |
74 | | -### getRaw(key, props, locale) |
75 | | -Get a localized text by key. |
76 | | -The returned value is a string. |
77 | | -HTML tags in your text will be rendered as text. |
78 | | -Optionally pass some properties or an explicit locale. |
79 | | -If no locale is given the default will be used. |
80 | | - |
81 | | -## Options |
82 | | - |
83 | | -### defaultLocale (string) |
84 | | -The default language used to retrieve localized strings. |
85 | | -Sample: `L20n.defaultLocale = 'nl'` |
86 | | - |
87 | | -### fallbackToDefault (bool) |
88 | | -Determines whether to use the default locale if the requested locale could not be found |
| 1 | +# react-l20n-u |
| 2 | +Mozilla's L20n localization framework for React |
| 3 | +Universal (isomorphic) works in Node (server) and Webpack (client). |
| 4 | + |
| 5 | +The l20n module dependancy uses default parameter value so using Node v6+ is recommended. |
| 6 | +If you see an error like `SyntaxError: Unexpected token =` try updating to Node v6+. |
| 7 | + |
| 8 | +## Getting Started: |
| 9 | + |
| 10 | +### Install it from [npm](http://www.npmjs.org): |
| 11 | +`npm i react-l20n-u` |
| 12 | + |
| 13 | +### Use it in your React App: |
| 14 | + |
| 15 | +```js |
| 16 | +import L20n, { L20nElement } from 'react-l20n-u'; |
| 17 | + |
| 18 | +// Load an ftl file with localization string |
| 19 | +L20n.load('en', require('./locales/en-US.ftl')) |
| 20 | +// Or load localization string inline |
| 21 | +L20n.load('fr', ` |
| 22 | +product-name = React L20n |
| 23 | +messages = {$count} messages |
| 24 | +login-input = Default value |
| 25 | + [html/placeholder] example@email.com |
| 26 | + [html/aria-label] Login input value |
| 27 | + [html/title] Type your login email |
| 28 | +`) |
| 29 | + |
| 30 | +// Use L20n.get to retrieve localized strings by key |
| 31 | +<div> |
| 32 | + <h1>Product name: { L20n.get('product-name') }</h1> |
| 33 | + <h2>Message count: { L20n.get('messages', { count: 2 }) }</h2> |
| 34 | +</div> |
| 35 | + |
| 36 | +// Or use the L20nElement React component to render HTML elements |
| 37 | +<div> |
| 38 | + <L20nElement id="login-input" renderAs="input" /> |
| 39 | +</div> |
| 40 | +``` |
| 41 | + |
| 42 | +## Universal (isomorphic) support |
| 43 | + |
| 44 | +```js |
| 45 | +// To load (require) ftl files with webpack use [raw-loader](https://github.com/webpack/raw-loader) |
| 46 | +loaders: [ |
| 47 | + { |
| 48 | + test: /\.ftl?$/, |
| 49 | + loader: 'raw-loader', |
| 50 | + exclude: /node_modules/ |
| 51 | + } |
| 52 | +] |
| 53 | + |
| 54 | +// To load (require) ftl files in node add the ftl extension |
| 55 | +require.extensions['.ftl'] = function(module, filename) { |
| 56 | + var content = fs.readFileSync(filename, 'utf8'); |
| 57 | + module.exports = content; |
| 58 | +}; |
| 59 | +``` |
| 60 | + |
| 61 | +## Methods |
| 62 | + |
| 63 | +### load(locale, ftl) |
| 64 | +Load localized text strings from an ftl string. |
| 65 | +User require to load from an ftl file (see Universal support above). |
| 66 | + |
| 67 | +### get(key, props, locale) |
| 68 | +Get a localized text by key. |
| 69 | +The returned value is a React Fragment element. |
| 70 | +HTML tags in your text will be rendered as HTML. |
| 71 | +Optionally pass some properties or an explicit locale. |
| 72 | +If no locale is given the default will be used. |
| 73 | + |
| 74 | +### getRaw(key, props, locale) |
| 75 | +Get a localized text by key. |
| 76 | +The returned value is a string. |
| 77 | +HTML tags in your text will be rendered as text. |
| 78 | +Optionally pass some properties or an explicit locale. |
| 79 | +If no locale is given the default will be used. |
| 80 | + |
| 81 | +## Options |
| 82 | + |
| 83 | +### defaultLocale (string) |
| 84 | +The default language used to retrieve localized strings. |
| 85 | +Sample: `L20n.defaultLocale = 'nl'` |
| 86 | + |
| 87 | +### fallbackToDefault (bool) |
| 88 | +Determines whether to use the default locale if the requested locale could not be found |
89 | 89 | Sample: `L20n.fallbackToDefault = true` |
0 commit comments