Skip to content

Commit dd54160

Browse files
authored
Merge pull request #5 from FriendsOfECMAScript/feature/router-components
Added missing React Router components
2 parents d15a6bb + 2c3ab50 commit dd54160

File tree

5 files changed

+76
-27
lines changed

5 files changed

+76
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
This changelog references the relevant changes done between versions.
44

55
To get the diff for a specific change, go to https://github.com/FriendsOfECMAScript/ReactI18nRouting/commit/XXX where XXX is the change hash
6-
To get the diff between two versions, go to https://github.com/FriendsOfECMAScript/ReactI18nRouting/compare/v0.2.0...v0.3.0
6+
To get the diff between two versions, go to https://github.com/FriendsOfECMAScript/ReactI18nRouting/compare/v0.3.0...v0.4.0
77

8+
* 0.4.0
9+
* Added `FormattedRedirect` and `FormattedNavLink` components.
810
* 0.3.0
911
* Added License, Travis and other useful files to make the library more compliant with OSS.
1012
* Decoupled `BrowserIntlProvider` from Redux, allowing other implementations like `LocalStorage` or `InMemory`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@foes/react-i18n-routing",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"license": "MIT",
55
"description": "Provides components and functions to make i18n routing React apps with ease.",
66
"keywords": [

src/component/FormattedLink.js

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React from 'react';
2+
import {Link, NavLink, Redirect} from 'react-router-dom';
3+
import {injectIntl} from 'react-intl';
4+
5+
const FormattedRouter = ({intl, children}) => {
6+
if (!intl.formats.formatIntlRoute) {
7+
throw new Error(
8+
'LanguageStrategy formatter is missing, pass formatIntlRoute ' +
9+
'as parameter to IntlProvider or to BrowserIntlProvider',
10+
);
11+
}
12+
13+
return children;
14+
};
15+
16+
export const FormattedRedirect = injectIntl(
17+
({name, params, intl, locale, ...rest}) => {
18+
return (
19+
<FormattedRouter intl={intl}>
20+
<Redirect
21+
to={intl.formats.formatIntlRoute(
22+
name,
23+
params,
24+
locale ? locale : intl.locale,
25+
)}
26+
{...rest}
27+
/>
28+
</FormattedRouter>
29+
);
30+
},
31+
);
32+
33+
export const FormattedNavLink = injectIntl(
34+
({name, params, intl, locale, ...rest}) => {
35+
return (
36+
<FormattedRouter intl={intl}>
37+
<NavLink
38+
to={intl.formats.formatIntlRoute(
39+
name,
40+
params,
41+
locale ? locale : intl.locale,
42+
)}
43+
{...rest}
44+
/>
45+
</FormattedRouter>
46+
);
47+
},
48+
);
49+
50+
export const FormattedLink = injectIntl(
51+
({name, params, intl, locale, ...rest}) => {
52+
return (
53+
<FormattedRouter intl={intl}>
54+
<Link
55+
to={intl.formats.formatIntlRoute(
56+
name,
57+
params,
58+
locale ? locale : intl.locale,
59+
)}
60+
{...rest}
61+
/>
62+
</FormattedRouter>
63+
);
64+
},
65+
);

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import ReduxBrowserIntlProvider, {
55
i18nReducer,
66
LANGUAGE_CHANGE,
77
} from './component/bridges/ReduxBrowserIntlProvider.js';
8-
import FormattedLink from './component/FormattedLink.js';
8+
import {
9+
FormattedLink,
10+
FormattedNavLink,
11+
FormattedRedirect,
12+
} from './component/FormattedReactRouter.js';
913
import defaultUnPrefixed from './languageStrategy/defaultUnprefixed.js';
1014
import subdomainBased from './languageStrategy/subdomainBased.js';
1115
import renderTranslatedRoutes from './renderTranslatedRoutes.js';
@@ -19,6 +23,8 @@ export {
1923
i18nReducer,
2024
LANGUAGE_CHANGE,
2125
FormattedLink,
26+
FormattedNavLink,
27+
FormattedRedirect,
2228
defaultUnPrefixed,
2329
subdomainBased,
2430
renderTranslatedRoutes,

0 commit comments

Comments
 (0)