Skip to content

Commit

Permalink
Fix: Escaping special characters did not work correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
vorlif committed Jun 5, 2022
1 parent 883dbfb commit c1bfc0f
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 120 deletions.
4 changes: 3 additions & 1 deletion localizer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package spreak

import (
"errors"

"golang.org/x/text/language"

"github.com/vorlif/spreak/localize"
Expand Down Expand Up @@ -232,7 +234,7 @@ func (l *Localizer) DNPGetf(domain localize.Domain, context localize.Context, si
// If no matching translation is found, the original string with the matching plural form and an error are returned.
func (l *Localizer) LocalizeWithError(t localize.Localizable) (string, error) {
if t == nil {
return "<nil>", nil
return "<nil>", errors.New("spreak: Localizable is nil")
}

var vars []interface{}
Expand Down
10 changes: 10 additions & 0 deletions localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func TestLocalizer_TranslateWithError(t *testing.T) {
tr, err = localizer.LocalizeWithError(localizeMsg)
assert.NoError(t, err)
assert.Equal(t, "Test mit Context", tr)

tr, err = localizer.LocalizeWithError(nil)
assert.Error(t, err)
assert.Equal(t, "<nil>", tr)
}

func TestLocalizer_MainFunctions(t *testing.T) {
Expand Down Expand Up @@ -246,6 +250,12 @@ func TestLocalizer_LocalizeError(t *testing.T) {
assert.Equal(t, want, get.Error())
})

t.Run("nil error", func(t *testing.T) {
actual := localizer.LocalizeError(nil)
assert.NoError(t, actual)
assert.Nil(t, actual)
})

t.Run("localizable error", func(t *testing.T) {
raw := &testLocalizeErr{
singular: "failure",
Expand Down
30 changes: 30 additions & 0 deletions pkg/po/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,36 @@ msgstr "ID"`
assert.Equal(t, "contrib/humanize/templatetags/humanize.py", msg.Comment.References[2].Path)
assert.Equal(t, 186, msg.Comment.References[2].Line)
})

t.Run("parse special chars", func(t *testing.T) {
content := `
msgid "This is an \"test\" \n with\ newline"
msgstr "Das ist ein \"test\" \n mit\ newline"`
file, err := Parse([]byte(content))
assert.NoError(t, err)
require.NotNil(t, file)
require.Contains(t, file.Messages, "")
require.Contains(t, file.Messages[""], "This is an \"test\" \n with\\ newline")
msg := file.GetMessage("", "This is an \"test\" \n with\\ newline")
require.NotNil(t, msg)
require.Len(t, msg.Str, 1)
assert.Equal(t, "Das ist ein \"test\" \n mit\\ newline", msg.Str[0])
})

t.Run("parse tabs", func(t *testing.T) {
content := `
msgid "Test with tabs\t"
msgstr "Test mit Tabs\t"`
file, err := Parse([]byte(content))
assert.NoError(t, err)
require.NotNil(t, file)
require.Contains(t, file.Messages, "")
require.Contains(t, file.Messages[""], "Test\t\twith \ttabs\t")
msg := file.GetMessage("", "Test \twith \ttabs\t")
require.NotNil(t, msg)
require.Len(t, msg.Str, 1)
assert.Equal(t, "Test\t\tmit \tTabs\t", msg.Str[0])
})
}

func TestMustParse(t *testing.T) {
Expand Down
Loading

0 comments on commit c1bfc0f

Please sign in to comment.