Skip to content

Commit e0f8d5f

Browse files
committed
Added null check
1 parent ed442f6 commit e0f8d5f

File tree

5 files changed

+127
-125
lines changed

5 files changed

+127
-125
lines changed

README.md

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,89 @@
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
8989
Sample: `L20n.fallbackToDefault = true`

build/react-l20n.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ function () {
122122
value: function get(key, props) {
123123
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.defaultLocale;
124124
var message = this.getRaw(key, props, locale);
125+
if (typeof message != 'string') return undefined;
125126
return _react.default.createElement(_react.default.Fragment, null, (0, _htmlReactParser.default)(message));
126127
}
127128
}, {

0 commit comments

Comments
 (0)