/usr/share/gocode/src/golang.org/x/text/message/message.go is in golang-golang-x-text-dev 0.0~git20170627.0.6353ef0-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | // Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package message implements formatted I/O for localized strings with functions
// analogous to the fmt's print functions.
//
// These are the important differences with fmt:
// - Output varies per locale.
// - The '#' flag is used to bypass localization.
//
// NOTE: Under construction. See https://golang.org/design/12750-localization
// and its corresponding proposal issue https://golang.org/issues/12750.
package message // import "golang.org/x/text/message"
import (
"io"
"os"
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
)
// TODO: allow more than one goroutine per printer. This will allow porting from
// fmt much less error prone.
// A Printer implements language-specific formatted I/O analogous to the fmt
// package. Only one goroutine may use a Printer at the same time.
type Printer struct {
// Wrap the fields in a hidden type to hide some of the implemented methods.
printer printer
// NOTE: limiting one goroutine per Printer allows for many optimizations
// and simplifications. We can consider removing this restriction down the
// road if it the benefits do not seem to outweigh the disadvantages.
}
type options struct {
cat *catalog.Catalog
// TODO:
// - allow %s to print integers in written form (tables are likely too large
// to enable this by default).
// - list behavior
//
}
// An Option defines an option of a Printer.
type Option func(o *options)
// Catalog defines the catalog to be used.
func Catalog(c *catalog.Catalog) Option {
return func(o *options) { o.cat = c }
}
// NewPrinter returns a Printer that formats messages tailored to language t.
func NewPrinter(t language.Tag, opts ...Option) *Printer {
options := &options{
cat: defaultCatalog,
}
for _, o := range opts {
o(options)
}
p := &Printer{printer{
tag: t,
}}
p.printer.toDecimal.InitDecimal(t)
p.printer.toScientific.InitScientific(t)
p.printer.catContext = options.cat.Context(t, &p.printer)
return p
}
// Sprint is like fmt.Sprint, but using language-specific formatting.
func (p *Printer) Sprint(a ...interface{}) string {
p.printer.reset()
p.printer.doPrint(a)
return p.printer.String()
}
// Fprint is like fmt.Fprint, but using language-specific formatting.
func (p *Printer) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
p.printer.reset()
p.printer.doPrint(a)
n64, err := io.Copy(w, &p.printer.Buffer)
return int(n64), err
}
// Print is like fmt.Print, but using language-specific formatting.
func (p *Printer) Print(a ...interface{}) (n int, err error) {
return p.Fprint(os.Stdout, a...)
}
// Sprintln is like fmt.Sprintln, but using language-specific formatting.
func (p *Printer) Sprintln(a ...interface{}) string {
p.printer.reset()
p.printer.doPrintln(a)
return p.printer.String()
}
// Fprintln is like fmt.Fprintln, but using language-specific formatting.
func (p *Printer) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
p.printer.reset()
p.printer.doPrintln(a)
n64, err := io.Copy(w, &p.printer.Buffer)
return int(n64), err
}
// Println is like fmt.Println, but using language-specific formatting.
func (p *Printer) Println(a ...interface{}) (n int, err error) {
return p.Fprintln(os.Stdout, a...)
}
// Sprintf is like fmt.Sprintf, but using language-specific formatting.
func (p *Printer) Sprintf(key Reference, a ...interface{}) string {
lookupAndFormat(p, key, a)
return p.printer.String()
}
// Fprintf is like fmt.Fprintf, but using language-specific formatting.
func (p *Printer) Fprintf(w io.Writer, key Reference, a ...interface{}) (n int, err error) {
lookupAndFormat(p, key, a)
return w.Write(p.printer.Bytes())
}
// Printf is like fmt.Printf, but using language-specific formatting.
func (p *Printer) Printf(key Reference, a ...interface{}) (n int, err error) {
lookupAndFormat(p, key, a)
return os.Stdout.Write(p.printer.Bytes())
}
func lookupAndFormat(p *Printer, r Reference, a []interface{}) {
p.printer.reset()
p.printer.args = a
var id, msg string
switch v := r.(type) {
case string:
id, msg = v, v
case key:
id, msg = v.id, v.fallback
default:
panic("key argument is not a Reference")
}
if p.printer.catContext.Execute(id) == catalog.ErrNotFound {
if p.printer.catContext.Execute(msg) == catalog.ErrNotFound {
p.printer.Render(msg)
return
}
}
}
// Arg implements catmsg.Renderer.
func (p *printer) Arg(i int) interface{} { // TODO, also return "ok" bool
if uint(i) < uint(len(p.args)) {
return p.args[i]
}
return nil
}
// Render implements catmsg.Renderer.
func (p *printer) Render(msg string) {
p.doPrintf(msg)
}
// A Reference is a string or a message reference.
type Reference interface {
// TODO: also allow []string
}
// Key creates a message Reference for a message where the given id is used for
// message lookup and the fallback is returned when no matches are found.
func Key(id string, fallback string) Reference {
return key{id, fallback}
}
type key struct {
id, fallback string
}
|