1
// Copyright 2015 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// Package message implements formatted I/O for localized strings with functions
6
// analogous to the fmt's print functions.
8
// Under construction. See https://golang.org/design/text/12750-localization
9
// and its corresponding proposal issue https://golang.org/issues/12750.
16
"golang.org/x/text/internal/format"
17
"golang.org/x/text/language"
20
// A Printer implements language-specific formatted I/O analogous to the fmt
21
// package. Only one goroutine may use a Printer at the same time.
25
// NOTE: limiting one goroutine per Printer allows for many optimizations
26
// and simplifications. We can consider removing this restriction down the
27
// road if it the benefits do not seem to outweigh the disadvantages.
30
// NewPrinter returns a Printer that formats messages tailored to language t.
31
func NewPrinter(t language.Tag) *Printer {
32
return &Printer{tag: t}
35
// Sprint is like fmt.Sprint, but using language-specific formatting.
36
func (p *Printer) Sprint(a ...interface{}) string {
37
return fmt.Sprint(p.bindArgs(a)...)
40
// Fprint is like fmt.Fprint, but using language-specific formatting.
41
func (p *Printer) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
42
return fmt.Fprint(w, p.bindArgs(a)...)
45
// Print is like fmt.Print, but using language-specific formatting.
46
func (p *Printer) Print(a ...interface{}) (n int, err error) {
47
return fmt.Print(p.bindArgs(a)...)
50
// bindArgs wraps arguments with implementation of fmt.Formatter, if needed.
51
func (p *Printer) bindArgs(a []interface{}) []interface{} {
52
out := make([]interface{}, len(a))
54
switch v := x.(type) {
56
// Wrap the value with a Formatter that augments the State with
57
// language-specific attributes.
60
// NOTE: as we use fmt.Formatter, we can't distinguish between
61
// regular and localized formatters, so we always need to wrap it.
74
// state implements "golang.org/x/text/internal/format".State.
80
func (s *state) Language() language.Tag { return s.p.tag }
82
var _ format.State = &state{}
89
func (v *value) Format(s fmt.State, verb rune) {
90
v.x.Format(&state{s, v.p}, verb)