gettext.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { Gettext } from '@jupyterlab/translation';
  4. let JSON_TEST_DATA = {
  5. '': {
  6. domain: 'jupyterlab',
  7. language: 'es',
  8. pluralForms: 'nplurals=2; plural=n>1;',
  9. version: '2.2.0rc1'
  10. },
  11. Welcome: ['Bienvenido'],
  12. 'Welcome %1': ['Bienvenido %1'],
  13. 'Context\u0004Welcome': ['Hola'],
  14. 'Context\u0004Welcome %1': ['Hola %1'],
  15. 'There is %1 apple': ['Hay %1 manzana', 'Hay %1 manzanas'],
  16. 'There is %1 apple, %2!': ['Hay %1 manzana, %2!', 'Hay %1 manzanas, %2!']
  17. };
  18. let JSON_TEST_DATA_VARIATION = {
  19. '': {
  20. domain: 'jupyterlab',
  21. language: 'es-CO',
  22. pluralForms: 'nplurals=2; plural=n>1;',
  23. version: '2.2.0rc1'
  24. },
  25. Welcome: ['Bienvenido pirobo']
  26. };
  27. describe('@jupyterlab/translation', () => {
  28. const trans = new Gettext({ domain: 'jupyterlab', locale: 'es' });
  29. trans.loadJSON(JSON_TEST_DATA, 'jupyterlab');
  30. trans.loadJSON(JSON_TEST_DATA_VARIATION, 'jupyterlab');
  31. describe('Gettext', () => {
  32. describe('#gettext', () => {
  33. it('should test whether the gettext bundle gettext/__ works', () => {
  34. // Shorthand method
  35. expect(trans.__('Welcome')).toBe('Bienvenido');
  36. expect(trans.__('Welcome %1', 'Joe')).toBe('Bienvenido Joe');
  37. // Normal method
  38. expect(trans.gettext('Welcome')).toBe('Bienvenido');
  39. expect(trans.gettext('Welcome %1', 'Joe')).toBe('Bienvenido Joe');
  40. });
  41. });
  42. describe('#pgettext', () => {
  43. it('should test whether the gettext bundle pgettext/_p works', () => {
  44. // Shorthand method
  45. expect(trans._p('Context', 'Welcome')).toBe('Hola');
  46. expect(trans._p('Context', 'Welcome %1', 'Joe')).toBe('Hola Joe');
  47. // Normal method
  48. expect(trans.pgettext('Context', 'Welcome')).toBe('Hola');
  49. expect(trans.pgettext('Context', 'Welcome %1', 'Joe')).toBe('Hola Joe');
  50. });
  51. });
  52. describe('#ngettext', () => {
  53. it('should test whether the gettext bundle ngettext/_n works', () => {
  54. // Shorthand method
  55. expect(trans._n('There is %1 apple', 'There are %1 apples', 1)).toBe(
  56. 'Hay 1 manzana'
  57. );
  58. expect(trans._n('There is %1 apple', 'There are %1 apples', 2)).toBe(
  59. 'Hay 2 manzanas'
  60. );
  61. expect(
  62. trans._n('There is %1 apple, %2!', 'I have %1 apples %2', 1, 'Joe')
  63. ).toBe('Hay 1 manzana, Joe!');
  64. expect(
  65. trans._n('There is %1 apple, %2!', 'I have %1 apples %2', 2, 'Joe')
  66. ).toBe('Hay 2 manzanas, Joe!');
  67. // Normal method
  68. expect(
  69. trans.ngettext('There is %1 apple', 'There are %1 apples', 1)
  70. ).toBe('Hay 1 manzana');
  71. expect(
  72. trans.ngettext('There is %1 apple', 'There are %1 apples', 2)
  73. ).toBe('Hay 2 manzanas');
  74. expect(
  75. trans.ngettext(
  76. 'There is %1 apple, %2!',
  77. 'I have %1 apples %2',
  78. 1,
  79. 'Joe'
  80. )
  81. ).toBe('Hay 1 manzana, Joe!');
  82. expect(
  83. trans.ngettext(
  84. 'There is %1 apple, %2!',
  85. 'I have %1 apples %2',
  86. 2,
  87. 'Joe'
  88. )
  89. ).toBe('Hay 2 manzanas, Joe!');
  90. });
  91. });
  92. describe('#fallbackLocale', () => {
  93. it('should test whether the gettext bundle pgettext/_p works', () => {
  94. trans.setLocale('es-CO');
  95. // Shorthand method
  96. expect(trans.__('Welcome')).toBe('Bienvenido pirobo');
  97. // Normal method
  98. expect(trans.gettext('Welcome')).toBe('Bienvenido pirobo');
  99. });
  100. });
  101. });
  102. });