|
8 | 8 | getCurrentInstance,
|
9 | 9 | ComponentOptions
|
10 | 10 | } from 'vue'
|
11 |
| -import { mount } from './helper' |
| 11 | +import { mount, pluralRules as _pluralRules } from './helper' |
12 | 12 | import { createI18n, useI18n } from '../src/index'
|
13 | 13 | import { errorMessages, I18nErrorCodes } from '../src/errors'
|
14 | 14 | import { Composer } from '../src/composer'
|
@@ -680,3 +680,145 @@ test('merge i18n custom blocks to global scope', async () => {
|
680 | 680 | foo: 'ふー!'
|
681 | 681 | })
|
682 | 682 | })
|
| 683 | + |
| 684 | +describe('custom pluralization', () => { |
| 685 | + test('legacy', async () => { |
| 686 | + const i18n = createI18n({ |
| 687 | + locale: 'ru', |
| 688 | + pluralizationRules: _pluralRules, |
| 689 | + messages: { |
| 690 | + ru: { |
| 691 | + car: '0 машин | {n} машина | {n} машины | {n} машин' |
| 692 | + } |
| 693 | + } |
| 694 | + }) |
| 695 | + |
| 696 | + const App = defineComponent({ |
| 697 | + template: ` |
| 698 | + <p>{{ $tc('car', 1) }}</p> |
| 699 | + <p>{{ $tc('car', 2) }}</p> |
| 700 | + <p>{{ $tc('car', 4) }}</p> |
| 701 | + <p>{{ $tc('car', 12) }}</p> |
| 702 | + <p>{{ $tc('car', 21) }}</p> |
| 703 | + ` |
| 704 | + }) |
| 705 | + const { find } = await mount(App, i18n) |
| 706 | + await nextTick() |
| 707 | + expect(find('p:nth-child(1)')!.innerHTML).toEqual('1 машина') |
| 708 | + expect(find('p:nth-child(2)')!.innerHTML).toEqual('2 машины') |
| 709 | + expect(find('p:nth-child(3)')!.innerHTML).toEqual('4 машины') |
| 710 | + expect(find('p:nth-child(4)')!.innerHTML).toEqual('12 машин') |
| 711 | + expect(find('p:nth-child(5)')!.innerHTML).toEqual('21 машина') |
| 712 | + }) |
| 713 | + |
| 714 | + test('legacy + custom block', async () => { |
| 715 | + const i18n = createI18n({ |
| 716 | + locale: 'ru', |
| 717 | + pluralizationRules: _pluralRules |
| 718 | + }) |
| 719 | + |
| 720 | + const App = defineComponent({ |
| 721 | + __i18n: [ |
| 722 | + { |
| 723 | + locale: 'ru', |
| 724 | + resource: { |
| 725 | + car: '0 машин | {n} машина | {n} машины | {n} машин' |
| 726 | + } |
| 727 | + } |
| 728 | + ], |
| 729 | + template: ` |
| 730 | + <p>{{ $tc('car', 1) }}</p> |
| 731 | + <p>{{ $tc('car', 2) }}</p> |
| 732 | + <p>{{ $tc('car', 4) }}</p> |
| 733 | + <p>{{ $tc('car', 12) }}</p> |
| 734 | + <p>{{ $tc('car', 21) }}</p> |
| 735 | + ` |
| 736 | + }) |
| 737 | + const { find } = await mount(App, i18n) |
| 738 | + await nextTick() |
| 739 | + expect(find('p:nth-child(1)')!.innerHTML).toEqual('1 машина') |
| 740 | + expect(find('p:nth-child(2)')!.innerHTML).toEqual('2 машины') |
| 741 | + expect(find('p:nth-child(3)')!.innerHTML).toEqual('4 машины') |
| 742 | + expect(find('p:nth-child(4)')!.innerHTML).toEqual('12 машин') |
| 743 | + expect(find('p:nth-child(5)')!.innerHTML).toEqual('21 машина') |
| 744 | + }) |
| 745 | + |
| 746 | + test('composition', async () => { |
| 747 | + const i18n = createI18n({ |
| 748 | + legacy: false, |
| 749 | + locale: 'ru', |
| 750 | + pluralRules: _pluralRules, |
| 751 | + messages: { |
| 752 | + ru: { |
| 753 | + car: '0 машин | {n} машина | {n} машины | {n} машин' |
| 754 | + } |
| 755 | + } |
| 756 | + }) |
| 757 | + |
| 758 | + const App = defineComponent({ |
| 759 | + setup() { |
| 760 | + const { t } = useI18n() |
| 761 | + return { t } |
| 762 | + }, |
| 763 | + template: ` |
| 764 | + <p>{{ t('car', 1) }}</p> |
| 765 | + <p>{{ t('car', 2) }}</p> |
| 766 | + <p>{{ t('car', 4) }}</p> |
| 767 | + <p>{{ t('car', 12) }}</p> |
| 768 | + <p>{{ t('car', 21) }}</p> |
| 769 | + ` |
| 770 | + }) |
| 771 | + const { find } = await mount(App, i18n) |
| 772 | + await nextTick() |
| 773 | + expect(find('p:nth-child(1)')!.innerHTML).toEqual('1 машина') |
| 774 | + expect(find('p:nth-child(2)')!.innerHTML).toEqual('2 машины') |
| 775 | + expect(find('p:nth-child(3)')!.innerHTML).toEqual('4 машины') |
| 776 | + expect(find('p:nth-child(4)')!.innerHTML).toEqual('12 машин') |
| 777 | + expect(find('p:nth-child(5)')!.innerHTML).toEqual('21 машина') |
| 778 | + }) |
| 779 | + |
| 780 | + test('composition + custom block', async () => { |
| 781 | + const i18n = createI18n({ |
| 782 | + legacy: false, |
| 783 | + locale: 'ru' |
| 784 | + }) |
| 785 | + |
| 786 | + const App = defineComponent({ |
| 787 | + setup() { |
| 788 | + const instance = getCurrentInstance() |
| 789 | + if (instance == null) { |
| 790 | + throw new Error() |
| 791 | + } |
| 792 | + const options = instance.type as ComponentOptions |
| 793 | + options.__i18n = [ |
| 794 | + { |
| 795 | + locale: 'ru', |
| 796 | + resource: { |
| 797 | + car: '0 машин | {n} машина | {n} машины | {n} машин' |
| 798 | + } |
| 799 | + } |
| 800 | + ] |
| 801 | + const { t } = useI18n({ |
| 802 | + inheritLocale: true, |
| 803 | + useScope: 'local', |
| 804 | + pluralRules: _pluralRules |
| 805 | + }) |
| 806 | + return { t } |
| 807 | + }, |
| 808 | + template: ` |
| 809 | + <p>{{ t('car', 1) }}</p> |
| 810 | + <p>{{ t('car', 2) }}</p> |
| 811 | + <p>{{ t('car', 4) }}</p> |
| 812 | + <p>{{ t('car', 12) }}</p> |
| 813 | + <p>{{ t('car', 21) }}</p> |
| 814 | + ` |
| 815 | + }) |
| 816 | + const { find } = await mount(App, i18n) |
| 817 | + await nextTick() |
| 818 | + expect(find('p:nth-child(1)')!.innerHTML).toEqual('1 машина') |
| 819 | + expect(find('p:nth-child(2)')!.innerHTML).toEqual('2 машины') |
| 820 | + expect(find('p:nth-child(3)')!.innerHTML).toEqual('4 машины') |
| 821 | + expect(find('p:nth-child(4)')!.innerHTML).toEqual('12 машин') |
| 822 | + expect(find('p:nth-child(5)')!.innerHTML).toEqual('21 машина') |
| 823 | + }) |
| 824 | +}) |
0 commit comments