Commit 58b74cb1 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Simplify TranslatedString

parent 66ea0b31
......@@ -82,23 +82,29 @@ func (ct CredentialType) IndexOf(ai AttributeTypeIdentifier) (int, error) {
return -1, errors.New("Attribute identifier not found")
}
// TranslatedString represents an XML tag containing a string translated to multiple languages.
// For example: <Foo id="bla"><en>Hello world</en><nl>Hallo wereld</nl></Foo>
type TranslatedString struct {
Translations []struct {
XMLName xml.Name
Text string `xml:",chardata"`
} `xml:",any"`
}
// Translation returns the specified translation.
func (ts *TranslatedString) Translation(lang string) string {
for _, translation := range ts.Translations {
if translation.XMLName.Local == lang {
return translation.Text
}
// TranslatedString is a map of translated strings.
type TranslatedString map[string]string
// UnmarshalXML unmarshals an XML tag containing a string translated to multiple languages,
// for example: <Foo><en>Hello world</en><nl>Hallo wereld</nl></Foo>
// into a TranslatedString: { "en": "Hello world" , "nl": "Hallo wereld" }
func (ts *TranslatedString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if map[string]string(*ts) == nil {
*ts = TranslatedString(make(map[string]string))
}
temp := &struct {
Translations []struct {
XMLName xml.Name
Text string `xml:",chardata"`
} `xml:",any"`
}{}
if err := d.DecodeElement(temp, &start); err != nil {
return err
}
for _, translation := range temp.Translations {
(*ts)[translation.XMLName.Local] = translation.Text
}
return ""
return nil
}
// Identifier returns the identifier of the specified credential type.
......
......@@ -132,15 +132,15 @@ func TestParseStore(t *testing.T) {
assert.NotNil(t, MetaStore.Issuers[NewIssuerIdentifier("irma-demo.RU")].CurrentPublicKey().N, "irma-demo.RU public key has no modulus")
assert.Equal(t,
"Irma Demo",
MetaStore.SchemeManagers[NewSchemeManagerIdentifier("irma-demo")].Name.Translation("en"),
MetaStore.SchemeManagers[NewSchemeManagerIdentifier("irma-demo")].Name["en"],
"irma-demo scheme manager has unexpected name")
assert.Equal(t,
"Radboud Universiteit Nijmegen",
MetaStore.Issuers[NewIssuerIdentifier("irma-demo.RU")].Name.Translation("en"),
MetaStore.Issuers[NewIssuerIdentifier("irma-demo.RU")].Name["en"],
"irma-demo.RU issuer has unexpected name")
assert.Equal(t,
"Student Card",
MetaStore.Credentials[NewCredentialTypeIdentifier("irma-demo.RU.studentCard")].ShortName.Translation("en"),
MetaStore.Credentials[NewCredentialTypeIdentifier("irma-demo.RU.studentCard")].ShortName["en"],
"irma-demo.RU.studentCard has unexpected name")
assert.Equal(t,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment