i18n.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { describe, it, expect } from 'vitest';
  6. import { I18n } from '../src';
  7. describe('i18n', () => {
  8. it('default', () => {
  9. expect(I18n.locale).toBe('en-US');
  10. });
  11. it('setLocal', () => {
  12. let changeTimes = 0;
  13. let dispose = I18n.onLanguageChange((langId) => {
  14. changeTimes++;
  15. });
  16. I18n.locale = 'en-US';
  17. expect(changeTimes).toEqual(0);
  18. I18n.locale = 'zh-CN';
  19. expect(changeTimes).toEqual(1);
  20. dispose.dispose();
  21. I18n.locale = 'en-US';
  22. expect(changeTimes).toEqual(1);
  23. });
  24. it('translation', () => {
  25. expect(I18n.t('Yes')).toEqual('Yes');
  26. I18n.locale = 'zh-CN';
  27. expect(I18n.t('Yes')).toEqual('是');
  28. expect(I18n.t('Unknown')).toEqual('Unknown');
  29. expect(I18n.t('Unknown', { defaultValue: '' })).toEqual('');
  30. I18n.addLanguage({
  31. languageId: 'zh-CN',
  32. contents: {
  33. Unknown: '未知',
  34. },
  35. });
  36. expect(I18n.t('Unknown')).toEqual('未知');
  37. expect(I18n.t('Unknown', { defaultValue: '' })).toEqual('未知');
  38. });
  39. it('missingStrictMode', () => {
  40. I18n.locale = 'en-US';
  41. I18n.missingStrictMode = true;
  42. expect(I18n.t('Unknown')).toEqual('[missing "en-US.Unknown" translation]');
  43. I18n.missingStrictMode = false;
  44. expect(I18n.t('Unknown')).toEqual('Unknown');
  45. });
  46. });