2020-08-18 06:09:28 -04:00
const { expect } = require ( 'chai' )
const SandboxedModule = require ( 'sandboxed-module' )
const MODULE _PATH = '../../../../app/src/infrastructure/Translations.js'
2021-04-14 09:17:21 -04:00
describe ( 'Translations' , function ( ) {
2020-09-17 04:30:08 -04:00
let req , res , translations
function runMiddlewares ( cb ) {
translations . i18nMiddleware ( req , res , ( ) => {
translations . setLangBasedOnDomainMiddleware ( req , res , cb )
} )
}
2021-04-14 09:17:21 -04:00
beforeEach ( function ( ) {
2020-09-17 04:30:08 -04:00
translations = SandboxedModule . require ( MODULE _PATH , {
2020-08-18 06:09:28 -04:00
requires : {
'settings-sharelatex' : {
i18n : {
2020-11-05 05:07:11 -05:00
escapeHTMLInVars : false ,
2020-08-18 06:09:28 -04:00
subdomainLang : {
2020-09-17 04:30:08 -04:00
www : { lngCode : 'en' , url : 'https://www.sharelatex.com' } ,
fr : { lngCode : 'fr' , url : 'https://fr.sharelatex.com' } ,
2021-04-27 03:52:58 -04:00
da : { lngCode : 'da' , url : 'https://da.sharelatex.com' } ,
} ,
} ,
} ,
} ,
2020-08-18 06:09:28 -04:00
} )
2020-09-17 04:30:08 -04:00
req = {
url : '/' ,
2020-08-18 06:09:28 -04:00
headers : {
2021-04-27 03:52:58 -04:00
'accept-language' : '' ,
} ,
2020-08-18 06:09:28 -04:00
}
2020-09-17 04:30:08 -04:00
res = {
locals : { } ,
2020-08-18 06:09:28 -04:00
getHeader : ( ) => { } ,
2021-04-27 03:52:58 -04:00
setHeader : ( ) => { } ,
2020-08-18 06:09:28 -04:00
}
} )
2021-04-14 09:17:21 -04:00
describe ( 'translate' , function ( ) {
beforeEach ( function ( done ) {
2020-09-17 04:30:08 -04:00
runMiddlewares ( done )
2020-08-18 06:09:28 -04:00
} )
2021-04-14 09:17:21 -04:00
it ( 'works' , function ( ) {
2020-09-17 04:30:08 -04:00
expect ( req . i18n . t ( 'give_feedback' ) ) . to . equal ( 'Give feedback' )
2020-08-18 06:09:28 -04:00
} )
2021-04-14 09:17:21 -04:00
it ( 'has translate alias' , function ( ) {
2020-09-17 04:30:08 -04:00
expect ( req . i18n . translate ( 'give_feedback' ) ) . to . equal ( 'Give feedback' )
2020-08-18 06:09:28 -04:00
} )
} )
2021-04-14 09:17:21 -04:00
describe ( 'interpolation' , function ( ) {
beforeEach ( function ( done ) {
2020-09-17 04:30:08 -04:00
runMiddlewares ( done )
2020-08-18 06:09:28 -04:00
} )
2021-04-14 09:17:21 -04:00
it ( 'works' , function ( ) {
2020-08-18 06:09:28 -04:00
expect (
2020-09-17 04:30:08 -04:00
req . i18n . t ( 'please_confirm_email' , {
2021-04-27 03:52:58 -04:00
emailAddress : 'foo@example.com' ,
2020-08-18 06:09:28 -04:00
} )
) . to . equal (
'Please confirm your email foo@example.com by clicking on the link in the confirmation email '
)
} )
2021-04-14 09:17:21 -04:00
it ( 'handles dashes after interpolation' , function ( ) {
2020-08-18 06:09:28 -04:00
// This translation string has a problematic interpolation followed by a
// dash: `__len__-day`
expect (
2020-09-17 04:30:08 -04:00
req . i18n . t ( 'faq_how_does_free_trial_works_answer' , {
2020-08-18 06:09:28 -04:00
appName : 'Overleaf' ,
2021-04-27 03:52:58 -04:00
len : '5' ,
2020-08-18 06:09:28 -04:00
} )
) . to . equal (
'You get full access to your chosen Overleaf plan during your 5-day free trial. There is no obligation to continue beyond the trial. Your card will be charged at the end of your 5 day trial unless you cancel before then. You can cancel via your subscription settings.'
)
} )
2021-04-14 09:17:21 -04:00
it ( 'disables escaping' , function ( ) {
2020-08-18 06:09:28 -04:00
expect (
2020-09-17 04:30:08 -04:00
req . i18n . t ( 'admin_user_created_message' , {
2021-04-27 03:52:58 -04:00
link : 'http://google.com' ,
2020-08-18 06:09:28 -04:00
} )
) . to . equal (
'Created admin user, <a href="http://google.com">Log in here</a> to continue'
)
} )
} )
2021-04-14 09:17:21 -04:00
describe ( 'setLangBasedOnDomainMiddleware' , function ( ) {
it ( 'should set the lang to french if the domain is fr' , function ( done ) {
2020-09-17 04:30:08 -04:00
req . headers . host = 'fr.sharelatex.com'
runMiddlewares ( ( ) => {
expect ( req . lng ) . to . equal ( 'fr' )
done ( )
2020-08-18 06:09:28 -04:00
} )
} )
2021-04-14 09:17:21 -04:00
describe ( 'suggestedLanguageSubdomainConfig' , function ( ) {
it ( 'should set suggestedLanguageSubdomainConfig if the detected lang is different to subdomain lang' , function ( done ) {
2020-09-17 04:30:08 -04:00
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'
2020-08-18 06:09:28 -04:00
)
2020-09-17 04:30:08 -04:00
done ( )
2020-08-18 06:09:28 -04:00
} )
} )
2021-04-14 09:17:21 -04:00
it ( 'should not set suggestedLanguageSubdomainConfig if the detected lang is the same as subdomain lang' , function ( done ) {
2020-09-17 04:30:08 -04:00
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 ( )
2020-08-18 06:09:28 -04:00
} )
} )
} )
} )
} )