This repository was archived by the owner on Sep 6, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlib.go
More file actions
113 lines (102 loc) · 2.91 KB
/
Copy pathlib.go
File metadata and controls
113 lines (102 loc) · 2.91 KB
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
/*
Onix Config Manager - Artisan
Copyright (c) 2018-Present by www.gatblau.org
Licensed under the Apache License, Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0
Contributors to this project, hereby assign copyright in this code to the project,
to be licensed under the same terms as the rest of the code.
*/
package i18n
import (
"fmt"
"github.com/gatblau/onix/artisan/core"
"github.com/pelletier/go-toml"
"io/ioutil"
"os"
"path"
"strings"
)
func String(artHome string, key I18NKey) string {
return get(artHome, key)
}
// Printf prints a localised message
func Printf(artHome string, key I18NKey, a ...interface{}) {
fmt.Printf(get(artHome, key), a...)
}
// Sprintf formats according to a format specifier and returns the resulting string
func Sprintf(artHome string, key I18NKey, a ...interface{}) string {
return fmt.Sprintf(get(artHome, key), a...)
}
// Err checks for the error and if it exists prints a localised error
func Err(artHome string, err error, key I18NKey, a ...interface{}) {
if err != nil {
fmt.Printf("%s - %s\n", fmt.Sprintf(get(artHome, key), a...), err)
os.Exit(1)
}
}
// raise an error
func Raise(artHome string, key I18NKey, a ...interface{}) {
fmt.Printf("%s\n", fmt.Sprintf(get(artHome, key), a...))
os.Exit(1)
}
// updates a specific i18n file by adding missing keys but keeping their value in english
func Update(i18nFile string) error {
file := core.ToAbs(i18nFile)
f, err := toml.LoadFile(file)
if err != nil {
return err
}
for key, value := range msg_en {
if !f.Has(string(key)) {
f.Set(string(key), value)
}
}
data, err := f.Marshal()
if err != nil {
return err
}
return ioutil.WriteFile(file, data, os.ModePerm)
}
func get(artHome string, key I18NKey) string {
var language string
// check if internationalisation is enabled
inter := os.Getenv("ARTISAN_I18N")
// if not then use english
if len(inter) == 0 {
core.Debug("i18n is disabled, to enable set ARTISAN_I18N")
return msg_en[key]
}
// check if an overriding language has been set
overrideLang := os.Getenv("ARTISAN_LANG")
// if not
if len(overrideLang) == 0 {
// use the current user language
language = lang()
} else {
// use the overriding language
language = overrideLang
}
// load the dictionary from file
t, err := toml.LoadFile(path.Join(core.LangPath(artHome), fmt.Sprintf("%s_i18n.toml", language)))
var value interface{}
if err == nil {
value = t.Get(string(key))
if value == nil {
// set value in english
value = msg_en[key]
}
} else {
value = msg_en[key]
}
return value.(string)
}
func splitLocale(locale string) (language string, territory string) {
formattedLocale := strings.Split(locale, ".")[0]
formattedLocale = strings.Replace(formattedLocale, "-", "_", -1)
pieces := strings.Split(formattedLocale, "_")
language = pieces[0]
territory = ""
if len(pieces) > 1 {
territory = strings.Split(formattedLocale, "_")[1]
}
return language, territory
}