Fix issue with locales

This commit is contained in:
Gabriel Carvalho de Campes 2017-10-27 09:36:27 -02:00
parent 590e6ecec0
commit 00bf09c4f9
3 changed files with 22 additions and 20 deletions

View File

@ -20,13 +20,12 @@ class IntlStartup extends Component {
this.state = {
messages: {},
appLocale: this.props.locale,
};
this.fetchLocalizedMessages = this.fetchLocalizedMessages.bind(this);
}
componentWillMount() {
this.fetchLocalizedMessages(this.state.appLocale);
this.fetchLocalizedMessages(this.props.locale);
}
componentWillUpdate(nextProps) {
@ -39,34 +38,30 @@ class IntlStartup extends Component {
const url = `/html5client/locale?locale=${locale}`;
const { baseControls } = this.props;
this.setState({ appLocale: locale });
baseControls.updateLoadingState(true);
fetch(url)
.then((response) => {
if (response.ok) {
return response.json();
if (!response.ok) {
return Promise.reject();
}
this.setState({ appLocale: 'en' });
return response.json();
})
.then((messages) => {
if (messages.statusCode === 506) {
this.setState({ appLocale: 'en' });
}
this.setState({ messages: messages.messages }, () => {
.then(({ messages, normalizedLocale }) => {
this.setState({ messages, locale: normalizedLocale.replace('_', '-') }, () => {
baseControls.updateLoadingState(false);
});
})
.catch((reason) => {
baseControls.updateErrorState(reason);
this.setState({ locale: 'en' });
baseControls.updateLoadingState(false);
});
}
render() {
return (
<IntlProvider locale={this.state.appLocale} messages={this.state.messages}>
<IntlProvider locale={this.state.locale} messages={this.state.messages}>
{this.props.children}
</IntlProvider>
);

View File

@ -21,15 +21,18 @@ WebApp.connectHandlers.use('/check', (req, res) => {
WebApp.connectHandlers.use('/locale', (req, res) => {
const APP_CONFIG = Meteor.settings.public.app;
const defaultLocale = APP_CONFIG.defaultSettings.application.locale;
const localeRegion = req.query.locale.split('-');
const localeRegion = req.query.locale.split(/[-_]/g);
const localeList = [defaultLocale, localeRegion[0]];
let normalizedLocale = localeRegion[0];
let messages = {};
const completeLocale = [defaultLocale, localeRegion[0]];
let statusCode = 200;
if (localeRegion.length > 1) {
locales.push(`${localeRegion[0]}_${localeRegion[1].toUpperCase()}`);
normalizedLocale = `${localeRegion[0]}_${localeRegion[1].toUpperCase()}`;
localeList.push(normalizedLocale);
}
completeLocale.forEach((locale) => {
localeList.forEach((locale) => {
try {
const data = Assets.getText(`locales/${locale}.json`);
messages = Object.assign(messages, JSON.parse(data));
@ -37,12 +40,11 @@ WebApp.connectHandlers.use('/locale', (req, res) => {
// Variant Also Negotiates Status-Code, to alert the client that we
// do not support the following lang.
// https://en.wikipedia.org/wiki/Content_negotiation
statusCode = 506;
}
});
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ statusCode, messages }));
res.end(JSON.stringify({ normalizedLocale, messages }));
});
WebApp.connectHandlers.use('/locales', (req, res) => {

View File

@ -30,6 +30,11 @@ class Settings {
},
});
});
// Sets default locale to browser locale
defaultValues.application.locale = navigator.language ||
defaultValues.application.locale;
this.setDefault(defaultValues);
}