本文整理匯總了Golang中github.com/nicksnyder/go-i18n/i18n/language.Parse函數的典型用法代碼示例。如果您正苦於以下問題:Golang Parse函數的具體用法?Golang Parse怎麽用?Golang Parse使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Parse函數的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: translatedLanguage
func (b *Bundle) translatedLanguage(src string) *language.Language {
langs := language.Parse(src)
for _, lang := range langs {
if len(b.translations[lang.Tag]) > 0 {
return lang
}
}
return nil
}
示例2: execute
func (mc *mergeCommand) execute() error {
if len(mc.translationFiles) < 1 {
return fmt.Errorf("need at least one translation file to parse")
}
if lang := language.Parse(mc.sourceLanguage); lang == nil {
return fmt.Errorf("invalid source locale: %s", mc.sourceLanguage)
}
marshal, err := newMarshalFunc(mc.format)
if err != nil {
return err
}
bundle := bundle.New()
for _, tf := range mc.translationFiles {
if err := bundle.LoadTranslationFile(tf); err != nil {
return fmt.Errorf("failed to load translation file %s because %s\n", tf, err)
}
}
translations := bundle.Translations()
sourceLanguageTag := language.NormalizeTag(mc.sourceLanguage)
sourceTranslations := translations[sourceLanguageTag]
if sourceTranslations == nil {
return fmt.Errorf("no translations found for source locale %s", sourceLanguageTag)
}
for translationID, src := range sourceTranslations {
for _, localeTranslations := range translations {
if dst := localeTranslations[translationID]; dst == nil || reflect.TypeOf(src) != reflect.TypeOf(dst) {
localeTranslations[translationID] = src.UntranslatedCopy()
}
}
}
for localeID, localeTranslations := range translations {
lang := language.MustParse(localeID)[0]
all := filter(localeTranslations, func(t translation.Translation) translation.Translation {
return t.Normalize(lang)
})
if err := mc.writeFile("all", all, localeID, marshal); err != nil {
return err
}
untranslated := filter(localeTranslations, func(t translation.Translation) translation.Translation {
if t.Incomplete(lang) {
return t.Normalize(lang).Backfill(sourceTranslations[t.ID()])
}
return nil
})
if err := mc.writeFile("untranslated", untranslated, localeID, marshal); err != nil {
return err
}
}
return nil
}
示例3: supportedLanguages
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
}
示例4: IsSupportedLocale
func IsSupportedLocale(locale string) bool {
for _, supportedLanguage := range supportedLanguages() {
for _, l := range language.Parse(locale) {
if supportedLanguage.String() == l.String() {
return true
}
}
}
return false
}
示例5: GetFallbackLanguage
// GetFallbackLanguage tries to obtain a requested language via the session,
// or the Accept-Language request header, or the LANG or LC_MESSAGES
// environment variables
func GetFallbackLanguage(c context.Context, r *http.Request, fallback ...string) string {
if val, ok := c.Get(r, context.BaseCtxKey("session")); ok {
sess := val.(context.Session)
if language, ok := sess.Get("language"); ok {
return language.(string)
}
}
langs := lng.Parse(r.Header.Get("Accept-Language"))
if len(langs) > 0 {
return langs[0].String()
}
language := os.Getenv("LANG")
if language == "" {
language = os.Getenv("LC_MESSAGES")
language = localeRegexp.ReplaceAllLiteralString(language, "")
}
if language == "" {
if len(fallback) > 0 {
language = fallback[0]
} else {
language = "en"
}
} else {
langs := lng.Parse(language)
if len(langs) > 0 {
return langs[0].String()
}
}
return language
}
示例6: ParseTranslationFileBytes
// ParseTranslationFileBytes is similar to LoadTranslationFile except it parses the bytes in buf.
//
// It is useful for parsing translation files embedded with go-bindata.
func (b *Bundle) ParseTranslationFileBytes(filename string, buf []byte) error {
basename := filepath.Base(filename)
langs := language.Parse(basename)
switch l := len(langs); {
case l == 0:
return fmt.Errorf("no language found in %q", basename)
case l > 1:
return fmt.Errorf("multiple languages found in filename %q: %v; expected one", basename, langs)
}
translations, err := parseTranslations(filename, buf)
if err != nil {
return err
}
b.AddTranslation(langs[0], translations...)
return nil
}
示例7: Init
func Init(config core_config.Reader) go_i18n.TranslateFunc {
sources := []string{
config.Locale(),
os.Getenv(lcAll),
os.Getenv(lang),
defaultLocale,
}
assetNames := resources.AssetNames()
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) {
assetBytes, err := resources.Asset(assetName)
if err != nil {
panic(fmt.Sprintf("Could not load asset '%s': %s", assetName, err.Error()))
}
err = go_i18n.ParseTranslationFileBytes(assetName, assetBytes)
if err != nil {
panic(fmt.Sprintf("Could not load translations '%s': %s", assetName, err.Error()))
}
T, err := go_i18n.Tfunc(source)
if err == nil {
return T
}
}
}
}
}
panic("Unable to find suitable translation")
}
示例8: Init
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
}
示例9: getConfiguredLocal
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
}