|
1 | | -import { assertEquals } from "@std/assert"; |
| 1 | +import { assertEquals } from "@std/assert/assert-equals"; |
2 | 2 | import { Formatter } from "./formatter.ts"; |
| 3 | +import { defaultHooks } from "./plugins/default.ts"; |
3 | 4 |
|
4 | | -Deno.test("formatter, very simple text", () => { |
5 | | - const formatter = new Formatter(); |
6 | | - |
7 | | - assertEquals( |
8 | | - formatter.format("Hello World"), |
9 | | - "Hello World", |
10 | | - ); |
11 | | -}); |
12 | | - |
13 | | -Deno.test("formatter, basic template value", () => { |
14 | | - const formatter = new Formatter(); |
15 | | - |
16 | | - assertEquals( |
17 | | - formatter.format("Hello {target}", { |
18 | | - target: "World!", |
19 | | - }), |
20 | | - "Hello World!", |
21 | | - ); |
22 | | -}); |
23 | | - |
24 | | -Deno.test("formatter, template value with method", () => { |
25 | | - const formatter = new Formatter({ |
26 | | - plugin: { |
27 | | - 이({ current: self }) { |
28 | | - if (self === "사과") { |
29 | | - return "사과가"; |
30 | | - } |
31 | | - return `${self}이`; |
32 | | - }, |
| 5 | +Deno.test("formatter, plural", () => { |
| 6 | + const messages = { |
| 7 | + ko: { |
| 8 | + "You have {count} file{count.other:}s{/}.": |
| 9 | + "{count}개의 파일이 있습니다.", |
| 10 | + }, |
| 11 | + ar: { |
| 12 | + "You have {count} file{count.other:}s{/}.": |
| 13 | + "{count.zero:}لا يوجد لديك ملفات.{.one:}لديك ملف واحد.{.two:}لديك ملفان.{.few:}لديك {count} ملفات قليلة.{.many:}لديك {count} ملفات كثيرة.{.other:}لديك {count} ملفات.{/}", |
33 | 14 | }, |
34 | | - }); |
| 15 | + }; |
35 | 16 |
|
36 | | - assertEquals( |
37 | | - formatter.format("{name.이} 없습니다.", { |
38 | | - name: "파일", |
39 | | - }), |
40 | | - "파일이 없습니다.", |
41 | | - ); |
42 | | - assertEquals( |
43 | | - formatter.format("{name.이} 없습니다.", { |
44 | | - name: "사과", |
45 | | - }), |
46 | | - "사과가 없습니다.", |
47 | | - ); |
48 | | -}); |
| 17 | + { |
| 18 | + const formatter = new Formatter({ |
| 19 | + locale: "en", |
| 20 | + hooks: defaultHooks, |
| 21 | + }); |
49 | 22 |
|
50 | | -Deno.test("formatter, template with method that returns template", () => { |
51 | | - const formatter = new Formatter({ |
52 | | - plugin: { |
53 | | - one({ self, args, metadata }) { |
54 | | - if (self == 1) { |
55 | | - metadata.matched = true; |
56 | | - return typeof args[0] === "function" ? args[0]() : args[0]; |
57 | | - } |
58 | | - return ""; |
59 | | - }, |
60 | | - other({ current, args, metadata }) { |
61 | | - if (!metadata.matched) { |
62 | | - return typeof args[0] === "function" ? args[0]() : args[0]; |
63 | | - } |
64 | | - return current; |
65 | | - }, |
66 | | - male({ self, args, metadata }) { |
67 | | - if (self === "male") { |
68 | | - metadata.matched = true; |
69 | | - return typeof args[0] === "function" ? args[0]() : args[0]; |
70 | | - } |
71 | | - return ""; |
72 | | - }, |
73 | | - female({ self, args, metadata }) { |
74 | | - if (self === "female") { |
75 | | - metadata.matched = true; |
76 | | - return typeof args[0] === "function" ? args[0]() : args[0]; |
77 | | - } |
78 | | - return ""; |
79 | | - }, |
80 | | - }, |
81 | | - }); |
| 23 | + assertEquals( |
| 24 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 25 | + count: 1, |
| 26 | + }), |
| 27 | + "You have 1 file.", |
| 28 | + ); |
| 29 | + assertEquals( |
| 30 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 31 | + count: 2, |
| 32 | + }), |
| 33 | + "You have 2 files.", |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + { |
| 38 | + const formatter = new Formatter({ |
| 39 | + locale: "ko", |
| 40 | + hooks: defaultHooks, |
| 41 | + messages: messages.ko, |
| 42 | + }); |
82 | 43 |
|
83 | | - assertEquals( |
84 | | - formatter.format( |
85 | | - "{user} added {photoCount.one:}a new photo{.other:}{_} new photos{/} to {userGender.male:}his{.female:}her{.other:}their{/} steam.", |
86 | | - { |
87 | | - user: "Alex", |
88 | | - photoCount: 3, |
89 | | - userGender: "female", |
90 | | - }, |
91 | | - ), |
92 | | - "Alex added 3 new photos to her steam.", |
93 | | - ); |
| 44 | + assertEquals( |
| 45 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 46 | + count: 1, |
| 47 | + }), |
| 48 | + "1개의 파일이 있습니다.", |
| 49 | + ); |
| 50 | + } |
| 51 | + { |
| 52 | + const formatter = new Formatter({ |
| 53 | + locale: "ar", |
| 54 | + hooks: defaultHooks, |
| 55 | + messages: messages.ar, |
| 56 | + }); |
94 | 57 |
|
95 | | - assertEquals( |
96 | | - formatter.format( |
97 | | - "{user} added {photoCount.one:}a new photo{.other:}{_} new photos{/} to {userGender.male:}his{.female:}her{.other:}their{/} steam.", |
98 | | - { |
99 | | - user: "Alex", |
100 | | - photoCount: 1, |
101 | | - userGender: "unknown", |
102 | | - }, |
103 | | - ), |
104 | | - "Alex added a new photo to their steam.", |
105 | | - ); |
| 58 | + assertEquals( |
| 59 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 60 | + count: 0, |
| 61 | + }), |
| 62 | + "لا يوجد لديك ملفات.", |
| 63 | + ); |
| 64 | + assertEquals( |
| 65 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 66 | + count: 1, |
| 67 | + }), |
| 68 | + "لديك ملف واحد.", |
| 69 | + ); |
| 70 | + assertEquals( |
| 71 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 72 | + count: 2, |
| 73 | + }), |
| 74 | + "لديك ملفان.", |
| 75 | + ); |
| 76 | + assertEquals( |
| 77 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 78 | + count: 3, |
| 79 | + }), |
| 80 | + "لديك 3 ملفات قليلة.", |
| 81 | + ); |
| 82 | + assertEquals( |
| 83 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 84 | + count: 11, |
| 85 | + }), |
| 86 | + "لديك 11 ملفات كثيرة.", |
| 87 | + ); |
| 88 | + assertEquals( |
| 89 | + formatter.format("You have {count} file{count.other:}s{/}.", { |
| 90 | + count: 100, |
| 91 | + }), |
| 92 | + "لديك 100 ملفات.", |
| 93 | + ); |
| 94 | + } |
106 | 95 | }); |
0 commit comments