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

How to translate strings with template syntax? #275

Closed
misitebao opened this issue Jul 2, 2022 · 5 comments · Fixed by #317
Closed

How to translate strings with template syntax? #275

misitebao opened this issue Jul 2, 2022 · 5 comments · Fixed by #317

Comments

@misitebao
Copy link
Contributor

misitebao commented Jul 2, 2022

The requirement is to return different template strings according to different languages, but not to parse the template.

# en.toml
Name = """
My name is {{ .Name}}.
"""
# zh.toml
Name = """
我的名字是 {{ .Name}}。
"""

Want to return different strings based on different languages, e.g. My name is {{ .Name}}. or 我的名字是 {{ .Name}}。

@misitebao
Copy link
Contributor Author

@nicksnyder
What do you think about this issue?

@nicksnyder
Copy link
Owner

What is your use case for this?

Would it work to export the getMessageTemplate on the Bundle struct?

go-i18n/v2/i18n/bundle.go

Lines 138 to 144 in 57bd175

func (b *Bundle) getMessageTemplate(tag language.Tag, id string) *MessageTemplate {
templates := b.messageTemplates[tag]
if templates == nil {
return nil
}
return templates[id]
}

@misitebao
Copy link
Contributor Author

misitebao commented Jul 9, 2022

@nicksnyder
thank you for your reply 🙏

I want to translate a string, but it comes with template syntax, using the example according to the documentation, it gives an error:

en.toml
# en.toml
HelpTemplate = """
{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}{{end}}

Usag:{{if .Runnable}}
  {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}

Aliases:
  {{.NameAndAliases}}{{end}}{{if .HasExample}}

Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}

Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand)}}
  {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}

Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
  {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}

"""
zh-Hans.toml
# zh-Hans.toml
HelpTemplate = """
{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}{{end}}

使用方法:{{if .Runnable}}
  {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}

别名:
  {{.NameAndAliases}}{{end}}{{if .HasExample}}

Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}

Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand)}}
  {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}

Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
  {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}

"""

error:

$ go run ./cmd/yakia/main.go 
panic: template: :1: function "trimTrailingWhitespaces" not defined

......
exit status 2

Because the translated string has template syntax in it, go-i18n will parse it, but I just want to use it as a string, and don't want the template syntax in it to be parsed. I don't know what to do.

The method you provided can solve my problem, but may need to optimize the API on the current basis.

@nicksnyder
Copy link
Owner

There are two ways to solve your problem.

First, you can set the Funcs field of LocalizeConfig so that go-i18n can execute the template correctly: https://github.com/nicksnyder/go-i18n/blob/main/v2/i18n/localizer.go#L70

Second, you probably don't want all this logic to be in a translation string. I would recommend creating multiple smaller strings to translate and then injecting them into the template that has the logic. See example here: https://github.com/nicksnyder/go-i18n/blob/main/v2/example/main.go

@nicksnyder
Copy link
Owner

With 2.4.0 there is now a way to disable template parsing. Check out the release notes for more info.

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

Successfully merging a pull request may close this issue.

2 participants