Example #1
0
func MustLoad() {
	// .env file
	err := godotenv.Load()
	if err != nil {
		log.Fatalf("Unable to load environment settings: %s", err)
	}

	// translations
	i18n.MustLoadTranslationFile("i18n/en-us.all.json")
	i18n.MustLoadTranslationFile("i18n/pt-br.all.json")
}
Example #2
0
func loadFromAsset(packageName, assetPath, locale, language string) error {
	assetName := locale + ".all.json"
	assetKey := filepath.Join(assetPath, language, packageName, assetName)

	byteArray, err := resources.Asset(assetKey)
	if err != nil {
		return err
	}

	if len(byteArray) == 0 {
		return errors.New(fmt.Sprintf("Could not load i18n asset: %v", assetKey))
	}

	tmpDir, err := ioutil.TempDir("", "cloudfoundry_cli_i18n_res")
	if err != nil {
		return err
	}
	defer func() {
		os.RemoveAll(tmpDir)
	}()

	fileName, err := saveLanguageFileToDisk(tmpDir, assetName, byteArray)
	if err != nil {
		return err
	}

	go_i18n.MustLoadTranslationFile(fileName)

	os.RemoveAll(fileName)

	return nil
}
Example #3
0
func main() {
	// Configure the i18n library to use the preferred language set in the CENOLANG environement variable
	setLanguage := os.Getenv("CENOLANG")
	if setLanguage == "" {
		os.Setenv("CENOLANG", "en-us")
		setLanguage = "en-us"
	}
	i18n.MustLoadTranslationFile("./translations/" + setLanguage + ".all.json")
	T, _ := i18n.Tfunc(setLanguage, "en-us")
	// Read an existing configuration file or have the user supply settings
	if conf, err := ReadConfigFile(CONFIG_FILE); err != nil {
		fmt.Println(T("no_config_cli", map[string]interface{}{"Location": CONFIG_FILE}))
		Configuration = GetConfigFromUser()
	} else {
		Configuration = conf
	}
	// Create an HTTP proxy server
	http.HandleFunc("/lookup", directHandler)
	http.HandleFunc("/", proxyHandler)
	fmt.Println(T("listening_msg_cli", map[string]interface{}{"Port": Configuration.PortNumber}))
	err := http.ListenAndServe(Configuration.PortNumber, nil)
	if err != nil {
		panic(err)
	}
}
Example #4
0
func Example() {
	i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-us.all.json")

	T, _ := i18n.Tfunc("en-US")

	bobMap := map[string]interface{}{"Person": "Bob"}
	bobStruct := struct{ Person string }{Person: "Bob"}

	fmt.Println(T("program_greeting"))
	fmt.Println(T("person_greeting", bobMap))
	fmt.Println(T("person_greeting", bobStruct))

	fmt.Println(T("your_unread_email_count", 0))
	fmt.Println(T("your_unread_email_count", 1))
	fmt.Println(T("your_unread_email_count", 2))
	fmt.Println(T("my_height_in_meters", "1.7"))

	fmt.Println(T("person_unread_email_count", 0, bobMap))
	fmt.Println(T("person_unread_email_count", 1, bobMap))
	fmt.Println(T("person_unread_email_count", 2, bobMap))
	fmt.Println(T("person_unread_email_count", 0, bobStruct))
	fmt.Println(T("person_unread_email_count", 1, bobStruct))
	fmt.Println(T("person_unread_email_count", 2, bobStruct))

	fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": T("d_days", 0),
	}))
	fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": T("d_days", 1),
	}))
	fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": T("d_days", 2),
	}))

	// Output:
	// Hello world
	// Hello Bob
	// Hello Bob
	// You have 0 unread emails.
	// You have 1 unread email.
	// You have 2 unread emails.
	// I am 1.7 meters tall.
	// Bob has 0 unread emails.
	// Bob has 1 unread email.
	// Bob has 2 unread emails.
	// Bob has 0 unread emails.
	// Bob has 1 unread email.
	// Bob has 2 unread emails.
	// Bob has 3 unread emails in the past 0 days.
	// Bob has 3 unread emails in the past 1 day.
	// Bob has 3 unread emails in the past 2 days.
}
Example #5
0
func InitTranslationsWithDir(dir string) {
	i18nDirectory := FindDir(dir)
	files, _ := ioutil.ReadDir(i18nDirectory)
	for _, f := range files {
		if filepath.Ext(f.Name()) == ".json" {
			filename := f.Name()
			locales[strings.Split(filename, ".")[0]] = i18nDirectory + filename
			i18n.MustLoadTranslationFile(i18nDirectory + filename)
		}
	}
}
Example #6
0
File: base.go Project: grr89/hrkb
func LoadLangs() {
	var langs []M.Lang
	l := &Lang{}
	err := DM.FindAll(&M.Lang{}, &langs, M.Sf{"Code"}, M.Where{}, M.NewParams(M.Params{Sort: "Code ASC"}))
	if err != nil {
		beego.Error(err)
		return
	}
	for _, v := range langs {
		i18n.MustLoadTranslationFile(l.langFileName("lang::folder", v.Code))
	}
}
// Init configures the localizaton page. Pass the valid path(s) to i18n
// JSON files as defined by the `github.com/nicksnyder/go-i18n` package.
func Init(files []string, defaultLocale string) (err error) {
	if len(files) == 0 {
		return ErrNoTranslationFiles
	}

	for _, path := range files {
		i18n.MustLoadTranslationFile(path)
	}

	T = NewTranslationFunc(defaultLocale)

	return nil
}
Example #8
0
func sysInit() {
	<-(gottp.SysInitChan) //Buffered Channel to receive the server upstart boolean

	config.InitTracer()
	log.Println("Initialized GoTracer")

	db.Conn = db.GetConn(
		config.Settings.Khabar.DBName,
		config.Settings.Khabar.DBAddress,
		config.Settings.Khabar.DBUsername,
		config.Settings.Khabar.DBPassword,
	)

	log.Println("Database Connected :" + config.Settings.Khabar.DBName + " " +
		"at address:" + config.Settings.Khabar.DBAddress)

	transDir := config.Settings.Khabar.TranslationDirectory

	if len(transDir) == 0 {
		cwd := os.Getenv("PWD")
		transDir = cwd + "/translations"
		config.Settings.Khabar.TranslationDirectory = cwd
	}

	log.Println("Directory for translation :" + transDir)

	filepath.Walk(transDir, func(path string, _ os.FileInfo, err error) error {
		fileExt := filepath.Ext(path)
		if fileExt == ".json" && err == nil {
			log.Println("Loading translation file:" + path)
			i18n.MustLoadTranslationFile(path)
		} else {
			log.Print("Skipping translation file:" + path + " " +
				"File Extension:" + fileExt + " ")
			if err != nil {
				log.Print("Error:" + err.Error())
			}
		}
		return nil
	})

	log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
	log.Println("Translations have been parsed.")
}
Example #9
0
// loadTranslationFiles loads the found translation files into the i18n
// messaging system for use by the application
func loadTranslationFiles(directory string) {
	// Read the directory
	fileInfos, err := ioutil.ReadDir(directory)
	if err != nil {
		tracelog.CompletedError(err, "localize", "loadTranslationFiles")
		return
	}

	// Look for JSON files
	for _, fileInfo := range fileInfos {
		if path.Ext(fileInfo.Name()) != ".json" {
			continue
		}

		fileName := fmt.Sprintf("%s/%s", directory, fileInfo.Name())

		tracelog.Info("localize", "loadTranslationFiles", "Loading %s", fileName)
		i18n.MustLoadTranslationFile(fileName)
	}
}
Example #10
0
func loadFromAsset(locale string) error {
	assetName := locale + ".all.json"
	assetKey := filepath.Join(GetResourcesPath(), assetName)

	byteArray, err := resources.Asset(assetKey)
	if err != nil {
		return err
	}

	if len(byteArray) == 0 {
		return errors.New(fmt.Sprintf("Could not load i18n asset: %v", assetKey))
	}

	_, err = os.Stat(os.TempDir())
	if err != nil {
		if !os.IsExist(err) {
			return errors.New("Please make sure Temp dir exist - " + os.TempDir())
		} else {
			return err
		}
	}

	tmpDir, err := ioutil.TempDir("", "cloudfoundry_cli_i18n_res")
	if err != nil {
		return err
	}
	defer func() {
		os.RemoveAll(tmpDir)
	}()

	fileName, err := saveLanguageFileToDisk(tmpDir, assetName, byteArray)
	if err != nil {
		return err
	}

	go_i18n.MustLoadTranslationFile(fileName)

	os.RemoveAll(fileName)

	return nil
}
Example #11
0
func Example_template() {
	i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-us.all.json")

	T, _ := i18n.Tfunc("en-US")
	tmpl.Funcs(map[string]interface{}{
		"T": T,
	})

	tmpl.Execute(os.Stdout, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": T("d_days", 1),
	})

	tmpl.Execute(os.Stdout, struct {
		Person    string
		Timeframe string
	}{
		Person:    "Bob",
		Timeframe: T("d_days", 1),
	})

	// Output:
	// Hello world
	// Hello Bob
	// You have 0 unread emails.
	// You have 1 unread email.
	// You have 2 unread emails.
	// Bob has 0 unread emails.
	// Bob has 1 unread email.
	// Bob has 2 unread emails.
	//
	// Hello world
	// Hello Bob
	// You have 0 unread emails.
	// You have 1 unread email.
	// You have 2 unread emails.
	// Bob has 0 unread emails.
	// Bob has 1 unread email.
	// Bob has 2 unread emails.
}
Example #12
0
func TestMain(m *testing.M) {

	c, err := config.NewConfig("ini", "../conf/app.conf")

	if err != nil {
		log.Fatal(err)
	}

	TestConfig, err := conf.Initialize("test", c)
	if err != nil {
		log.Fatal(err)
	}

	if err := DbOpen(TestConfig.Db); err != nil {
		log.Fatal(err)
	}

	defer DbClose()

	PrepareTables(&User{}, &Cand{}, &Rat{}, &Crit{}, &Dep{}, &Comment{}, &Role{}, &Contact{}, &Mail{})

	err = ClearTestData(&User{}, &Crit{}, &Rat{}, &Dep{})
	if err != nil {
		log.Fatal(err)
	}

	err = InsertTestData("../migrations/test_data/data.sql")
	if err != nil {
		log.Fatal(err)
	}

	i18n.MustLoadTranslationFile("../langs/en-US.all.json")
	T, _ = i18n.Tfunc("en-US", "en-US", "en-US")

	exitCode := m.Run()

	os.Exit(exitCode)
}
Example #13
0
func initI18N() {
	i18n.MustLoadTranslationFile("service/userService/locale/en-us.all.json")
	i18n.MustLoadTranslationFile("service/userService/locale/ko-kr.all.json")
}
Example #14
0
func Initializei18n() {
	i18nFile := "i18n/" + *lang + ".all.json"
	log.Printf("Loading i18n from [%s]", i18nFile)

	i18n.MustLoadTranslationFile(i18nFile)
}