TranslationsTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const { expect } = require('chai')
  2. const SandboxedModule = require('sandboxed-module')
  3. const MODULE_PATH = '../../../../app/src/infrastructure/Translations.js'
  4. describe('Translations', function () {
  5. let req, res, translations
  6. function runMiddlewares(cb) {
  7. translations.i18nMiddleware(req, res, () => {
  8. translations.setLangBasedOnDomainMiddleware(req, res, cb)
  9. })
  10. }
  11. beforeEach(function () {
  12. translations = SandboxedModule.require(MODULE_PATH, {
  13. requires: {
  14. '@overleaf/settings': {
  15. i18n: {
  16. escapeHTMLInVars: false,
  17. subdomainLang: {
  18. www: { lngCode: 'en', url: 'https://www.overleaf.com' },
  19. fr: { lngCode: 'fr', url: 'https://fr.overleaf.com' },
  20. da: { lngCode: 'da', url: 'https://da.overleaf.com' },
  21. },
  22. },
  23. },
  24. },
  25. })
  26. req = {
  27. url: '/',
  28. headers: {
  29. 'accept-language': '',
  30. },
  31. }
  32. res = {
  33. locals: {},
  34. getHeader: () => {},
  35. setHeader: () => {},
  36. }
  37. })
  38. describe('translate', function () {
  39. beforeEach(function (done) {
  40. runMiddlewares(done)
  41. })
  42. it('works', function () {
  43. expect(req.i18n.t('give_feedback')).to.equal('Give feedback')
  44. })
  45. it('has translate alias', function () {
  46. expect(req.i18n.translate('give_feedback')).to.equal('Give feedback')
  47. })
  48. })
  49. describe('interpolation', function () {
  50. beforeEach(function (done) {
  51. runMiddlewares(done)
  52. })
  53. it('works', function () {
  54. expect(
  55. req.i18n.t('please_confirm_email', {
  56. emailAddress: 'foo@example.com',
  57. })
  58. ).to.equal(
  59. 'Please confirm your email foo@example.com by clicking on the link in the confirmation email '
  60. )
  61. })
  62. it('handles dashes after interpolation', function () {
  63. // This translation string has a problematic interpolation followed by a
  64. // dash: `__len__-day`
  65. expect(
  66. req.i18n.t('faq_how_does_free_trial_works_answer', {
  67. appName: 'Overleaf',
  68. len: '5',
  69. })
  70. ).to.equal(
  71. '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.'
  72. )
  73. })
  74. it('disables escaping', function () {
  75. expect(
  76. req.i18n.t('admin_user_created_message', {
  77. link: 'http://google.com',
  78. })
  79. ).to.equal(
  80. 'Created admin user, <a href="http://google.com">Log in here</a> to continue'
  81. )
  82. })
  83. })
  84. describe('setLangBasedOnDomainMiddleware', function () {
  85. it('should set the lang to french if the domain is fr', function (done) {
  86. req.headers.host = 'fr.overleaf.com'
  87. runMiddlewares(() => {
  88. expect(req.lng).to.equal('fr')
  89. done()
  90. })
  91. })
  92. describe('suggestedLanguageSubdomainConfig', function () {
  93. it('should set suggestedLanguageSubdomainConfig if the detected lang is different to subdomain lang', function (done) {
  94. req.headers['accept-language'] = 'da, en-gb;q=0.8, en;q=0.7'
  95. req.headers.host = 'fr.overleaf.com'
  96. runMiddlewares(() => {
  97. expect(res.locals.suggestedLanguageSubdomainConfig).to.exist
  98. expect(res.locals.suggestedLanguageSubdomainConfig.lngCode).to.equal(
  99. 'da'
  100. )
  101. done()
  102. })
  103. })
  104. it('should not set suggestedLanguageSubdomainConfig if the detected lang is the same as subdomain lang', function (done) {
  105. req.headers['accept-language'] = 'da, en-gb;q=0.8, en;q=0.7'
  106. req.headers.host = 'da.overleaf.com'
  107. runMiddlewares(() => {
  108. expect(res.locals.suggestedLanguageSubdomainConfig).to.not.exist
  109. done()
  110. })
  111. })
  112. })
  113. })
  114. })