Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no plural rule registered for zh-Hant #107

Closed
aitour opened this issue May 24, 2018 · 5 comments · Fixed by #109
Closed

no plural rule registered for zh-Hant #107

aitour opened this issue May 24, 2018 · 5 comments · Fixed by #109
Labels
bug Not working as intended.
Milestone

Comments

@aitour
Copy link

aitour commented May 24, 2018

hi,
I want i18n to translate between zh-Hans and zh-Hant. But the result was not expcted, and was always translated to zh-Hans. I follow the code and found code @bundle.go:98 returns an error.
return fmt.Errorf("no plural rule registered for %s", tag)

It seems no plural rule registered for zh-Hant. so the messages can't be registered.
Is this a bug?

@nicksnyder
Copy link
Owner

This should work. There is a plural rule for zh, which should match both zh-Hant and zh-Hans.

On bundle.go:96 either b.pluralRules doesn't contain the plural rule for zh for some reason, or there is a bug in the rules.go:10

If you could debug that would be helpful.

@nicksnyder nicksnyder added the bug Not working as intended. label May 25, 2018
@nicksnyder nicksnyder added this to the v2 milestone May 25, 2018
@aitour
Copy link
Author

aitour commented May 28, 2018

Thanks for your reply. I hacked the local code to work for my needs. But I'm don't know if this is a correct fix (the Parent()@golang.org/x/text/language/language.go:338 does not think of zh as parent of zh-Hant). Here's my [email protected]:10, which uses the base language to match:

func (r Rules) Rule(tag language.Tag) *Rule {
	for {
		if rule := r[tag]; rule != nil {
			return rule
		}
		tag = tag.Parent()
		if tag.IsRoot() {
			break
		}
	}

	base, _ := tag.Base()
	baseLang := language.MustParse(base.String())
	if rule := r[baseLang]; rule != nil {
		return rule
	}
	return nil
}

@nicksnyder
Copy link
Owner

Thanks for figuring this out. I am not sure if the behavior you are observing from golang.org/x/text/language is intentional or not so I filed golang/go#25603

Can you clarify what your use-case is? Your source translations are in zh-Hans and you are translating to zh-Hant?

@nicksnyder
Copy link
Owner

I pushed a fix to master. I found some bugs in your proposed workaround, so take a look.

@aitour
Copy link
Author

aitour commented May 29, 2018

Thanks for the fix and figuring the bug.
My use-case is translation in many languages include en, zh-Hans, zh-Hant and etc.
I simply use a map to find language.Tag

localesMap = map[string]language.Tag{
		"en":         language.English,
		"zh":         language.SimplifiedChinese,
		"zh-hans-cn": language.SimplifiedChinese,
		"zh-hans":    language.SimplifiedChinese,
		"zh-cn":      language.SimplifiedChinese,
		"zh-hant":    language.TraditionalChinese,
		"zh-tw":      language.TraditionalChinese,
		"ja":         language.Japanese,
		"fr":         language.French,
		"ru":         language.Russian,
		"es":         language.Spanish,
		"ko":         language.Korean,
		"ar":         language.Arabic,
	}

In the init proc I load the languages messages. But when langTag is language.TraditionalChinese, the AddMessage returns an error.

err = i18nBundle.AddMessages(langTag, localeMsgs...)
if err != nil {
	log.WithFields(log.Fields{"file": fi.Name(), "error": err}).Warn("add messages error")
}

When language is language.TraditionalChinese, the translation process returns SimplifiedChinese.

localizer := i18n.NewLocalizer(i18nBundle, language)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Not working as intended.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants