forked from parisjs/parisjs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intl.js
52 lines (44 loc) · 1.22 KB
/
intl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React from 'react'
import { Link } from 'react-router'
import { addLocaleData, FormattedMessage } from 'react-intl'
import en from 'react-intl/locale-data/en'
import pt from 'react-intl/locale-data/pt'
import messages from './lang'
addLocaleData([...en, ...pt])
export const defaultLocale = 'pt'
export const locales = ['pt', 'en']
export function getLocale(url) {
const firstURIlevel = url.replace(/^\//, '').split('/')[0]
return firstURIlevel && locales.indexOf(firstURIlevel) > -1
? firstURIlevel
: defaultLocale
}
export function getIntl(locale) {
return {
locale: locale,
key: locale,
messages: messages[locale],
defaultLocale: defaultLocale
}
}
export function IntlLink({ to, title, activeClassName }) {
return (
<FormattedMessage id={to}>
{txt => (
<Link to={txt} activeClassName={activeClassName}>
<FormattedMessage id={title} />
</Link>
)}
</FormattedMessage>
)
}
export function LocalLink({ to, children, ...otherProps }) {
const locale = getLocale(
typeof location !== 'undefined' ? location.pathname : '/'
)
return (
<Link to={`${locale !== defaultLocale ? locale : ''}${to}`} {...otherProps}>
{children}
</Link>
)
}