gofmt.
This commit is contained in:
@@ -14,23 +14,23 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
// stdlib
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Translator struct {
|
||||
// Accepted languages.
|
||||
AcceptedLanguages map[string]string
|
||||
// Currently active language.
|
||||
Language string
|
||||
// Translations.
|
||||
translations map[string]map[string]string
|
||||
// Path to translations files.
|
||||
translationsPath string
|
||||
// Accepted languages.
|
||||
AcceptedLanguages map[string]string
|
||||
// Currently active language.
|
||||
Language string
|
||||
// Translations.
|
||||
translations map[string]map[string]string
|
||||
// Path to translations files.
|
||||
translationsPath string
|
||||
}
|
||||
|
||||
// Formats string from passed map.
|
||||
@@ -50,73 +50,73 @@ type Translator struct {
|
||||
// Also note that we will replace ALL occurences of "{{ VAR }}" within string!
|
||||
// All untranslated variables will not be touched at all.
|
||||
func (t *Translator) formatFromMap(data string, params map[string]string) string {
|
||||
new_data := data
|
||||
for k, v := range params {
|
||||
new_data = strings.Replace(new_data, "{{ " + k + " }}", v, -1)
|
||||
}
|
||||
return new_data
|
||||
new_data := data
|
||||
for k, v := range params {
|
||||
new_data = strings.Replace(new_data, "{{ "+k+" }}", v, -1)
|
||||
}
|
||||
return new_data
|
||||
}
|
||||
|
||||
// Translator initialization.
|
||||
func (t *Translator) Initialize() {
|
||||
fmt.Println("Initializing translations...")
|
||||
fmt.Println("Initializing translations...")
|
||||
|
||||
t.AcceptedLanguages = map[string]string{
|
||||
"System's default language": "default",
|
||||
"English": "en_US",
|
||||
"French": "fr_FR",
|
||||
"Russian": "ru_RU",
|
||||
}
|
||||
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 = ""
|
||||
// Initialize storages.
|
||||
t.translations = make(map[string]map[string]string)
|
||||
t.translationsPath = ""
|
||||
|
||||
// Getting locale name from environment.
|
||||
// ToDo: Windows compatability. Possible reference:
|
||||
// https://github.com/cloudfoundry-attic/jibber_jabber/blob/master/jibber_jabber_windows.go
|
||||
t.detectLanguage()
|
||||
// Getting locale name from environment.
|
||||
// ToDo: Windows compatability. Possible reference:
|
||||
// https://github.com/cloudfoundry-attic/jibber_jabber/blob/master/jibber_jabber_windows.go
|
||||
t.detectLanguage()
|
||||
|
||||
fmt.Println("Using translations for '" + t.Language + "'")
|
||||
fmt.Println("Using translations for '" + t.Language + "'")
|
||||
|
||||
err := t.detectTranslationsDirectory()
|
||||
if err == nil {
|
||||
t.loadTranslations()
|
||||
} else {
|
||||
fmt.Println("Skipping translations loading due to missing translations directory.")
|
||||
}
|
||||
err := t.detectTranslationsDirectory()
|
||||
if err == nil {
|
||||
t.loadTranslations()
|
||||
} else {
|
||||
fmt.Println("Skipping translations loading due to missing translations directory.")
|
||||
}
|
||||
}
|
||||
|
||||
// Load translations into memory.
|
||||
func (t *Translator) loadTranslations() {
|
||||
fmt.Println("Loading translations for language " + t.Language)
|
||||
fmt.Println("Translations directory: " + t.translationsPath)
|
||||
fmt.Println("Loading translations for language " + t.Language)
|
||||
fmt.Println("Translations directory: " + t.translationsPath)
|
||||
|
||||
t.translations[t.Language] = make(map[string]string)
|
||||
t.translations[t.Language] = make(map[string]string)
|
||||
|
||||
if 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(translationsDir, files_list[i].Name())
|
||||
file_data, _ := ioutil.ReadFile(file_path)
|
||||
var trans map[string]string
|
||||
json.Unmarshal(file_data, &trans)
|
||||
// Assign parsed translations to language code.
|
||||
t.translations[t.Language] = trans
|
||||
}
|
||||
}
|
||||
}
|
||||
if 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(translationsDir, files_list[i].Name())
|
||||
file_data, _ := ioutil.ReadFile(file_path)
|
||||
var trans map[string]string
|
||||
json.Unmarshal(file_data, &trans)
|
||||
// Assign parsed translations to language code.
|
||||
t.translations[t.Language] = trans
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actual translation function.
|
||||
@@ -129,18 +129,18 @@ func (t *Translator) loadTranslations() {
|
||||
// Translates passed data from loaded translations file.
|
||||
// Returns passed data without changes if translation wasn't found.
|
||||
func (t *Translator) Translate(data string, params map[string]string) string {
|
||||
val, ok := t.translations[t.Language][data]
|
||||
if !ok {
|
||||
if params != nil && len(params) > 0 {
|
||||
return t.formatFromMap(data, params)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
val, ok := t.translations[t.Language][data]
|
||||
if !ok {
|
||||
if params != nil && len(params) > 0 {
|
||||
return t.formatFromMap(data, params)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
if params != nil && len(params) > 0 {
|
||||
return t.formatFromMap(val, params)
|
||||
}
|
||||
if params != nil && len(params) > 0 {
|
||||
return t.formatFromMap(val, params)
|
||||
}
|
||||
|
||||
return val
|
||||
return val
|
||||
}
|
||||
|
@@ -22,64 +22,64 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
// stdlib
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Detect language on Unices.
|
||||
func (t *Translator) detectLanguage() {
|
||||
// Use LC_ALL first.
|
||||
t.Language = os.Getenv("LC_ALL")
|
||||
// If LC_ALL is empty - use LANG.
|
||||
if t.Language == "" {
|
||||
t.Language = os.Getenv("LANG")
|
||||
}
|
||||
// Use LC_ALL first.
|
||||
t.Language = os.Getenv("LC_ALL")
|
||||
// If LC_ALL is empty - use LANG.
|
||||
if t.Language == "" {
|
||||
t.Language = os.Getenv("LANG")
|
||||
}
|
||||
|
||||
// If still nothing - force "en_US" as default locale. Otherwise
|
||||
// split language string by "." and take first part.
|
||||
// Note: en_US is a default thing, so you will not found anything
|
||||
// in "translations" directory!
|
||||
if t.Language == "" {
|
||||
fmt.Println("No locale data for current user found, using default (en_US)...")
|
||||
t.Language = "en_US"
|
||||
} else {
|
||||
t.Language = strings.Split(t.Language, ".")[0]
|
||||
}
|
||||
// If still nothing - force "en_US" as default locale. Otherwise
|
||||
// split language string by "." and take first part.
|
||||
// Note: en_US is a default thing, so you will not found anything
|
||||
// in "translations" directory!
|
||||
if t.Language == "" {
|
||||
fmt.Println("No locale data for current user found, using default (en_US)...")
|
||||
t.Language = "en_US"
|
||||
} else {
|
||||
t.Language = strings.Split(t.Language, ".")[0]
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Translator) detectTranslationsDirectory() error {
|
||||
// Try to use directory near binary.
|
||||
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
// ..which can be overriden by URTRATOR_BINDIR environment variable.
|
||||
// Useful for developers.
|
||||
envdir := os.Getenv("URTRATOR_BINDIR")
|
||||
if envdir != "" {
|
||||
dir = envdir
|
||||
}
|
||||
// Try to use directory near binary.
|
||||
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
// ..which can be overriden by URTRATOR_BINDIR environment variable.
|
||||
// Useful for developers.
|
||||
envdir := os.Getenv("URTRATOR_BINDIR")
|
||||
if envdir != "" {
|
||||
dir = envdir
|
||||
}
|
||||
|
||||
translations_dir := filepath.Join(dir, "translations")
|
||||
_, err := os.Stat(translations_dir)
|
||||
if err != nil {
|
||||
fmt.Println("Translations wasn't found near binary!")
|
||||
// As we're using JSON translation storage, it should be
|
||||
// put in /usr/share/urtrator/translations by package
|
||||
// maintainers in distros.
|
||||
fmt.Println("Trying /usr/share/urtrator/translations...")
|
||||
_, err := os.Stat("/usr/share/urtrator/translations")
|
||||
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"
|
||||
}
|
||||
} else {
|
||||
t.translationsPath = translations_dir
|
||||
}
|
||||
translations_dir := filepath.Join(dir, "translations")
|
||||
_, err := os.Stat(translations_dir)
|
||||
if err != nil {
|
||||
fmt.Println("Translations wasn't found near binary!")
|
||||
// As we're using JSON translation storage, it should be
|
||||
// put in /usr/share/urtrator/translations by package
|
||||
// maintainers in distros.
|
||||
fmt.Println("Trying /usr/share/urtrator/translations...")
|
||||
_, err := os.Stat("/usr/share/urtrator/translations")
|
||||
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"
|
||||
}
|
||||
} else {
|
||||
t.translationsPath = translations_dir
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
@@ -22,23 +22,23 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"os"
|
||||
// stdlib
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Detect language on Windows.
|
||||
func (t *Translator) detectLanguage() {
|
||||
fmt.Println("ToDo! Forcing en_US for now!")
|
||||
t.Language = "en_US"
|
||||
fmt.Println("ToDo! Forcing en_US for now!")
|
||||
t.Language = "en_US"
|
||||
}
|
||||
|
||||
func (t *Translator) detectTranslationsDirectory() error {
|
||||
// Translations MUST reside in directory neear binary!
|
||||
// ToDo: more checks.
|
||||
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
t.translationsPath = filepath.Join(dir, "translations")
|
||||
// Translations MUST reside in directory neear binary!
|
||||
// ToDo: more checks.
|
||||
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
t.translationsPath = filepath.Join(dir, "translations")
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user