~ubuntu-branches/ubuntu/wily/golang-x-text/wily

« back to all changes in this revision

Viewing changes to message/message.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-10-19 22:03:07 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20151019220307-06qbha67qp4yf5gn
Tags: 0+git20151019.0fe7e68-0ubuntu1
New upstream snapshot, resolving FTBFS with golang 1.5 and supporting
MIR of juju (see http://pad.lv/1267393).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
4
 
 
5
// Package message implements formatted I/O for localized strings with functions
 
6
// analogous to the fmt's print functions.
 
7
//
 
8
// Under construction. See https://golang.org/design/text/12750-localization
 
9
// and its corresponding proposal issue https://golang.org/issues/12750.
 
10
package message
 
11
 
 
12
import (
 
13
        "fmt"
 
14
        "io"
 
15
 
 
16
        "golang.org/x/text/internal/format"
 
17
        "golang.org/x/text/language"
 
18
)
 
19
 
 
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.
 
22
type Printer struct {
 
23
        tag language.Tag
 
24
 
 
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.
 
28
}
 
29
 
 
30
// NewPrinter returns a Printer that formats messages tailored to language t.
 
31
func NewPrinter(t language.Tag) *Printer {
 
32
        return &Printer{tag: t}
 
33
}
 
34
 
 
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)...)
 
38
}
 
39
 
 
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)...)
 
43
}
 
44
 
 
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)...)
 
48
}
 
49
 
 
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))
 
53
        for i, x := range a {
 
54
                switch v := x.(type) {
 
55
                case fmt.Formatter:
 
56
                        // Wrap the value with a Formatter that augments the State with
 
57
                        // language-specific attributes.
 
58
                        out[i] = &value{v, p}
 
59
 
 
60
                        // NOTE: as we use fmt.Formatter, we can't distinguish between
 
61
                        // regular and localized formatters, so we always need to wrap it.
 
62
 
 
63
                        // TODO: handle
 
64
                        // - numbers
 
65
                        // - lists
 
66
                        // - time?
 
67
                default:
 
68
                        out[i] = x
 
69
                }
 
70
        }
 
71
        return out
 
72
}
 
73
 
 
74
// state implements "golang.org/x/text/internal/format".State.
 
75
type state struct {
 
76
        fmt.State
 
77
        p *Printer
 
78
}
 
79
 
 
80
func (s *state) Language() language.Tag { return s.p.tag }
 
81
 
 
82
var _ format.State = &state{}
 
83
 
 
84
type value struct {
 
85
        x fmt.Formatter
 
86
        p *Printer
 
87
}
 
88
 
 
89
func (v *value) Format(s fmt.State, verb rune) {
 
90
        v.x.Format(&state{s, v.p}, verb)
 
91
}