Пример #1
0
// Loads translation files and inits L10n func that retrieves the translations.
// l10nDir is a name of a directory with translations.
// userLocale specifies a locale preferred by the user (a preference or accept
// header or language cookie).
func InitL10n(assets *AssetBin, userLocale string) {
	loadLanguage(assets, "en-US.all.json")
	loadLanguage(assets, "lt-LT.all.json")
	defaultLocale := "en-US" // known valid locale
	L10n = i18n.MustTfunc(userLocale, defaultLocale)
	addTemplateFunc("L10n", L10n)
}
Пример #2
0
func InitWithLocale(locale string) {
	err := loadFromAsset(locale)
	if err != nil {
		panic(err)
	}
	T = goi18n.MustTfunc(locale)
}
Пример #3
0
func getDefaultLocal() (i18n.TranslateFunc, error) {
	err := loadAsset("cf/i18n/resources/" + defaultLocale + resourceSuffix)
	if err != nil {
		return nil, err
	}

	return i18n.MustTfunc(defaultLocale), nil
}
func Init(config LocalReader) go_i18n.TranslateFunc {
	loadAsset("cf/i18n/resources/" + defaultLocale + resourceSuffix)
	defaultTfunc := go_i18n.MustTfunc(defaultLocale)

	assetNames := resources.AssetNames()

	sources := []string{
		config.Locale(),
		os.Getenv(lcAll),
		os.Getenv(lang),
	}

	for _, source := range sources {
		if source == "" {
			continue
		}

		for _, l := range language.Parse(source) {
			if l.Tag == zhTW || l.Tag == zhHK {
				l.Tag = zhHant
			}

			for _, assetName := range assetNames {
				assetLocale := strings.ToLower(strings.Replace(path.Base(assetName), underscore, hyphen, -1))
				if strings.HasPrefix(assetLocale, l.Tag) {
					loadAsset(assetName)

					t := go_i18n.MustTfunc(source)

					return func(translationID string, args ...interface{}) string {
						if translated := t(translationID, args...); translated != translationID {
							return translated
						}

						return defaultTfunc(translationID, args...)
					}
				}
			}
		}
	}

	return defaultTfunc
}
Пример #5
0
func getConfiguredLocal(config Config) (i18n.TranslateFunc, error) {
	source := config.Locale()
	assetNames := resources.AssetNames()

	for _, l := range language.Parse(source) {
		if l.Tag == zhTW || l.Tag == zhHK {
			l.Tag = zhHant
		}

		for _, assetName := range assetNames {
			assetLocale := strings.ToLower(strings.Replace(path.Base(assetName), underscore, hyphen, -1))
			if strings.HasPrefix(assetLocale, l.Tag) {
				err := loadAsset(assetName)
				if err != nil {
					return nil, err
				}

				return i18n.MustTfunc(l.Tag), nil
			}
		}
	}

	return nil, nil
}
Пример #6
0
// NewTranslationFunc returns a new translation function
func NewTranslationFunc(locale string) i18n.TranslateFunc {
	return i18n.MustTfunc(locale)
}