Skip to content

Commit

Permalink
Merge pull request #170 from ministryofjustice/i18n
Browse files Browse the repository at this point in the history
[P4-463] Shared i18n module
  • Loading branch information
teneightfive committed Jul 30, 2019
2 parents d2f61bc + 49cd2f8 commit 95020a3
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 104 deletions.
13 changes: 7 additions & 6 deletions common/presenters/move-to-card-component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assessmentToTagList = require('./assessment-to-tag-list')
const personService = require('../services/person')
const i18n = require('../../config/i18n')
const filters = require('../../config/nunjucks/filters')

function _removeEmpty (items, keys) {
Expand All @@ -19,21 +20,21 @@ function _removeEmpty (items, keys) {
module.exports = function moveToCardComponent ({ id, reference, person }) {
const meta = [
{
label: 'Date of birth',
label: i18n.t('fields::date_of_birth.label'),
hideLabel: true,
html: person.date_of_birth
? `${filters.formatDate(
person.date_of_birth
)} (Age ${filters.calculateAge(person.date_of_birth)})`
? `${filters.formatDate(person.date_of_birth)} (${i18n.t(
'age'
)} ${filters.calculateAge(person.date_of_birth)})`
: '',
},
{
label: 'Gender',
label: i18n.t('fields::gender.label'),
hideLabel: true,
text: person.gender ? person.gender.title : '',
},
{
label: 'Ethnicity',
label: i18n.t('fields::ethnicity.label'),
hideLabel: true,
text: person.ethnicity ? person.ethnicity.title : '',
},
Expand Down
40 changes: 35 additions & 5 deletions common/presenters/move-to-card-component.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const moveToCardComponent = require('./move-to-card-component')

const i18n = require('../../config/i18n')
const filters = require('../../config/nunjucks/filters')
const personService = require('../services/person')

Expand All @@ -11,6 +12,7 @@ const fullname = `${mockMove.person.last_name}, ${mockMove.person.first_names}`
describe('Presenters', function () {
describe('#moveToCardComponent()', function () {
beforeEach(function () {
sinon.stub(i18n, 't').returns('__translated__')
sinon.stub(filters, 'formatDate').returns('18 Jun 1960')
sinon.stub(filters, 'calculateAge').returns(50)
sinon.stub(personService, 'getFullname').returns(fullname)
Expand Down Expand Up @@ -49,17 +51,17 @@ describe('Presenters', function () {
items: [
{
hideLabel: true,
label: 'Date of birth',
html: '18 Jun 1960 (Age 50)',
label: '__translated__',
html: '18 Jun 1960 (__translated__ 50)',
},
{
hideLabel: true,
label: 'Gender',
label: '__translated__',
text: mockMove.person.gender.title,
},
{
hideLabel: true,
label: 'Ethnicity',
label: '__translated__',
text: mockMove.person.ethnicity.title,
},
],
Expand Down Expand Up @@ -94,6 +96,34 @@ describe('Presenters', function () {
})
})

describe('translations', function () {
it('should translate date of birth label', function () {
expect(i18n.t.firstCall).to.be.calledWithExactly(
'fields::date_of_birth.label'
)
})

it('should translate age label', function () {
expect(i18n.t.secondCall).to.be.calledWithExactly('age')
})

it('should translate gender label', function () {
expect(i18n.t.thirdCall).to.be.calledWithExactly(
'fields::gender.label'
)
})

it('should translate ethnicity label', function () {
expect(i18n.t.getCall(3)).to.be.calledWithExactly(
'fields::ethnicity.label'
)
})

it('should translate correct number of times', function () {
expect(i18n.t).to.be.callCount(4)
})
})

context('when meta contains all falsey values', function () {
let transformedResponse

Expand Down Expand Up @@ -139,7 +169,7 @@ describe('Presenters', function () {
items: [
{
hideLabel: true,
label: 'Gender',
label: '__translated__',
text: mockMove.person.gender.title,
},
],
Expand Down
28 changes: 17 additions & 11 deletions common/presenters/move-to-meta-list-component.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-disable camelcase */
const {
isToday,
isTomorrow,
isYesterday,
} = require('date-fns')
const { isToday, isTomorrow, isYesterday } = require('date-fns')

const i18n = require('../../config/i18n')
const filters = require('../../config/nunjucks/filters')

function _isRelativeDate (date) {
Expand All @@ -15,35 +12,44 @@ function _isRelativeDate (date) {
return false
}

module.exports = function moveToMetaListComponent ({ date, time_due, from_location, to_location }) {
module.exports = function moveToMetaListComponent ({
date,
time_due,
from_location,
to_location,
}) {
const items = [
{
key: {
text: 'From',
text: i18n.t('fields::from_location.short_label'),
},
value: {
text: from_location.title,
},
},
{
key: {
text: 'To',
text: i18n.t('fields::to_location_type.short_label'),
},
value: {
text: to_location.title,
},
},
{
key: {
text: 'Date',
text: i18n.t('fields::date_type.label'),
},
value: {
text: _isRelativeDate(date) ? `${filters.formatDateWithDay(date)} (${filters.formatDateAsRelativeDay(date)})` : filters.formatDateWithDay(date),
text: _isRelativeDate(date)
? `${filters.formatDateWithDay(
date
)} (${filters.formatDateAsRelativeDay(date)})`
: filters.formatDateWithDay(date),
},
},
{
key: {
text: 'Time due',
text: i18n.t('fields::time_due.label'),
},
value: {
text: filters.formatTime(time_due),
Expand Down
65 changes: 54 additions & 11 deletions common/presenters/move-to-meta-list-component.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const { subDays, addDays } = require('date-fns')

const moveToMetaListComponent = require('./move-to-meta-list-component')
const i18n = require('../../config/i18n')

const { data: mockMove } = require('../../test/fixtures/api-client/move.get.deserialized.json')
const {
data: mockMove,
} = require('../../test/fixtures/api-client/move.get.deserialized.json')

describe('Presenters', function () {
describe('#moveToMetaListComponent()', function () {
beforeEach(function () {
sinon.stub(i18n, 't').returns('__translated__')
})

context('when provided with a mock move object', function () {
let transformedResponse

beforeEach(function () {
this.clock = sinon.useFakeTimers(subDays(new Date(mockMove.date), 3).getTime())
this.clock = sinon.useFakeTimers(
subDays(new Date(mockMove.date), 3).getTime()
)

transformedResponse = moveToMetaListComponent(mockMove)
})
Expand All @@ -29,7 +38,7 @@ describe('Presenters', function () {
const item = transformedResponse.items[0]

expect(item).to.deep.equal({
key: { text: 'From' },
key: { text: '__translated__' },
value: { text: mockMove.from_location.title },
})
})
Expand All @@ -38,7 +47,7 @@ describe('Presenters', function () {
const item = transformedResponse.items[1]

expect(item).to.deep.equal({
key: { text: 'To' },
key: { text: '__translated__' },
value: { text: mockMove.to_location.title },
})
})
Expand All @@ -47,7 +56,7 @@ describe('Presenters', function () {
const item = transformedResponse.items[2]

expect(item).to.deep.equal({
key: { text: 'Date' },
key: { text: '__translated__' },
value: { text: 'Sunday 9 Jun 2019' },
})
})
Expand All @@ -56,11 +65,41 @@ describe('Presenters', function () {
const item = transformedResponse.items[3]

expect(item).to.deep.equal({
key: { text: 'Time due' },
key: { text: '__translated__' },
value: { text: '2pm' },
})
})
})

describe('translations', function () {
it('should translate from location label', function () {
expect(i18n.t.firstCall).to.be.calledWithExactly(
'fields::from_location.short_label'
)
})

it('should translate to location label', function () {
expect(i18n.t.secondCall).to.be.calledWithExactly(
'fields::to_location_type.short_label'
)
})

it('should translate date label', function () {
expect(i18n.t.thirdCall).to.be.calledWithExactly(
'fields::date_type.label'
)
})

it('should translate time due label', function () {
expect(i18n.t.getCall(3)).to.be.calledWithExactly(
'fields::time_due.label'
)
})

it('should translate correct number of times', function () {
expect(i18n.t).to.be.callCount(4)
})
})
})

context('when date is today', function () {
Expand All @@ -81,7 +120,7 @@ describe('Presenters', function () {
const item = transformedResponse.items[2]

expect(item).to.deep.equal({
key: { text: 'Date' },
key: { text: '__translated__' },
value: { text: 'Sunday 9 Jun 2019 (Today)' },
})
})
Expand All @@ -92,7 +131,9 @@ describe('Presenters', function () {
let transformedResponse

beforeEach(function () {
this.clock = sinon.useFakeTimers(subDays(new Date(mockMove.date), 1).getTime())
this.clock = sinon.useFakeTimers(
subDays(new Date(mockMove.date), 1).getTime()
)

transformedResponse = moveToMetaListComponent(mockMove)
})
Expand All @@ -106,7 +147,7 @@ describe('Presenters', function () {
const item = transformedResponse.items[2]

expect(item).to.deep.equal({
key: { text: 'Date' },
key: { text: '__translated__' },
value: { text: 'Sunday 9 Jun 2019 (Tomorrow)' },
})
})
Expand All @@ -117,7 +158,9 @@ describe('Presenters', function () {
let transformedResponse

beforeEach(function () {
this.clock = sinon.useFakeTimers(addDays(new Date(mockMove.date), 1).getTime())
this.clock = sinon.useFakeTimers(
addDays(new Date(mockMove.date), 1).getTime()
)

transformedResponse = moveToMetaListComponent(mockMove)
})
Expand All @@ -131,7 +174,7 @@ describe('Presenters', function () {
const item = transformedResponse.items[2]

expect(item).to.deep.equal({
key: { text: 'Date' },
key: { text: '__translated__' },
value: { text: 'Sunday 9 Jun 2019 (Yesterday)' },
})
})
Expand Down
Loading

0 comments on commit 95020a3

Please sign in to comment.