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) { ...@@ -82,23 +82,29 @@ func (ct CredentialType) IndexOf(ai AttributeTypeIdentifier) (int, error) {
return -1, errors.New("Attribute identifier not found") return -1, errors.New("Attribute identifier not found")
} }
// TranslatedString represents an XML tag containing a string translated to multiple languages. // TranslatedString is a map of translated strings.
// For example: <Foo id="bla"><en>Hello world</en><nl>Hallo wereld</nl></Foo> type TranslatedString map[string]string
type TranslatedString struct {
Translations []struct { // UnmarshalXML unmarshals an XML tag containing a string translated to multiple languages,
XMLName xml.Name // for example: <Foo><en>Hello world</en><nl>Hallo wereld</nl></Foo>
Text string `xml:",chardata"` // into a TranslatedString: { "en": "Hello world" , "nl": "Hallo wereld" }
} `xml:",any"` func (ts *TranslatedString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
} if map[string]string(*ts) == nil {
*ts = TranslatedString(make(map[string]string))
// Translation returns the specified translation. }
func (ts *TranslatedString) Translation(lang string) string { temp := &struct {
for _, translation := range ts.Translations { Translations []struct {
if translation.XMLName.Local == lang { XMLName xml.Name
return translation.Text 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. // Identifier returns the identifier of the specified credential type.
......
...@@ -132,15 +132,15 @@ func TestParseStore(t *testing.T) { ...@@ -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.NotNil(t, MetaStore.Issuers[NewIssuerIdentifier("irma-demo.RU")].CurrentPublicKey().N, "irma-demo.RU public key has no modulus")
assert.Equal(t, assert.Equal(t,
"Irma Demo", "Irma Demo",
MetaStore.SchemeManagers[NewSchemeManagerIdentifier("irma-demo")].Name.Translation("en"), MetaStore.SchemeManagers[NewSchemeManagerIdentifier("irma-demo")].Name["en"],
"irma-demo scheme manager has unexpected name") "irma-demo scheme manager has unexpected name")
assert.Equal(t, assert.Equal(t,
"Radboud Universiteit Nijmegen", "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") "irma-demo.RU issuer has unexpected name")
assert.Equal(t, assert.Equal(t,
"Student Card", "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") "irma-demo.RU.studentCard has unexpected name")
assert.Equal(t, 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