Skip to content

Commit 977ef8c

Browse files
author
Dave MacFarlane
committed
[Multilingual] Simplify language imports
The importing of a named variable for every language at the top of multilingual files, just to pass it to addResourceBundle is getting a little unwieldy. Replace them with an inline require() at the addResourceBundle call site so that we don't need to pollute the import namespace for every language (and to make life easier if dialects are added such as en-CA and en-US).
1 parent 21a1d0d commit 977ef8c

File tree

8 files changed

+23
-32
lines changed

8 files changed

+23
-32
lines changed

jsx/I18nSetup.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import i18n from 'i18next';
22
import {initReactI18next} from 'react-i18next';
33

4-
import jaResources from '../locale/ja/LC_MESSAGES/loris.json';
5-
import hiResources from '../locale/hi/LC_MESSAGES/loris.json';
6-
import esResources from '../locale/es/LC_MESSAGES/loris.json';
7-
84
const resources = {
95
ja: {
10-
loris: jaResources,
6+
loris: require('../locale/ja/LC_MESSAGES/loris.json'),
117
},
128
hi: {
13-
loris: hiResources,
9+
loris: require('../locale/hi/LC_MESSAGES/loris.json'),
1410
},
1511
es: {
16-
loris: esResources,
12+
loris: require('../es/LC_MESSAGES/loris.json'),
1713
},
1814
};
1915

modules/candidate_list/jsx/candidateListIndex.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import OpenProfileForm from './openProfileForm';
1212
import i18n from 'I18nSetup';
1313
import {withTranslation} from 'react-i18next';
1414

