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
|
/*
* Copyright (C) 2014 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package iso
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
. "pkg.deepin.io/lib/gettext"
"strings"
"sync"
)
const iso3166XMLFile = "/usr/share/xml/iso-codes/iso_3166.xml"
// map dtd of iso_3166.xml to go structures
type CountryDatabase struct {
Countries []Country `xml:"iso_3166_entry"`
}
type Country struct {
Alpha2Code string `xml:"alpha_2_code,attr"`
Alpha3Code string `xml:"alpha_3_code,attr"`
NumericCode string `xml:"numeric_code,attr"`
CommonName string `xml:"common_name,attr"`
Name string `xml:"name,attr"`
OfficialName string `xml:"official_name,attr"`
}
var countryDatabase *CountryDatabase
var countryDatabaseLock sync.Mutex
var (
errLanguageFormatInvalid = fmt.Errorf("invalid environment variable LANGUAGE")
errCountryCodeInvalid = fmt.Errorf("invalid country code")
)
// GetCountryDatabase return country database that marshaled from ISO
// 3166 xml file.
func GetCountryDatabase() (*CountryDatabase, error) {
countryDatabaseLock.Lock()
defer countryDatabaseLock.Unlock()
if countryDatabase != nil {
return countryDatabase, nil
}
countryDatabase = &CountryDatabase{}
xmlContent, err := ioutil.ReadFile(iso3166XMLFile)
if err != nil {
return countryDatabase, err
}
err = xml.Unmarshal(xmlContent, countryDatabase)
return countryDatabase, err
}
// GetLocaleCountryCode return locale country code by analysis
// environment variable "LANGUAGE".
func GetLocaleCountryCode() (code string, err error) {
return GetCountryCodeForLanguage(getLocalLanguage())
}
func getLocalLanguage() string {
if value := os.Getenv("LANGUAGE"); len(value) > 0 {
return value
}
if value := os.Getenv("LC_ALL"); len(value) > 0 {
return value
}
if value := os.Getenv("LC_MESSAGES"); len(value) > 0 {
return value
}
if value := os.Getenv("LANG"); len(value) > 0 {
return value
}
return "en_US.UTF-8"
}
// GetLocaleCountryName return locale country name by analysis
// environment variable "LANGUAGE".
func GetLocaleCountryName() (name string, err error) {
code, err := GetLocaleCountryCode()
if err != nil {
return
}
return GetCountryNameForCode(code)
}
// GetCountryCodeForLanguage return country code for a language
// variable, e.g. "CN" will be return if passing "zh_CN.UTF-8".
func GetCountryCodeForLanguage(language string) (code string, err error) {
if !strings.Contains(language, "_") {
err = errLanguageFormatInvalid
return
}
var indexFrom, indexTo int
indexFrom = strings.Index(language, "_")
if strings.Contains(language, ".") {
indexTo = strings.Index(language, ".")
} else {
indexTo = len(language)
}
if indexFrom+1 >= indexTo {
err = errLanguageFormatInvalid
return
}
code = language[indexFrom+1 : indexTo]
return
}
// GetCountryNameForCode return country name that corresponding to the
// country code.
func GetCountryNameForCode(code string) (name string, err error) {
database, err := GetCountryDatabase()
if err != nil {
return
}
for _, entry := range database.Countries {
if strings.EqualFold(code, entry.Alpha2Code) {
name = DGettext("iso_3166", entry.Name)
break
}
}
if len(name) == 0 {
err = errCountryCodeInvalid
}
return
}
// GetAllCountryCode return all country code.
func GetAllCountryCode() (codeList []string, err error) {
database, err := GetCountryDatabase()
if err != nil {
return
}
for _, entry := range database.Countries {
codeList = append(codeList, entry.Alpha2Code)
}
return
}
// GetAllCountryNames return all country names.
func GetAllCountryNames() (nameList []string, err error) {
database, err := GetCountryDatabase()
if err != nil {
return
}
for _, entry := range database.Countries {
nameList = append(nameList, DGettext("iso_3166", entry.Name))
}
return
}
|