Language selection combobox in options and accepted languages.

Added language selection combobox in options dialog. Selecting
language here will overwrite language code gathered from system.

Added AcceptedLanguages map to Translator, which holds readable
names and lang-country codes with avaailable languages. Filled
it with languages in which URTrator already translated. Francais
coming :)
This commit is contained in:
Stanislav Nikitin 2017-02-09 02:31:24 +05:00
parent 4890882021
commit 9273273b49
5 changed files with 68 additions and 13 deletions

View File

@ -116,16 +116,16 @@ func (ctx *Context) initializeTimer() {
}
func (ctx *Context) initializeTranslator() {
ctx.Translator = translator.New()
ctx.Translator = translator.New(ctx.Cfg)
ctx.Translator.Initialize()
}
func (ctx *Context) Initialize() {
fmt.Println("Initializing application context...")
ctx.initializeTranslator()
ctx.initializeColorizer()
ctx.initializeConfig()
ctx.initializeDatabase()
ctx.initializeTranslator()
ctx.initializeEventer()
ctx.initializeCache()
ctx.initializeLauncher()

View File

@ -9,7 +9,18 @@
// ToDo: put full text of license here.
package translator
func New() *Translator {
import (
// local
"github.com/pztrn/urtrator/configuration"
)
var (
// Configuration.
cfg *configuration.Config
)
func New(c *configuration.Config) *Translator {
cfg = c
t := Translator{}
return &t
}

View File

@ -23,6 +23,8 @@ import (
)
type Translator struct {
// Accepted languages.
AcceptedLanguages map[string]string
// Currently active language.
Language string
// Translations.
@ -59,6 +61,13 @@ func (t *Translator) formatFromMap(data string, params map[string]string) string
func (t *Translator) Initialize() {
fmt.Println("Initializing translations...")
t.AcceptedLanguages = map[string]string{
"System's default language": "default",
"English": "en_US",
"French": "fr_FR",
"Russian": "ru_RU",
}
// Initialize storages.
t.translations = make(map[string]map[string]string)
t.translationsPath = ""
@ -85,11 +94,20 @@ func (t *Translator) loadTranslations() {
t.translations[t.Language] = make(map[string]string)
if t.translationsPath != "" {
files_list, _ := ioutil.ReadDir(t.translationsPath)
// Check if language was selected in options dialog. In that
// case it will overwrite autodetected language.
var translationsDir string = ""
if cfg.Cfg["/general/language"] != "" {
translationsDir = filepath.Join(t.translationsPath, cfg.Cfg["/general/language"])
t.Language = cfg.Cfg["/general/language"]
} else {
translationsDir = filepath.Join(t.translationsPath, t.Language)
}
files_list, _ := ioutil.ReadDir(translationsDir)
if len(files_list) > 0 {
for i := range files_list {
// Read file.
file_path := filepath.Join(t.translationsPath, files_list[i].Name())
file_path := filepath.Join(translationsDir, files_list[i].Name())
file_data, _ := ioutil.ReadFile(file_path)
var trans map[string]string
json.Unmarshal(file_data, &trans)

View File

@ -73,6 +73,7 @@ func (t *Translator) detectTranslationsDirectory() error {
if err != nil {
t.Language = "en_US"
fmt.Println("Translations unavailable, forcing en_US language code")
return errors.New("No translations directory was detected!")
} else {
t.translationsPath = "/usr/share/urtrator/translations"
}
@ -80,11 +81,5 @@ func (t *Translator) detectTranslationsDirectory() error {
t.translationsPath = translations_dir
}
if t.translationsPath != "" {
t.translationsPath = filepath.Join(t.translationsPath, t.Language)
} else {
return errors.New("No translations directory was detected!")
}
return nil
}

View File

@ -36,6 +36,9 @@ type OptionsDialog struct {
show_tray_icon *gtk.CheckButton
// Enable autoupdate checkbutton.
autoupdate *gtk.CheckButton
// Appearance tab.
// Language to use.
language_combo *gtk.ComboBoxText
// Urban Terror tab.
// Profiles list.
profiles_list *gtk.TreeView
@ -73,6 +76,7 @@ func (o *OptionsDialog) closeOptionsDialogWithSaving() {
fmt.Println("Saving changes to options...")
o.saveGeneral()
o.saveAppearance()
// Temporary disable all these modals on Linux.
// See https://github.com/mattn/go-gtk/issues/289.
@ -167,9 +171,32 @@ func (o *OptionsDialog) fill() {
func (o *OptionsDialog) initializeAppearanceTab() {
appearance_vbox := gtk.NewVBox(false, 0)
l := gtk.NewLabel(ctx.Translator.Translate("There will be some appearance configuration options soon.", nil))
appearance_vbox.PackStart(l, false, true, 5)
appearance_table := gtk.NewTable(1, 2, false)
language_selection_tooltip := ctx.Translator.Translate("Language which URTrator will use.\n\nChanging this requires URTrator restart!", nil)
language_selection_label := gtk.NewLabel(ctx.Translator.Translate("Language:", nil))
language_selection_label.SetAlignment(0, 0)
language_selection_label.SetTooltipText(language_selection_tooltip)
appearance_table.Attach(language_selection_label, 0, 1, 0, 1, gtk.FILL, gtk.SHRINK, 5, 5)
o.language_combo = gtk.NewComboBoxText()
o.language_combo.SetTooltipText(language_selection_tooltip)
// Get all available languages and fill combobox.
lang_idx := 0
var lang_active int = 0
for lang, _ := range ctx.Translator.AcceptedLanguages {
o.language_combo.AppendText(lang)
if ctx.Translator.AcceptedLanguages[lang] == ctx.Cfg.Cfg["/general/language"] {
lang_active = lang_idx
}
lang_idx += 1
}
o.language_combo.SetActive(lang_active)
appearance_table.Attach(o.language_combo, 1, 2, 0, 1, gtk.FILL, gtk.FILL, 5, 5)
appearance_vbox.PackStart(appearance_table, false, true, 0)
o.tab_widget.AppendPage(appearance_vbox, gtk.NewLabel(ctx.Translator.Translate("Appearance", nil)))
}
@ -344,6 +371,10 @@ func (o *OptionsDialog) loadProfiles(data map[string]string) {
}
}
func (o *OptionsDialog) saveAppearance() {
ctx.Cfg.Cfg["/general/language"] = ctx.Translator.AcceptedLanguages[o.language_combo.GetActiveText()]
}
func (o *OptionsDialog) saveGeneral() {
if o.show_tray_icon.GetActive() {
ctx.Cfg.Cfg["/general/show_tray_icon"] = "1"