Translator.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import {
  20. logging,
  21. configure,
  22. t,
  23. tn,
  24. addLocaleData,
  25. addTranslation,
  26. addTranslations,
  27. } from '@superset-ui/core';
  28. import Translator from '../../src/translation/Translator';
  29. import languagePackZh from './languagePacks/zh';
  30. import languagePackEn from './languagePacks/en';
  31. configure({
  32. languagePack: languagePackEn,
  33. });
  34. describe('Translator', () => {
  35. const spy = jest.spyOn(logging, 'warn');
  36. beforeAll(() => {
  37. spy.mockImplementation((info: any) => {
  38. throw new Error(info);
  39. });
  40. process.env.WEBPACK_MODE = 'production';
  41. });
  42. afterAll(() => {
  43. spy.mockRestore();
  44. process.env.WEBPACK_MODE = 'test';
  45. });
  46. describe('new Translator(config)', () => {
  47. it('initializes when config is not specified', () => {
  48. expect(new Translator()).toBeInstanceOf(Translator);
  49. });
  50. it('initializes when config is an empty object', () => {
  51. expect(new Translator({})).toBeInstanceOf(Translator);
  52. });
  53. it('initializes when config is specified', () => {
  54. expect(
  55. new Translator({
  56. languagePack: languagePackZh,
  57. }),
  58. ).toBeInstanceOf(Translator);
  59. });
  60. });
  61. describe('.translate(input, ...args)', () => {
  62. const translator = new Translator({
  63. languagePack: languagePackZh,
  64. });
  65. it('returns original text for unknown text', () => {
  66. expect(translator.translate('abc')).toEqual('abc');
  67. });
  68. it('translates simple text', () => {
  69. expect(translator.translate('second')).toEqual('秒');
  70. });
  71. it('translates template text with an argument', () => {
  72. expect(translator.translate('Copy of %s', 1)).toEqual('1 的副本');
  73. expect(translator.translate('Copy of %s', 2)).toEqual('2 的副本');
  74. });
  75. it('translates template text with multiple arguments', () => {
  76. expect(translator.translate('test %d %d', 1, 2)).toEqual('test 1 2');
  77. });
  78. });
  79. describe('.translateWithNumber(singular, plural, num, ...args)', () => {
  80. const translator = new Translator({
  81. languagePack: languagePackZh,
  82. });
  83. it('returns original text for unknown text', () => {
  84. expect(translator.translateWithNumber('fish', 'fishes', 1)).toEqual(
  85. 'fish',
  86. );
  87. });
  88. it('uses 0 as default value', () => {
  89. expect(translator.translateWithNumber('box', 'boxes')).toEqual('boxes');
  90. });
  91. it('translates simple text', () => {
  92. expect(translator.translateWithNumber('second', 'seconds', 1)).toEqual(
  93. '秒',
  94. );
  95. });
  96. it('translates template text with an argument', () => {
  97. expect(
  98. translator.translateWithNumber('Copy of %s', 'Copies of %s', 12, 12),
  99. ).toEqual('12 的副本');
  100. });
  101. it('translates template text with multiple arguments', () => {
  102. expect(
  103. translator.translateWithNumber(
  104. '%d glass %s',
  105. '%d glasses %s',
  106. 3,
  107. 3,
  108. 'abc',
  109. ),
  110. ).toEqual('3 glasses abc');
  111. });
  112. });
  113. describe('.translateWithNumber(key, num, ...args)', () => {
  114. const translator = new Translator({
  115. languagePack: languagePackEn,
  116. });
  117. it('translates template text with an argument', () => {
  118. expect(translator.translateWithNumber('%s copies', 1)).toEqual('1 copy');
  119. expect(translator.translateWithNumber('%s copies', 2)).toEqual(
  120. '2 copies',
  121. );
  122. });
  123. });
  124. // Extending language pack
  125. describe('.addTranslation(...)', () => {
  126. it('can add new translation', () => {
  127. addTranslation('haha', ['Hahaha']);
  128. expect(t('haha')).toEqual('Hahaha');
  129. });
  130. });
  131. describe('.addTranslations(...)', () => {
  132. it('can add new translations', () => {
  133. addTranslations({
  134. foo: ['bar', '%s bars'],
  135. bar: ['foo'],
  136. });
  137. // previous translation still exists
  138. expect(t('haha')).toEqual('Hahaha');
  139. // new translations work as expected
  140. expect(tn('foo', 1)).toEqual('bar');
  141. expect(tn('foo', 2)).toEqual('2 bars');
  142. expect(tn('bar', 2)).toEqual('bar');
  143. });
  144. it('throw warning on invalid arguments', () => {
  145. expect(() => addTranslations(undefined as never)).toThrow(
  146. 'Invalid translations',
  147. );
  148. expect(tn('bar', '2 foo', 2)).toEqual('2 foo');
  149. });
  150. it('throw warning on duplicates', () => {
  151. expect(() => {
  152. addTranslations({
  153. haha: ['this is duplicate'],
  154. });
  155. }).toThrow('Duplicate translation key "haha"');
  156. expect(t('haha')).toEqual('Hahaha');
  157. });
  158. });
  159. describe('.addLocaleData(...)', () => {
  160. it('can add new translations for language', () => {
  161. addLocaleData({
  162. en: {
  163. yes: ['ok'],
  164. },
  165. });
  166. expect(t('yes')).toEqual('ok');
  167. });
  168. it('throw on unknown locale', () => {
  169. expect(() => {
  170. addLocaleData({
  171. zh: {
  172. haha: ['yes'],
  173. },
  174. });
  175. }).toThrow('Invalid locale data');
  176. });
  177. it('missing locale falls back to English', () => {
  178. configure({
  179. languagePack: languagePackZh,
  180. });
  181. // expect and error because zh is not current locale
  182. expect(() => {
  183. addLocaleData({
  184. en: {
  185. yes: ['OK'],
  186. },
  187. });
  188. }).not.toThrow();
  189. expect(t('yes')).toEqual('OK');
  190. });
  191. });
  192. });