|
4 | 4 | package i18n
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "strings" |
7 | 8 | "testing"
|
8 | 9 |
|
9 | 10 | "github.com/stretchr/testify/assert"
|
|
75 | 76 | assert.Equal(t, "21", ls.Tr("lang1", "b"))
|
76 | 77 | assert.Equal(t, "22", ls.Tr("lang1", "c"))
|
77 | 78 | }
|
| 79 | + |
| 80 | +func TestLocaleStoreQuirks(t *testing.T) { |
| 81 | + const nl = "\n" |
| 82 | + q := func(q1, s string, q2 ...string) string { |
| 83 | + return q1 + s + strings.Join(q2, "") |
| 84 | + } |
| 85 | + testDataList := []struct { |
| 86 | + in string |
| 87 | + out string |
| 88 | + hint string |
| 89 | + }{ |
| 90 | + {` xx`, `xx`, "simple, no quote"}, |
| 91 | + {`" xx"`, ` xx`, "simple, double-quote"}, |
| 92 | + {`' xx'`, ` xx`, "simple, single-quote"}, |
| 93 | + {"` xx`", ` xx`, "simple, back-quote"}, |
| 94 | + |
| 95 | + {`x\"y`, `x\"y`, "no unescape, simple"}, |
| 96 | + {q(`"`, `x\"y`, `"`), `"x\"y"`, "unescape, double-quote"}, |
| 97 | + {q(`'`, `x\"y`, `'`), `x\"y`, "no unescape, single-quote"}, |
| 98 | + {q("`", `x\"y`, "`"), `x\"y`, "no unescape, back-quote"}, |
| 99 | + |
| 100 | + {q(`"`, `x\"y`) + nl + "b=", `"x\"y`, "half open, double-quote"}, |
| 101 | + {q(`'`, `x\"y`) + nl + "b=", `'x\"y`, "half open, single-quote"}, |
| 102 | + {q("`", `x\"y`) + nl + "b=`", `x\"y` + nl + "b=", "half open, back-quote, multi-line"}, |
| 103 | + |
| 104 | + {`x ; y`, `x ; y`, "inline comment (;)"}, |
| 105 | + {`x # y`, `x # y`, "inline comment (#)"}, |
| 106 | + {`x \; y`, `x ; y`, `inline comment (\;)`}, |
| 107 | + {`x \# y`, `x # y`, `inline comment (\#)`}, |
| 108 | + } |
| 109 | + |
| 110 | + for _, testData := range testDataList { |
| 111 | + ls := NewLocaleStore() |
| 112 | + err := ls.AddLocaleByIni("lang1", "Lang1", []byte("a="+testData.in), nil) |
| 113 | + assert.NoError(t, err, testData.hint) |
| 114 | + assert.Equal(t, testData.out, ls.Tr("lang1", "a"), testData.hint) |
| 115 | + } |
| 116 | + |
| 117 | + // TODO: Crowdin needs the strings to be quoted correctly and doesn't like incomplete quotes |
| 118 | + // and Crowdin always outputs quoted strings if there are quotes in the strings. |
| 119 | + // So, Gitea's `key="quoted" unquoted` content shouldn't be used on Crowdin directly, |
| 120 | + // it should be converted to `key="\"quoted\" unquoted"` first. |
| 121 | + // TODO: We can not use UnescapeValueDoubleQuotes=true, because there are a lot of back-quotes in en-US.ini, |
| 122 | + // then Crowdin will output: |
| 123 | + // > key = "`x \" y`" |
| 124 | + // Then Gitea will read a string with back-quotes, which is incorrect. |
| 125 | + // TODO: Crowdin might generate multi-line strings, quoted by double-quote, it's not supported by LocaleStore |
| 126 | + // LocaleStore uses back-quote for multi-line strings, it's not supported by Crowdin. |
| 127 | + // TODO: Crowdin doesn't support back-quote as string quoter, it mainly uses double-quote |
| 128 | + // so, the following line will be parsed as: value="`first", comment="second`" on Crowdin |
| 129 | + // > a = `first; second` |
| 130 | +} |
0 commit comments