Skip to content

Commit

Permalink
Added support for messages with variable arguments
Browse files Browse the repository at this point in the history
- Refactored GetMessage(locale, name) to Message(locale, name, args...)
- Refactored/expanded tests for Message
  • Loading branch information
tmbrggmn committed Nov 14, 2012
1 parent 4d19874 commit 8b09cb1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
7 changes: 6 additions & 1 deletion i18n.go
Expand Up @@ -32,7 +32,7 @@ func MessageLocales() []string {
// Perform a message look-up for the given locale and message.
//
// When either an unknown locale or message is detected, a specially formatted string is returned.
func GetMessage(locale string, message string) (value string) {
func Message(locale, message string, args ...interface{}) (value string) {
messageConfig, knownLocale := messages[locale]
if !knownLocale {
WARN.Printf("Unknown locale '%s' for message '%s'", locale, message)
Expand All @@ -45,6 +45,11 @@ func GetMessage(locale string, message string) (value string) {
return fmt.Sprintf(unknownValueFormat, message)
}

if len(args) > 0 {
TRACE.Printf("Arguments detected, formatting '%s' with %v", value, args)
value = fmt.Sprintf(value, args...)
}

return value
}

Expand Down
39 changes: 25 additions & 14 deletions i18n_test.go
Expand Up @@ -17,38 +17,49 @@ func TestI8nLoadMessages(t *testing.T) {
}
}

func TestI8nGetMessage(t *testing.T) {
func TestI8nMessage(t *testing.T) {
loadMessages(testDataPath)

// Assert that we can get a message and we get the expected return value
if message := GetMessage("nl", "greeting"); message != "Hallo" {
t.Error("Message 'greeting' for locale 'nl' does not have the expected value")
if message := Message("nl", "greeting"); message != "Hallo" {
t.Errorf("Message 'greeting' for locale 'nl' (%s) does not have the expected value", message)
}
if message := GetMessage("en", "greeting"); message != "Hello" {
t.Error("Message 'greeting' for locale 'en' does not have the expected value")
if message := Message("en", "greeting"); message != "Hello" {
t.Errorf("Message 'greeting' for locale 'en' (%s) does not have the expected value", message)
}
if message := GetMessage("en", "folded"); message != "Greeting is 'Hello'" {
if message := Message("en", "folded"); message != "Greeting is 'Hello'" {
t.Error("Unexpected unfolded message: ", message)
}
if message := Message("en", "arguments.string", "Vincent Hanna"); message != "My name is Vincent Hanna" {
t.Errorf("Message 'arguments.string' for locale 'en' (%s) does not have the expected value", message)
}
if message := Message("en", "arguments.hex", 1234567, 1234567); message != "The number 1234567 in hexadecimal notation would be 12d687" {
t.Errorf("Message 'arguments.hex' for locale 'en' (%s) does not have the expected value", message)
}

// TODO: re-enable this test once files merging has been implemented
/*if message := GetMessage("en", "greeting2"); message != "Yo!" {
/*if message := Message("en", "greeting2"); message != "Yo!" {
t.Error("Message 'greeting2' for locale 'en' does not have the expected value")
}*/

// Assert that we get the expected return value for a locale that doesn't exist
if message := GetMessage("unknown locale", "message"); message != "??? unknown locale ???" {
t.Error("Locale 'doesn't exist' is not supposed to exist")
if message := Message("unknown locale", "message"); message != "??? unknown locale ???" {
t.Error("Locale 'unknown locale' is not supposed to exist")
}

// Assert that we get the expected return value for a message that doesn't exist
if message := GetMessage("nl", "unknown message"); message != "??? unknown message ???" {
t.Error("Message 'doesn't exist' is not supposed to exist")
if message := Message("nl", "unknown message"); message != "??? unknown message ???" {
t.Error("Message 'unknown message' is not supposed to exist")
}
}

func BenchmarkI8nMessage(b *testing.B) {
for i := 0; i < b.N; i++ {
Message("nl", "greeting")
}
}

func BenchmarkI8nGetMessage(b *testing.B) {
func BenchmarkI8nMessageWithArguments(b *testing.B) {
for i := 0; i < b.N; i++ {
GetMessage("nl", "greeting")
Message("en", "greeting")
}
}
3 changes: 3 additions & 0 deletions testdata/i18n/messages/english_messages.en
Expand Up @@ -4,6 +4,9 @@ greeting.suffix=, welcome to Revel!

folded=Greeting is '%(greeting)s'

arguments.string=My name is %s
arguments.hex=The number %d in hexadecimal notation would be %x

[AU]
greeting=G'day

Expand Down

0 comments on commit 8b09cb1

Please sign in to comment.