Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Nov 28, 2024
1 parent 90e0c00 commit 6b5b4ec
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c *GIUContext) SetDirty() {
// It does the following:
// - adds a string to the FontAtlas
// - translates the string with the Translator set in the context
// Not all widgets will use this. Text with user-defined input (e.g. InputText will still use FontAtlas.RegisterString)
// Not all widgets will use this. Text with user-defined input (e.g. InputText will still use FontAtlas.RegisterString).
func (c *GIUContext) PrepareString(str string) string {
str = c.Translator.Translate(str)
return c.FontAtlas.RegisterString(str)
Expand Down
8 changes: 5 additions & 3 deletions Translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Translator interface {
// 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
// See also Context.RegisterString.
func (c *GIUContext) SetTranslator(t Translator) {
Assert(t != nil, "Context", "SetTranslator", "Translator must not be nil.")
c.Translator = t
Expand All @@ -25,13 +25,15 @@ func (c *GIUContext) SetTranslator(t Translator) {
var _ Translator = &EmptyTranslator{}

// EmptyTranslator is the default one (to save resources).
// It does nothing
// 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
}
Expand Down Expand Up @@ -70,7 +72,7 @@ func (t *BasicTranslator) Translate(s string) string {
return ""
}

Assert(t.currentLanguage != "", "BasicTranslator", "Translate", "Current language is not set, so ther is no sense in using BasicTranslator.")
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)

Expand Down
13 changes: 9 additions & 4 deletions examples/translation/translation.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main shows using of translations in giu.
package main

import "github.com/AllenDang/giu"
Expand All @@ -15,14 +16,16 @@ var (
},
}

languageCodes = []string{"en", "pl", "de"}
currentLang int32 = 0
languageCodes = []string{"en", "pl", "de"}
currentLang int32
)

func loop() {
giu.SingleWindow().Layout(
giu.Combo("Select language", languageCodes[currentLang], languageCodes, &currentLang).OnChange(func() {
giu.Context.Translator.SetLanguage(languageCodes[currentLang])
if err := giu.Context.Translator.SetLanguage(languageCodes[currentLang]); err != nil {
panic(err)
}
}),
giu.Label("Hello world!"),
)
Expand All @@ -36,7 +39,9 @@ func main() {
translator.AddLanguage(k, v)
}

translator.SetLanguage(languageCodes[currentLang])
if err := translator.SetLanguage(languageCodes[currentLang]); err != nil {
panic(err)
}

giu.Context.SetTranslator(translator)

Expand Down

0 comments on commit 6b5b4ec

Please sign in to comment.