From 9273273b49ef12f878783fc965870d40e37fa3cb Mon Sep 17 00:00:00 2001 From: Stanislav Nikitin Date: Thu, 9 Feb 2017 02:31:24 +0500 Subject: [PATCH] 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 :) --- context/context_object.go | 4 ++-- translator/exported.go | 13 ++++++++++++- translator/translator.go | 22 ++++++++++++++++++++-- translator/translator_unix.go | 7 +------ ui/gtk2/options.go | 35 +++++++++++++++++++++++++++++++++-- 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/context/context_object.go b/context/context_object.go index dc3fdf4..9b9bc7a 100644 --- a/context/context_object.go +++ b/context/context_object.go @@ -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() diff --git a/translator/exported.go b/translator/exported.go index b51b540..a182d0e 100644 --- a/translator/exported.go +++ b/translator/exported.go @@ -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 } diff --git a/translator/translator.go b/translator/translator.go index 0c12674..ec08831 100644 --- a/translator/translator.go +++ b/translator/translator.go @@ -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) diff --git a/translator/translator_unix.go b/translator/translator_unix.go index 0d33dfd..58b7317 100644 --- a/translator/translator_unix.go +++ b/translator/translator_unix.go @@ -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 } diff --git a/ui/gtk2/options.go b/ui/gtk2/options.go index 6fceabc..76c6ef0 100644 --- a/ui/gtk2/options.go +++ b/ui/gtk2/options.go @@ -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"