golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.98k stars 17.54k forks source link

x/text: printer.Printf works but printer.Print does not (all variants of print/printf) #41781

Open kalbasit opened 3 years ago

kalbasit commented 3 years ago

When using the message printer, I can translate fine with the Printf variants (Sprintf, Fprintf, Printf) but I don't get the translation for the Print variants (Sprint, Fprint, Print).

What version of Go are you using (go version)?

$ go version
go version go1.14.7 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/yl/.cache/go/cache"
GOENV="/yl/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/yl/.cache/ktmr/go:/yl/.cache/go/path"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/nix/store/y456vnv1032n1fx4bmh1b54m2br4c6jq-go-1.14.7/share/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/nix/store/y456vnv1032n1fx4bmh1b54m2br4c6jq-go-1.14.7/share/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/yl/code/stories/keeptruckin/SLB-962/github.com/KeepTruckin/kt/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-static-libstdc++"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/run/user/2000/go-build560714827=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The following code is also available on the playground: https://play.golang.org/p/FHjZ3a8ug51

package main

import (
    "fmt"

    "golang.org/x/text/language"
    "golang.org/x/text/message"
)

func init() {
    message.SetString(language.English, "Book Now", "Book Now")
    message.SetString(language.French, "Book Now", "Réserver Maintenant")
    message.SetString(language.Spanish, "Book Now", "Reserve Ahora")
}

func main() {
    tags := []language.Tag{language.English, language.French, language.Spanish}

    for _, tag := range tags {
        p := message.NewPrinter(tag)
        fmt.Printf("==== %s ====\n", tag)

        fmt.Print("p.Print(\"Book Now\"): ")
        p.Print("Book Now")
        fmt.Println("")

        fmt.Print("p.Printf(\"Book Now\"): ")
        p.Printf("Book Now")
        fmt.Println("")
    }
}

What did you expect to see?

==== en ====
p.Print("Book Now"): Book Now
p.Printf("Book Now"): Book Now
==== fr ====
p.Print("Book Now"): Réserver Maintenant
p.Printf("Book Now"): Réserver Maintenant
==== es ====
p.Print("Book Now"): Reserve Ahora
p.Printf("Book Now"): Reserve Ahora

What did you see instead?

==== en ====
p.Print("Book Now"): Book Now
p.Printf("Book Now"): Book Now
==== fr ====
p.Print("Book Now"): Book Now
p.Printf("Book Now"): Réserver Maintenant
==== es ====
p.Print("Book Now"): Book Now
p.Printf("Book Now"): Reserve Ahora

@kaskavalci mentioned this problem in https://github.com/golang/go/issues/26767#issuecomment-446170489

cc @mpvl

gopherbot commented 2 years ago

Change https://golang.org/cl/356314 mentions this issue: x/text: translate messages formatted via non-printf methods

mpvl commented 2 years ago

Translations are only done for format strings or substitutions in %m. So this is intentional.

In my opinion, also translating strings for Print variants is undesirable. But even if it is, this would now be a backwards incompatible change.

To print without a message, one can use p.Printf("%m", str).