summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/text/internal/catmsg
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/text/internal/catmsg')
-rw-r--r--vendor/golang.org/x/text/internal/catmsg/catmsg.go67
-rw-r--r--vendor/golang.org/x/text/internal/catmsg/catmsg_test.go15
-rw-r--r--[-rwxr-xr-x]vendor/golang.org/x/text/internal/catmsg/codec.go2
3 files changed, 72 insertions, 12 deletions
diff --git a/vendor/golang.org/x/text/internal/catmsg/catmsg.go b/vendor/golang.org/x/text/internal/catmsg/catmsg.go
index f8abf3704..c0bf86f09 100644
--- a/vendor/golang.org/x/text/internal/catmsg/catmsg.go
+++ b/vendor/golang.org/x/text/internal/catmsg/catmsg.go
@@ -74,13 +74,6 @@ import (
// A Handle refers to a registered message type.
type Handle int
-// First is used as a Handle to EncodeMessageType, followed by a series of calls
-// to EncodeMessage, to implement selecting the first matching Message.
-//
-// TODO: this can be removed once we either can use type aliases or if the
-// internals of this package are merged with the catalog package.
-var First Handle = msgFirst
-
// A Handler decodes and evaluates data compiled by a Message and sends the
// result to the Decoder. The output may depend on the value of the substitution
// arguments, accessible by the Decoder's Arg method. The Handler returns false
@@ -111,20 +104,24 @@ const (
msgFirst
msgRaw
msgString
- numFixed
+ msgAffix
+ // Leave some arbitrary room for future expansion: 20 should suffice.
+ numInternal = 20
)
const prefix = "golang.org/x/text/internal/catmsg."
var (
+ // TODO: find a more stable way to link handles to message types.
mutex sync.Mutex
names = map[string]Handle{
prefix + "Vars": msgVars,
prefix + "First": msgFirst,
prefix + "Raw": msgRaw,
prefix + "String": msgString,
+ prefix + "Affix": msgAffix,
}
- handlers = make([]Handler, numFixed)
+ handlers = make([]Handler, numInternal)
)
func init() {
@@ -168,6 +165,20 @@ func init() {
}
return true
}
+
+ handlers[msgAffix] = func(d *Decoder) bool {
+ // TODO: use an alternative method for common cases.
+ prefix := d.DecodeString()
+ suffix := d.DecodeString()
+ if prefix != "" {
+ d.Render(prefix)
+ }
+ ret := d.ExecuteMessage()
+ if suffix != "" {
+ d.Render(suffix)
+ }
+ return ret
+ }
}
var (
@@ -236,6 +247,23 @@ func Compile(tag language.Tag, macros Dictionary, m Message) (data string, err e
return string(buf), err
}
+// FirstOf is a message type that prints the first message in the sequence that
+// resolves to a match for the given substitution arguments.
+type FirstOf []Message
+
+// Compile implements Message.
+func (s FirstOf) Compile(e *Encoder) error {
+ e.EncodeMessageType(msgFirst)
+ err := ErrIncomplete
+ for i, m := range s {
+ if err == nil {
+ return fmt.Errorf("catalog: message argument %d is complete and blocks subsequent messages", i-1)
+ }
+ err = e.EncodeMessage(m)
+ }
+ return err
+}
+
// Var defines a message that can be substituted for a placeholder of the same
// name. If an expression does not result in a string after evaluation, Name is
// used as the substitution. For example:
@@ -364,3 +392,24 @@ func (s String) Compile(e *Encoder) (err error) {
}
return err
}
+
+// Affix is a message that adds a prefix and suffix to another message.
+// This is mostly used add back whitespace to a translation that was stripped
+// before sending it out.
+type Affix struct {
+ Message Message
+ Prefix string
+ Suffix string
+}
+
+// Compile implements Message.
+func (a Affix) Compile(e *Encoder) (err error) {
+ // TODO: consider adding a special message type that just adds a single
+ // return. This is probably common enough to handle the majority of cases.
+ // Get some stats first, though.
+ e.EncodeMessageType(msgAffix)
+ e.EncodeString(a.Prefix)
+ e.EncodeString(a.Suffix)
+ e.EncodeMessage(a.Message)
+ return nil
+}
diff --git a/vendor/golang.org/x/text/internal/catmsg/catmsg_test.go b/vendor/golang.org/x/text/internal/catmsg/catmsg_test.go
index df913a1de..b2a7a9e45 100644
--- a/vendor/golang.org/x/text/internal/catmsg/catmsg_test.go
+++ b/vendor/golang.org/x/text/internal/catmsg/catmsg_test.go
@@ -71,6 +71,10 @@ func TestCodec(t *testing.T) {
m: String("foo"),
tests: single("foo", ""),
}, {
+ desc: "affix",
+ m: &Affix{String("foo"), "\t", "\n"},
+ tests: single("\t|foo|\n", ""),
+ }, {
desc: "missing var",
m: String("foo${bar}"),
enc: "\x03\x03foo\x02\x03bar",
@@ -101,6 +105,13 @@ func TestCodec(t *testing.T) {
},
tests: single("foo|baz", ""),
}, {
+ desc: "affix with substitution",
+ m: &Affix{seq{
+ &Var{"bar", String("baz")},
+ String("foo${bar}"),
+ }, "\t", "\n"},
+ tests: single("\t|foo|baz|\n", ""),
+ }, {
desc: "shadowed variable",
m: seq{
&Var{"bar", String("baz")},
@@ -140,7 +151,7 @@ func TestCodec(t *testing.T) {
&Var{"bar", incomplete{}},
String("${bar}"),
},
- enc: "\x00\t\b\x01\x01\x04\x04\x02bar\x03\x00\x00\x00",
+ enc: "\x00\t\b\x01\x01\x14\x04\x02bar\x03\x00\x00\x00",
// TODO: recognize that it is cheaper to substitute bar.
tests: single("bar", ""),
}, {
@@ -246,7 +257,7 @@ type seq []Message
func (s seq) Compile(e *Encoder) (err error) {
err = ErrIncomplete
- e.EncodeMessageType(First)
+ e.EncodeMessageType(msgFirst)
for _, m := range s {
// Pass only the last error, but allow erroneous or complete messages
// here to allow testing different scenarios.
diff --git a/vendor/golang.org/x/text/internal/catmsg/codec.go b/vendor/golang.org/x/text/internal/catmsg/codec.go
index ab587f856..49c9fc978 100755..100644
--- a/vendor/golang.org/x/text/internal/catmsg/codec.go
+++ b/vendor/golang.org/x/text/internal/catmsg/codec.go
@@ -169,7 +169,7 @@ func (e *Encoder) addVar(key string, m Message) error {
case err == ErrIncomplete:
if Handle(e.buf[0]) != msgFirst {
seq := &Encoder{root: e.root, parent: e}
- seq.EncodeMessageType(First)
+ seq.EncodeMessageType(msgFirst)
e.flushTo(seq)
e = seq
}