Skip to content

Commit 4044de1

Browse files
author
ryanjohnson
committed
update translate function to return plain string when translation string does not contain HTML
1 parent 7c276b3 commit 4044de1

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ A collection of helpers for managing localized content in your React/Redux appli
1414
- [Getting Started](#getting-started)
1515
- [Features](#features)
1616
- [API](#api)
17+
- [Dead simple localization for your React components](https://medium.com/@ryandrewjohnson/adding-multi-language-support-to-your-react-redux-app-cf6e64250050)
1718
- [Migrating from v1 to v2?](MIGRATING.md)
1819

1920
## Installation
@@ -248,7 +249,15 @@ Also If you are using a tool like [webpack](https://webpack.js.org) for bundling
248249

249250
A selector that takes the localeReducer slice of your `state` and returns the translate function. This function will have access to any and all translations that were added with [addTranslation](#addtranslationdata).
250251

251-
returns `(key | key[], data) => LocalizedElement | { [key: string]: LocalizedElement }`
252+
For single translation:
253+
254+
returns `(key: string) => LocalizedElement | string`
255+
256+
For multiple translations:
257+
258+
returns `(key: []string) => { [key: string]: LocalizedElement | string`
259+
260+
> Note: If a translation contains HTML a LocalizedElement will be used to render. In all other cases a string will be used.
252261
253262
* `key: string|array` = A translation key or an array of translation keys.
254263
* `data: object` = Pass data to your [dynamic translation](#add-dynamic-content-to-translations) string.

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const getLocalizedElement = (key, translations, data) => {
55
const translatedValue = templater(localizedString, data)
66
return hasHtmlTags(translatedValue)
77
? React.createElement('span', { dangerouslySetInnerHTML: { __html: translatedValue }})
8-
: <span>{ translatedValue }</span>;
8+
: translatedValue;
99
};
1010

1111
export const hasHtmlTags = (value) => {
@@ -28,7 +28,7 @@ export const templater = (strings, data = {}) => {
2828
}
2929
return strings;
3030
}
31-
31+
``
3232
export const getIndexForLanguageCode = (code, languages) => {
3333
return languages.map(language => language.code).indexOf(code);
3434
}

tests/utils.test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ describe('locale utils', () => {
66
describe('getLocalizedElement', () => {
77
it('should return element with localized string', () => {
88
const translations = { test: 'Here is my test' };
9-
const result = shallow(utils.getLocalizedElement('test', translations));
10-
expect(result.find('span').exists()).toBe(true);
11-
expect(result.find('span').html()).toEqual(`<span>${translations.test}</span>`);
9+
const result = utils.getLocalizedElement('test', translations);
10+
expect(result).toBe(translations.test);
1211
});
1312

1413
it('should return element with HTML from translation rendered', () => {
@@ -22,9 +21,14 @@ describe('locale utils', () => {
2221
it('should return element with warning when no localized string found', () => {
2322
const translations = { test: 'Here is my test' };
2423
const key = 'test2';
25-
const result = shallow(utils.getLocalizedElement(key, translations));
26-
expect(result.find('span').exists()).toBe(true);
27-
expect(result.find('span').html()).toEqual(`<span>Missing locaized: ${key}</span>`);
24+
const result = utils.getLocalizedElement(key, translations);
25+
expect(result).toEqual(`Missing locaized: ${key}`);
26+
});
27+
28+
it('should replace variables in translation string with data', () => {
29+
const translations = { test: 'Hello ${ name }' };
30+
const result = utils.getLocalizedElement('test', translations, { name: 'Ted' });
31+
expect(result).toEqual('Hello Ted');
2832
});
2933
});
3034

0 commit comments

Comments
 (0)