-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtranslit_example_test.go
More file actions
38 lines (30 loc) · 928 Bytes
/
translit_example_test.go
File metadata and controls
38 lines (30 loc) · 928 Bytes
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
package translit_test
import (
"fmt"
"github.com/mxmCherry/translit"
"golang.org/x/text/transform"
)
func ExampleMap() {
// pre-compile a transformer factory from rule map;
// this is recommended to be a global variable in your own package:
custom := translit.Map(
map[string]string{
"л": "l",
"Л": "L",
"ля": "lya",
"Ля": "Lya",
},
)
// get a "fresh" transformer (can be done for each transliteration instead of tr.Reset()):
tr := custom.Transformer()
var s string
tr.Reset() // reset transformer state before usage - it is stateful and non-thread-safe
s, _, _ = transform.String(tr, "Л - л")
fmt.Println(s) // L - l
tr.Reset() // reset transformer state before usage - it is stateful and non-thread-safe
s, _, _ = transform.String(tr, "Ля-лЯ-ля")
fmt.Println(s) // Lya-lЯ-lya - no rule for upper-case "Я", so it's not converted
// Output:
// L - l
// Lya-lЯ-lya
}