Skip to content

Commit

Permalink
Add date translation
Browse files Browse the repository at this point in the history
The weekly/trends chart have also been updated from "MMM D" to "D MMM" in French
  • Loading branch information
coyotte508 committed Mar 23, 2018
1 parent 1365e12 commit 7fc2bd6
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"plugins": ["rewire"]
}
},
"plugins": [
"transform-decorators-legacy"
],
"presets": [
"es2015",
"stage-0",
Expand Down
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"jsx": true,
"classes": true,
"modules": true,
"experimentalDecorators": true
},
"env": {
// I write for browser
Expand Down Expand Up @@ -43,5 +44,5 @@
},
"plugins": [
"react"
]
],
}
6 changes: 4 additions & 2 deletions app/components/chart/trends.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import bows from 'bows';
import React, { PropTypes, PureComponent } from 'react';
import ReactDOM from 'react-dom';
import sundial from 'sundial';
import { translate } from 'react-i18next';

import Header from './header';
import SubNav from './trendssubnav';
Expand All @@ -35,6 +36,7 @@ const TrendsContainer = viz.containers.TrendsContainer;
const reshapeBgClassesToBgBounds = viz.utils.reshapeBgClassesToBgBounds;
const Loader = viz.components.Loader;

@translate()
class Trends extends PureComponent {
static propTypes = {
bgPrefs: PropTypes.object.isRequired,
Expand Down Expand Up @@ -105,15 +107,15 @@ class Trends extends PureComponent {
}

formatDate(datetime) {
const timePrefs = this.props.timePrefs
const { timePrefs, t } = this.props;
let timezone;
if (!timePrefs.timezoneAware) {
timezone = 'UTC';
}
else {
timezone = timePrefs.timezoneName || 'UTC';
}
return sundial.formatInTimezone(datetime, timezone, 'MMM D, YYYY');
return sundial.formatInTimezone(datetime, timezone, t('MMM D, YYYY'));
}

getNewDomain(current, extent) {
Expand Down
8 changes: 5 additions & 3 deletions app/components/chart/weekly.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var bows = require('bows');
var React = require('react');
var ReactDOM = require('react-dom');
var sundial = require('sundial');
import { translate } from 'react-i18next';

// tideline dependencies & plugins
var tidelineBlip = require('tideline/plugins/blip');
Expand Down Expand Up @@ -150,7 +151,7 @@ var WeeklyChart = React.createClass({
}
});

var Weekly = React.createClass({
var Weekly = translate()(React.createClass({
chartType: 'weekly',
log: bows('Weekly View'),
propTypes: {
Expand Down Expand Up @@ -297,8 +298,9 @@ var Weekly = React.createClass({
},

formatDate: function(datetime) {
const { t } = this.props;
// even when timezoneAware, labels should be generated as if UTC; just trust me (JEB)
return sundial.formatInTimezone(datetime, 'UTC', 'MMM D, YYYY');
return sundial.formatInTimezone(datetime, 'UTC', t('MMM D, YYYY'));
},

getTitle: function(datetimeLocationEndpoints) {
Expand Down Expand Up @@ -411,6 +413,6 @@ var Weekly = React.createClass({
}
this.setState({showingValues: !this.state.showingValues});
}
});
}));

module.exports = Weekly;
6 changes: 2 additions & 4 deletions app/components/navbar/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var NavbarPatientCard = require('../../components/navbarpatientcard');
var logoSrc = require('./images/tidepool-logo-408x46.png');

// The pure component needs to be exported for tests
export const PureNavbar = React.createClass({
export default translate('translation', {withRef: true})(React.createClass({
propTypes: {
currentPage: React.PropTypes.string,
user: React.PropTypes.object,
Expand Down Expand Up @@ -227,6 +227,4 @@ export const PureNavbar = React.createClass({
logout();
}
}
});

export default translate('translation', {withRef: true})(PureNavbar);
}));
4 changes: 4 additions & 0 deletions app/core/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import i18n from 'i18next';
import { reactI18nextModule } from 'react-i18next';
import getLocale from 'browser-locale';
import moment from 'moment';

// Update moment with the right language, for date display
i18n.on('languageChanged', lng => moment.locale(lng));

i18n
.use(reactI18nextModule)
Expand Down
1 change: 1 addition & 0 deletions locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Logging in...": "",
"Login": "",
"Logout": "",
"MMM D, YYYY": "",
"Password": "",
"Remember me": "",
"Sign up": ""
Expand Down
1 change: 1 addition & 0 deletions locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Logging in...": "Connexion...",
"Login": "Connexion",
"Logout": "Déconnexion",
"MMM D, YYYY": "D MMM YYYY",
"Password": "Mot de passe",
"Remember me": "Se souvenir de moi",
"Sign up": "Inscription"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"devDependencies": {
"babel-eslint": "6.1.2",
"babel-plugin-rewire": "1.0.0-rc-6",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"chai": "3.5.0",
"chromedriver": "2.23.1",
"eslint": "2.13.1",
Expand Down
15 changes: 11 additions & 4 deletions test/unit/app/core/language.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import i18n from '../../../../app/core/language';

/* global chai */
/* global describe */
/* global it */
/* global chai, describe, before, after, it */

const expect = chai.expect;

describe('languages', () => {
before(() => {
i18n.changeLanguage('en');
});

it('should be able to change language', () => {
expect(i18n.t('Log in')).to.equal('Log in');

i18n.changeLanguage('fr');
expect(i18n.t('Log in')).to.equal('Connexion');
})
});

after(() => {
// Avoid affecting other tests
i18n.changeLanguage('en');
});
});
6 changes: 4 additions & 2 deletions test/unit/components/chart/trends.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Trends from '../../../../app/components/chart/trends';
import { shallow } from 'enzyme';
import { MGDL_UNITS } from '../../../../app/core/constants';
import { components as vizComponents } from '@tidepool/viz';
import i18next from '../../../../app/core/language';

const { Loader } = vizComponents;

Expand Down Expand Up @@ -88,14 +89,15 @@ describe('Trends', () => {
trendsState: {
'1234': {},
},
t: i18next.t.bind(i18next),
loading: false,
onUpdateChartDateRange: sinon.stub(),
updateDatetimeLocation: sinon.stub(),
};

let wrapper;
beforeEach(() => {
wrapper = shallow(<Trends {...baseProps} />);
wrapper = shallow(<Trends.WrappedComponent {...baseProps} />);
})

afterEach(() => {
Expand All @@ -120,7 +122,7 @@ describe('Trends', () => {
let instance;

beforeEach(() => {
wrapper = shallow(<Trends {...baseProps} />);
wrapper = shallow(<Trends.WrappedComponent {...baseProps} />);
instance = wrapper.instance();
});

Expand Down
6 changes: 4 additions & 2 deletions test/unit/components/chart/weekly.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Weekly from '../../../../app/components/chart/weekly';
import { shallow } from 'enzyme';
import { MGDL_UNITS } from '../../../../app/core/constants';
import { components as vizComponents } from '@tidepool/viz';
import i18next from '../../../../app/core/language';

const { Loader } = vizComponents;

Expand Down Expand Up @@ -77,11 +78,12 @@ describe('Weekly', () => {
loading: false,
onUpdateChartDateRange: sinon.stub(),
updateDatetimeLocation: sinon.stub(),
t: i18next.t.bind(i18next)
};

let wrapper;
beforeEach(() => {
wrapper = shallow(<Weekly {...baseProps} />);
wrapper = shallow(<Weekly.WrappedComponent {...baseProps} />);
})

afterEach(() => {
Expand Down Expand Up @@ -110,7 +112,7 @@ describe('Weekly', () => {
};

beforeEach(() => {
wrapper = shallow(<Weekly {...baseProps} />);
wrapper = shallow(<Weekly.WrappedComponent {...baseProps} />);
instance = wrapper.instance();
});

Expand Down
10 changes: 5 additions & 5 deletions test/unit/components/navbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import React from 'react';
import { shallow } from 'enzyme';

import '../../../app/core/language';
// The HOC makes it difficult to access / set properties of the pure component,
// in this case the trackMetric property of PureNavbar. So we test
// on the pure component instead.
import Navbar, {PureNavbar} from '../../../app/components/navbar';
import Navbar from '../../../app/components/navbar';

const expect = chai.expect;

Expand All @@ -25,7 +22,10 @@ describe('Navbar', () => {
</div>
);
});
wrapper = shallow(<PureNavbar {...props} />);
// The HOC makes it difficult to access / set properties of the pure component,
// in this case the trackMetric property of PureNavbar. So we test
// on the pure component instead.
wrapper = shallow(<Navbar.WrappedComponent {...props} />);
});

after(() => {
Expand Down
Loading

0 comments on commit 7fc2bd6

Please sign in to comment.