-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathTranslator.go
More file actions
102 lines (84 loc) · 3.08 KB
/
Translator.go
File metadata and controls
102 lines (84 loc) · 3.08 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
package giu
import (
"strings"
)
// Translator should be implemented by type that can translate a string to another string.
type Translator interface {
// Translate returns tranalted string
Translate(string) string
// SetLanguage changes language of the translation.
SetLanguage(string) error
}
// SetTranslator allows to change the default (&EmptyTranslator{})
// This will raise a panic if t is nil.
// Note that using translator will change labels of widgets,
// so this might affect internal imgui's state.
// See also Context.RegisterString.
func (c *GIUContext) SetTranslator(t Translator) {
Assert(t != nil, "Context", "SetTranslator", "Translator must not be nil.")
c.Translator = t
}
var _ Translator = &EmptyTranslator{}
// EmptyTranslator is the default one (to save resources).
// It does nothing.
type EmptyTranslator struct{}
// Translate implements Translator interface.
func (t *EmptyTranslator) Translate(s string) string {
return s
}
// SetLanguage implements Translator interface.
func (t *EmptyTranslator) SetLanguage(_ string) error {
return nil
}
var _ Translator = &BasicTranslator{}
// BasicTranslator is a simpliest implementation of translation mechanism.
// This is NOT thread-safe yet. If you need thread-safety, write your own implementation.
//
// It is supposed to be used in the following way:
// - create translator
// - add languages (tip: you can use empty map for default language)
// - set translator in Context
// - set default language
// - write your UI as always.
type BasicTranslator struct {
// language tag -> key -> value
source map[string]map[string]string
currentLanguage string
}
// NewBasicTranslator creates a new BasicTranslator with the given language tag.
func NewBasicTranslator() *BasicTranslator {
return &BasicTranslator{}
}
// Translate implements Translator interface.
// It translates s to the current language, under the following conditions:
// - If s is empty, an empty string is returned with no further processing.
// - If t.currentLanguage is empty, a panic will be raised.
// - If t.currentLanguage is not in a.source, BasicTranslator raises panic.
// - If s is not in source[currentLanguage], s is returned as-is.
func (t *BasicTranslator) Translate(s string) string {
s = strings.Split(s, "##")[0]
if s == "" {
return ""
}
Assert(t.currentLanguage != "", "BasicTranslator", "Translate", "Current language is not set, so there is no sense in using BasicTranslator.")
locale, ok := t.source[t.currentLanguage]
Assert(ok, "BasicTranslator", "Translate", "There is no language tag %s known by the translator. Did you add it?", t.currentLanguage)
translated, ok := locale[s]
if !ok {
return s
}
return translated
}
// SetLanguage sets the current language of the translator.
func (t *BasicTranslator) SetLanguage(tag string) error {
t.currentLanguage = tag
return nil
}
// AddLanguage adds a new "dictionary" to the translator.
func (t *BasicTranslator) AddLanguage(tag string, source map[string]string) *BasicTranslator {
if t.source == nil {
t.source = make(map[string]map[string]string)
}
t.source[tag] = source
return t
}