message: add Catalog option
Change-Id: I1aed5c4e0c278c0da697551d44fd645bc447014f
Reviewed-on: https://go-review.googlesource.com/45102
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
diff --git a/message/message.go b/message/message.go
index edcd3bf..8b3bad1 100644
--- a/message/message.go
+++ b/message/message.go
@@ -17,6 +17,9 @@
"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 {
@@ -28,12 +31,35 @@
// 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) *Printer {
+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.catContext = defaultCatalog.Context(t, &p.printer)
+ p.printer.catContext = options.cat.Context(t, &p.printer)
return p
}
diff --git a/message/message_test.go b/message/message_test.go
index 9c14813..e411415 100644
--- a/message/message_test.go
+++ b/message/message_test.go
@@ -133,11 +133,7 @@
for i, pt := range tc.test {
t.Run(fmt.Sprintf("%s:%d", tc.desc, i), func(t *testing.T) {
- tag := language.MustParse(pt.tag)
- p := Printer{printer{
- tag: tag,
- }}
- p.printer.catContext = cat.Context(tag, &p.printer)
+ p := NewPrinter(language.MustParse(pt.tag), Catalog(cat))
if got := p.Sprintf(pt.key, pt.args...); got != pt.want {
t.Errorf("Sprintf(%q, %v) = %s; want %s",