-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
64 lines (57 loc) · 1.8 KB
/
example_test.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
// Copyright (c) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
package address_test
import (
"fmt"
"github.com/bojanz/address"
)
func ExampleNewLocale() {
firstLocale := address.NewLocale("en-US")
fmt.Println(firstLocale)
fmt.Println(firstLocale.Language, firstLocale.Territory)
// Locale IDs are normalized.
secondLocale := address.NewLocale("sr_rs_latn")
fmt.Println(secondLocale)
fmt.Println(secondLocale.Language, secondLocale.Script, secondLocale.Territory)
// Output: en-US
// en US
// sr-Latn-RS
// sr Latn RS
}
func ExampleFormatter_Format() {
locale := address.NewLocale("en")
formatter := address.NewFormatter(locale)
addr := address.Address{
Line1: "1098 Alta Ave",
Locality: "Mountain View",
Region: "CA",
PostalCode: "94043",
CountryCode: "US",
}
fmt.Println(formatter.Format(addr))
addr = address.Address{
Line1: "幸福中路",
Sublocality: "新城区",
Locality: "西安市",
Region: "SN",
PostalCode: "710043",
CountryCode: "CN",
}
locale = address.NewLocale("zh")
formatter = address.NewFormatter(locale)
formatter.NoCountry = true
formatter.WrapperElement = "div"
formatter.WrapperClass = "postal-address"
fmt.Println(formatter.Format(addr))
// Output:
// <p class="address" translate="no">
// <span class="line1">1098 Alta Ave</span><br>
// <span class="locality">Mountain View</span>, <span class="region">CA</span> <span class="postal-code">94043</span><br>
// <span class="country" data-value="US">United States</span>
// </p>
// <div class="postal-address" translate="no">
// <span class="postal-code">710043</span><br>
// <span class="region">陕西省</span><span class="locality">西安市</span><span class="sublocality">新城区</span><br>
// <span class="line1">幸福中路</span>
// </div>
}