-
Notifications
You must be signed in to change notification settings - Fork 277
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
Comments
@nicksnyder |
What is your use case for this? Would it work to export the Lines 138 to 144 in 57bd175
|
@nicksnyder 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. |
There are two ways to solve your problem. First, you can set the 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 |
With 2.4.0 there is now a way to disable template parsing. Check out the release notes for more info. |
The requirement is to return different template strings according to different languages, but not to parse the template.
Want to return different strings based on different languages, e.g.
My name is {{ .Name}}.
or我的名字是 {{ .Name}}。
The text was updated successfully, but these errors were encountered: