Example #1
0
func supportedLanguages() []*language.Language {
	assetNames := resources.AssetNames()
	languages := []*language.Language{}

	for _, assetName := range assetNames {
		assetLocale := strings.TrimSuffix(path.Base(assetName), resourceSuffix)
		languages = append(languages, language.Parse(assetLocale)...)
	}

	return languages
}
Example #2
0
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(l.Tag)

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

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

	return defaultTfunc
}
Example #3
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
}