-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpersian.go
241 lines (222 loc) · 4.66 KB
/
persian.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package persian
import (
"fmt"
"regexp"
"strings"
)
//ToPersianDigits Converts all English digits in the string to Persian digits.
func ToPersianDigits(text string) string {
return strings.NewReplacer(
"0", "۰",
"1", "۱",
"2", "۲",
"3", "۳",
"4", "۴",
"5", "۵",
"6", "۶",
"7", "۷",
"8", "۸",
"9", "۹",
).Replace(text)
}
//ToPersianDigitsFromInt Converts integer value to string with Persian digits.
func ToPersianDigitsFromInt(value int) string {
return ToPersianDigits(fmt.Sprintf("%d", value))
}
//ToEnglishDigits Converts all Persian digits in the string to English digits.
func ToEnglishDigits(text string) string {
return strings.NewReplacer(
"۰", "0",
"۱", "1",
"۲", "2",
"۳", "3",
"۴", "4",
"۵", "5",
"۶", "6",
"۷", "7",
"۸", "8",
"۹", "9",
// Arabic Number
"٠", "0",
"١", "1",
"٢", "2",
"٣", "3",
"٤", "4",
"٥", "5",
"٦", "6",
"٧", "7",
"٨", "8",
"٩", "9",
).Replace(text)
}
//OnlyEnglishNumbers extracts only English digits from string.
func OnlyEnglishNumbers(text string) string {
re := regexp.MustCompile("[^0-9.]")
return re.ReplaceAllLiteralString(text, "")
}
//OnlyPersianNumbers extracts only Persian digits from string.
func OnlyPersianNumbers(text string) string {
re := regexp.MustCompile("[^۰-۹.]")
return re.ReplaceAllLiteralString(text, "")
}
//OnlyNumbers extracts only digits from string.
func OnlyNumbers(text string) string {
re := regexp.MustCompile("[^۰-۹0-9.]")
return re.ReplaceAllLiteralString(text, "")
}
//OnlyPersianAlpha extracts only persian alphabetes from string.
func OnlyPersianAlpha(text string) string {
re := regexp.MustCompile("[^\u0600-\u06FF.]")
return re.ReplaceAllLiteralString(text, "")
}
//SwitchToPersianKey converts English chars to their equivalent Persian char on keyboard.
func SwitchToPersianKey(text string) string {
return strings.NewReplacer(
"q", "ض",
"w", "ص",
"e", "ث",
"r", "ق",
"t", "ف",
"y", "غ",
"u", "ع",
"i", "ه",
"o", "خ",
"p", "ح",
"\\[", "ج",
"\\]", "چ",
"a", "ش",
"s", "س",
"d", "ی",
"f", "ب",
"g", "ل",
"h", "ا",
"j", "ت",
"k", "ن",
"l", "م",
";", "ک",
"'", "گ",
"z", "ظ",
"x", "ط",
"c", "ز",
"v", "ر",
"b", "ذ",
"n", "د",
"m", "پ",
",", "و",
"?", "؟",
).Replace(text)
}
//SwitchToEnglishKey converts Persian chars to their equivalent English char on keyboard.
func SwitchToEnglishKey(text string) string {
return strings.NewReplacer(
"ض", "q",
"ص", "w",
"ث", "e",
"ق", "r",
"ف", "t",
"غ", "y",
"ع", "u",
"ه", "i",
"خ", "o",
"ح", "p",
"ج", "\\[",
"چ", "\\]",
"ش", "a",
"س", "s",
"ی", "d",
"ب", "f",
"ل", "g",
"ا", "h",
"ت", "j",
"ن", "k",
"م", "l",
"ک", ";",
"گ", "'",
"ظ", "z",
"ط", "x",
"ز", "c",
"ر", "v",
"ذ", "b",
"د", "n",
"پ", "m",
"و", ",",
"؟", "?",
).Replace(text)
}
//FixArabic used for converting Arabic characters to Persian.
func FixArabic(text string) string {
return strings.NewReplacer(
"ي", "ی",
"ك", "ک",
"دِ", "د",
"بِ", "ب",
"زِ", "ز",
"ذِ", "ذ",
"ِشِ", "ش",
"ِسِ", "س",
"ى", "ی",
).Replace(text)
}
//Normalize used for Normalize Persian for sort and equality check.
//TODO: Complete list according to Persian Collation
func Normalize(text string) string {
return strings.NewReplacer(
"ي", "ی",
"ك", "ک",
"دِ", "د",
"بِ", "ب",
"زِ", "ز",
"ذِ", "ذ",
"ِشِ", "ش",
"ِسِ", "س",
"", " ",
"", " ",
"ى", "ی",
"ٱ","ا" ,
"آ","ا" ,
"ء","ا" ,
"ئ","ی" ,
"أ","ا" ,
"ة","ه" ,
).Replace(text)
}
//Reverse reverses the given string.
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
// Currency formats number to Persian currency style.
func Currency(amount string) string {
countThrees := 0
out := ""
_amount := []rune(OnlyNumbers(amount))
for i := len(_amount) - 1; i >= 0; i-- {
if countThrees == 3 {
out += "،" + string(_amount[i])
countThrees = 1
} else {
out += string(_amount[i])
countThrees++
}
}
return ToPersianDigits(Reverse(out))
}
// Toman formats number to Persian currency style with تومان postfix.
func Toman(amount string) string {
return Currency(amount) + " تومان"
}
// Rial formats number to Persian currency style with ﷼ postfix.
func Rial(amount string) string {
return Currency(amount) + " ﷼"
}
func CheckIsEnglish(text string) bool {
for _, r := range text {
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
return false
}
}
return true
}