15-
import jaStrings from '../locale/ja/LC_MESSAGES/candidate_list.json';
16-
import hiStrings from '../locale/hi/LC_MESSAGES/candidate_list.json';
17-
1815
/**
1916
* Candidate List
2017
*
@@ -407,8 +404,10 @@ CandidateListIndex.propTypes = {
407404

408405
window.addEventListener('load', () => {
409406
const args = QueryString.get();
410-
i18n.addResourceBundle('ja', 'candidate_list', jaStrings);
411-
i18n.addResourceBundle('hi', 'candidate_list', hiStrings);
407+
i18n.addResourceBundle(
408+
'ja', 'candidate_list', require('../locale/ja/LC_MESSAGES/candidate_list.json'));
409+
i18n.addResourceBundle('hi', 'candidate_list', require('../locale/hi/LC_MESSAGES/candidate_list.json'));
410+
412411

413412

414413
const CLIndex = withTranslation(

modules/data_release/jsx/dataReleaseIndex.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import UploadFileForm from './uploadFileForm';
1313
import AddPermissionForm from './addPermissionForm';
1414
import ManagePermissionsForm from './managePermissionsForm';
1515

16-
import hiStrings from '../locale/hi/LC_MESSAGES/data_release.json';
17-
import jaStrings from '../locale/ja/LC_MESSAGES/data_release.json';
18-
1916
/**
2017
* Data Release
2118
*
@@ -244,8 +241,10 @@ DataReleaseIndex.propTypes = {
244241
};
245242

246243
window.addEventListener('load', () => {
247-
i18n.addResourceBundle('ja', 'data_release', jaStrings);
248-
i18n.addResourceBundle('hi', 'data_release', hiStrings);
244+
i18n.addResourceBundle('ja', 'data_release', require('../locale/ja/LC_MESSAGES/data_release.json'));
245+
i18n.addResourceBundle('hi', 'data_release', require('../locale/hi/LC_MESSAGES/data_release.json'));
246+
247+
249248
const Index = withTranslation(
250249
['data_release', 'loris']
251250
)(DataReleaseIndex);

modules/datadict/jsx/dataDictIndex.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import FilterableDataTable from 'FilterableDataTable';
1010

1111
import fetchDataStream from 'jslib/fetchDataStream';
1212

13-
import hiStrings from '../locale/hi/LC_MESSAGES/datadict.json';
14-
import jaStrings from '../locale/ja/LC_MESSAGES/datadict.json';
15-
1613
/**
1714
* Data Dictionary Page.
1815
*
@@ -239,8 +236,10 @@ DataDictIndex.propTypes = {
239236
};
240237

241238
window.addEventListener('load', () => {
242-
i18n.addResourceBundle('hi', 'datadict', hiStrings);
243-
i18n.addResourceBundle('ja', 'datadict', jaStrings);
239+
i18n.addResourceBundle('hi', 'datadict', require('../locale/hi/LC_MESSAGES/datadict.json'));
240+
i18n.addResourceBundle('ja', 'datadict', require('../locale/ja/LC_MESSAGES/datadict.json'));
241+
242+
244243
const Index = withTranslation(
245244
['datadict', 'loris']
246245
)(DataDictIndex);

modules/dictionary/jsx/dataDictIndex.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import Loader from 'Loader';
99
import FilterableDataTable from 'FilterableDataTable';
1010
import swal from 'sweetalert2';
1111

12-
import hiStrings from '../locale/hi/LC_MESSAGES/dictionary.json';
13-
import jaStrings from '../locale/ja/LC_MESSAGES/dictionary.json';
14-
1512
/**
1613
* Data Dictionary Page.
1714
*
@@ -357,8 +354,9 @@ DataDictIndex.propTypes = {
357354
};
358355

359356
window.addEventListener('load', () => {
360-
i18n.addResourceBundle('ja', 'dictionary', jaStrings);
361-
i18n.addResourceBundle('hi', 'dictionary', hiStrings);
357+
i18n.addResourceBundle('ja', 'dictionary', require('../locale/ja/LC_MESSAGES/dictionary.json'));
358+
i18n.addResourceBundle('hi', 'dictionary', require('../locale/hi/LC_MESSAGES/dictionary.json'));
359+
362360
const Index = withTranslation(
363361
['dictionary', 'loris']
364362
)(DataDictIndex);

modules/statistics/jsx/WidgetIndex.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {useTranslation} from 'react-i18next';
1212
import '../css/WidgetIndex.css';
1313

1414
import {setupCharts} from './widgets/helpers/chartBuilder';
15-
import jaStrings from '../locale/ja/LC_MESSAGES/statistics.json';
1615

1716
/**
1817
* WidgetIndex - the main window.
@@ -26,7 +25,8 @@ const WidgetIndex = (props) => {
2625
const [modalChart, setModalChart] = useState(null);
2726
const {t, i18n} = useTranslation();
2827
useEffect( () => {
29-
i18n.addResourceBundle('ja', 'statistics', jaStrings);
28+
29+
i18n.addResourceBundle('ja', 'statistics', require('../locale/ja/LC_MESSAGES/statistics.json'));
3030
}, []);
3131

3232
// used by recruitment.js and studyprogression.js to display each chart.

modules/statistics/jsx/widgets/studyprogression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Panel from 'Panel';
66
import {QueryChartForm} from './helpers/queryChartForm';
77
import {setupCharts} from './helpers/chartBuilder';
88
import {useTranslation} from 'react-i18next';
9-
import jaStrings from '../../locale/ja/LC_MESSAGES/statistics.json';
109

1110
/**
1211
* StudyProgression - a widget containing statistics for study data.
@@ -21,7 +20,8 @@ const StudyProgression = (props) => {
2120
const [showFiltersRecruitment, setShowFiltersRecruitment] = useState(false);
2221
const [activeView, setActiveView] = useState(0);
2322
useEffect( () => {
24-
i18n.addResourceBundle('ja', 'statistics', jaStrings);
23+
i18n.addResourceBundle('ja', 'statistics', require('../../locale/ja/LC_MESSAGES/statistics.json'));
24+
2525

2626
// Re-set default state that depended on the translation
2727
let newdetails = {...chartDetails};

src/Middleware/Language.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function process(
7878
$loris = $request->getAttribute("loris");
7979
$lang = self::detectLocale($loris, $request);
8080
if ($lang !== null) {
81-
\setlocale(LC_MESSAGES, $lang . '.utf8');
81+
\setlocale(LC_ALL, $lang . '.utf8');
8282
return $this->next->process(
8383
$request->withAttribute("locale", $lang),
8484
$handler

0 commit comments

Comments
 (0)