Merge pull request #3163 from overleaf/as-jpa-i18n-cleanup

[misc] Translations cleanup

GitOrigin-RevId: 46bf1142bb9415eeebf638c120597996aaa55f8b
This commit is contained in:
Jakob Ackermann
2020-09-18 02:04:41 +00:00
committed by Copybot
parent 1b72640b5c
commit 435af75ef7
11 changed files with 142 additions and 3711 deletions
@@ -4,28 +4,36 @@ const SandboxedModule = require('sandboxed-module')
const MODULE_PATH = '../../../../app/src/infrastructure/Translations.js'
describe('Translations', function() {
let req, res, translations
function runMiddlewares(cb) {
translations.i18nMiddleware(req, res, () => {
translations.setLangBasedOnDomainMiddleware(req, res, cb)
})
}
beforeEach(function() {
this.translations = SandboxedModule.require(MODULE_PATH, {
translations = SandboxedModule.require(MODULE_PATH, {
requires: {
'settings-sharelatex': {
i18n: {
subdomainLang: {
www: { lngCode: 'en', url: 'www.sharelatex.com' },
fr: { lngCode: 'fr', url: 'fr.sharelatex.com' },
da: { lngCode: 'da', url: 'da.sharelatex.com' }
www: { lngCode: 'en', url: 'https://www.sharelatex.com' },
fr: { lngCode: 'fr', url: 'https://fr.sharelatex.com' },
da: { lngCode: 'da', url: 'https://da.sharelatex.com' }
}
}
}
}
})
this.req = {
originalUrl: "doesn'tmatter.sharelatex.com/login",
req = {
url: '/',
headers: {
'accept-language': ''
}
}
this.res = {
res = {
locals: {},
getHeader: () => {},
setHeader: () => {}
}
@@ -33,28 +41,26 @@ describe('Translations', function() {
describe('translate', function() {
beforeEach(function(done) {
this.req.url = 'www.sharelatex.com/login'
this.translations.expressMiddleware(this.req, this.res, done)
runMiddlewares(done)
})
it('works', function() {
expect(this.req.i18n.t('give_feedback')).to.equal('Give feedback')
expect(req.i18n.t('give_feedback')).to.equal('Give feedback')
})
it('has translate alias', function() {
expect(this.req.i18n.translate('give_feedback')).to.equal('Give feedback')
expect(req.i18n.translate('give_feedback')).to.equal('Give feedback')
})
})
describe('interpolation', function() {
beforeEach(function(done) {
this.req.url = 'www.sharelatex.com/login'
this.translations.expressMiddleware(this.req, this.res, done)
runMiddlewares(done)
})
it('works', function() {
expect(
this.req.i18n.t('please_confirm_email', {
req.i18n.t('please_confirm_email', {
emailAddress: 'foo@example.com'
})
).to.equal(
@@ -66,7 +72,7 @@ describe('Translations', function() {
// This translation string has a problematic interpolation followed by a
// dash: `__len__-day`
expect(
this.req.i18n.t('faq_how_does_free_trial_works_answer', {
req.i18n.t('faq_how_does_free_trial_works_answer', {
appName: 'Overleaf',
len: '5'
})
@@ -77,7 +83,7 @@ describe('Translations', function() {
it('disables escaping', function() {
expect(
this.req.i18n.t('admin_user_created_message', {
req.i18n.t('admin_user_created_message', {
link: 'http://google.com'
})
).to.equal(
@@ -86,88 +92,34 @@ describe('Translations', function() {
})
})
describe('query string detection', function() {
it('sets the language to french if the setLng query string is fr', function(done) {
this.req.originalUrl = 'www.sharelatex.com/login?setLng=fr'
this.req.url = 'www.sharelatex.com/login'
this.req.query = { setLng: 'fr' }
this.req.headers.host = 'www.sharelatex.com'
this.translations.expressMiddleware(this.req, this.res, () => {
this.translations.setLangBasedOnDomainMiddleware(
this.req,
this.res,
() => {
expect(this.req.lng).to.equal('fr')
done()
}
)
})
})
})
describe('setLangBasedOnDomainMiddleware', function() {
it('should set the lang to french if the domain is fr', function(done) {
this.req.url = 'fr.sharelatex.com/login'
this.req.headers.host = 'fr.sharelatex.com'
this.translations.expressMiddleware(this.req, this.res, () => {
this.translations.setLangBasedOnDomainMiddleware(
this.req,
this.res,
() => {
expect(this.req.lng).to.equal('fr')
done()
}
)
req.headers.host = 'fr.sharelatex.com'
runMiddlewares(() => {
expect(req.lng).to.equal('fr')
done()
})
})
it('ignores domain if setLng query param is set', function(done) {
this.req.originalUrl = 'fr.sharelatex.com/login?setLng=en'
this.req.url = 'fr.sharelatex.com/login'
this.req.query = { setLng: 'en' }
this.req.headers.host = 'fr.sharelatex.com'
this.translations.expressMiddleware(this.req, this.res, () => {
this.translations.setLangBasedOnDomainMiddleware(
this.req,
this.res,
() => {
expect(this.req.lng).to.equal('en')
done()
}
)
})
})
describe('showUserOtherLng', function() {
it('should set showUserOtherLng=true if the detected lang is different to subdomain lang', function(done) {
this.req.headers['accept-language'] = 'da, en-gb;q=0.8, en;q=0.7'
this.req.url = 'fr.sharelatex.com/login'
this.req.headers.host = 'fr.sharelatex.com'
this.translations.expressMiddleware(this.req, this.res, () => {
this.translations.setLangBasedOnDomainMiddleware(
this.req,
this.res,
() => {
expect(this.req.showUserOtherLng).to.equal('da')
done()
}
describe('suggestedLanguageSubdomainConfig', function() {
it('should set suggestedLanguageSubdomainConfig if the detected lang is different to subdomain lang', function(done) {
req.headers['accept-language'] = 'da, en-gb;q=0.8, en;q=0.7'
req.headers.host = 'fr.sharelatex.com'
runMiddlewares(() => {
expect(res.locals.suggestedLanguageSubdomainConfig).to.exist
expect(res.locals.suggestedLanguageSubdomainConfig.lngCode).to.equal(
'da'
)
done()
})
})
it('should not set showUserOtherLng if the detected lang is the same as subdomain lang', function(done) {
this.req.headers['accept-language'] = 'da, en-gb;q=0.8, en;q=0.7'
this.req.url = 'da.sharelatex.com/login'
this.req.headers.host = 'da.sharelatex.com'
this.translations.expressMiddleware(this.req, this.res, () => {
this.translations.setLangBasedOnDomainMiddleware(
this.req,
this.res,
() => {
expect(this.req.showUserOtherLng).to.not.exist
done()
}
)
it('should not set suggestedLanguageSubdomainConfig if the detected lang is the same as subdomain lang', function(done) {
req.headers['accept-language'] = 'da, en-gb;q=0.8, en;q=0.7'
req.headers.host = 'da.sharelatex.com'
runMiddlewares(() => {
expect(res.locals.suggestedLanguageSubdomainConfig).to.not.exist
done()
})
})
})