Skip to content

Commit 11b1bf1

Browse files
authored
Merge pull request #4 from donseba/dep
upgrade to golang 1.24 , harden tests , linter
2 parents 7168ba1 + 31896ef commit 11b1bf1

11 files changed

+173
-23
lines changed

.github/FUNDING.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# These are supported funding model platforms
2+
3+
github: donseba # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
12+
polar: # Replace with a single Polar username
13+
buy_me_a_coffee: # Replace with a single Buy Me a Coffee username
14+
thanks_dev: # Replace with a single thanks.dev username
15+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

.github/workflows/golangci-lint.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
pull_request:
8+
9+
permissions:
10+
contents: read
11+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
12+
# pull-requests: read
13+
14+
jobs:
15+
golangci:
16+
name: lint
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v3
20+
- uses: actions/setup-go@v4
21+
with:
22+
go-version: '1.24'
23+
cache: false
24+
- name: golangci-lint
25+
uses: golangci/golangci-lint-action@v3
26+
with:
27+
# Require: The version of golangci-lint to use.
28+
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
29+
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
30+
version: latest
31+
32+
# Optional: working directory, useful for monorepos
33+
# working-directory: somedir
34+
35+
# Optional: golangci-lint command line arguments.
36+
#
37+
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
38+
# The location of the configuration file can be changed by using `--config=`
39+
# args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0
40+
args: --out-format=colored-line-number
41+
42+
# Optional: show only new issues if it's a pull request. The default value is `false`.
43+
# only-new-issues: true
44+
45+
# Optional: if set to true, then all caching functionality will be completely disabled,
46+
# takes precedence over all other caching options.
47+
# skip-cache: true
48+
49+
# Optional: if set to true, then the action won't cache or restore ~/go/pkg.
50+
# skip-pkg-cache: true
51+
52+
# Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
53+
# skip-build-cache: true
54+
55+
# Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
56+
# install-mode: "goinstall"

get.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
// Tl translates a string based on the given language tag and key.
8-
func (t *Translator) tl(loc Localizer, key string, args ...interface{}) string {
8+
func (t *Translator) tl(loc Localizer, key string, args ...any) string {
99
translator, exists := t.languages[loc.GetLocale()]
1010
if !exists {
1111
return fmt.Sprintf(DefaultNoTranslationTL, key)
@@ -24,11 +24,11 @@ func (t *Translator) tl(loc Localizer, key string, args ...interface{}) string {
2424
return fmt.Sprintf(DefaultNoTranslationTL, key)
2525
}
2626

27-
translated := translator.Get(key, args...)
27+
translated := translator.Get(fmt.Sprintf("%s", key), args...) //nolint:gosimple
2828
return t.removePrefix(translated)
2929
}
3030

31-
func (t *Translator) ctl(loc Localizer, ctx, key string, args ...interface{}) string {
31+
func (t *Translator) ctl(loc Localizer, ctx, key string, args ...any) string {
3232
translator, exists := t.languages[loc.GetLocale()]
3333
if !exists {
3434
return fmt.Sprintf(DefaultNoTranslationCTL, ctx, key)
@@ -55,7 +55,7 @@ func (t *Translator) ctl(loc Localizer, ctx, key string, args ...interface{}) st
5555
return fmt.Sprintf(DefaultNoTranslationCTL, ctx, key)
5656
}
5757

58-
translated := translator.GetC(key, ctx, args...)
58+
translated := translator.GetC(fmt.Sprintf("%s", key), ctx, args...) //nolint:gosimple
5959
return t.removePrefix(translated)
6060
}
6161

@@ -111,7 +111,7 @@ func (t *Translator) ctn(loc Localizer, ctx, singular, plural string, n int, arg
111111
}
112112

113113
// Tl translates a string based on the given language tag and key.
114-
func (t *Translator) Tl(loc Localizer, key string, args ...interface{}) string {
114+
func (t *Translator) Tl(loc Localizer, key string, args ...any) string {
115115
return t.tl(loc, key, args...)
116116
}
117117

@@ -121,7 +121,7 @@ func (t *Translator) Tn(loc Localizer, singular, plural string, n int) string {
121121
}
122122

123123
// Ctl method for handling string translation with context
124-
func (t *Translator) Ctl(loc Localizer, ctx, key string, args ...interface{}) string {
124+
func (t *Translator) Ctl(loc Localizer, ctx, key string, args ...any) string {
125125
return t.ctl(loc, ctx, key, args...)
126126
}
127127

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/donseba/go-translator
22

3-
go 1.21.6
3+
go 1.24.0
44

5-
require github.com/donseba/gotext v1.5.7
5+
require github.com/leonelquinteros/gotext v1.7.2-0.20250311125224-bdb582003488

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/donseba/gotext v1.5.7 h1:7aDhXXdB09GObC350IpJ5xADjgY83tGe5iCD+gUIi1U=
2-
github.com/donseba/gotext v1.5.7/go.mod h1:LxKX7opw9kjMdEZqplR6sgDp0qFG6AFEvz4oYpNtCcY=
1+
github.com/leonelquinteros/gotext v1.7.2-0.20250311125224-bdb582003488 h1:ifSZSC/OCl/43oeCGssg+HmSgR/Ni88yEhqEOlLFRwo=
2+
github.com/leonelquinteros/gotext v1.7.2-0.20250311125224-bdb582003488/go.mod h1:I0WoFDn9u2D3VbPnnDPT8mzZu0iSXG8iih+AH2fHHqg=

set.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package translator
22

33
import (
44
"fmt"
5-
"github.com/donseba/gotext"
65
"os"
76
"path/filepath"
87
"time"
8+
9+
"github.com/leonelquinteros/gotext"
910
)
1011

1112
func (t *Translator) SetTL(loc Localizer, key string, value string) error {

set_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package translator
22

33
import (
44
"fmt"
5-
"github.com/donseba/gotext"
65
"testing"
76
)
87

@@ -15,7 +14,6 @@ func TestTranslator_SetTL(t *testing.T) {
1514
type fields struct {
1615
translationsDir string
1716
templateDir string
18-
languages map[string]*gotext.Po
1917
uniqueKeys map[string]uniqueKey
2018
uniqueKeysCtx map[string]map[string]uniqueKey
2119
}
@@ -114,7 +112,7 @@ func TestTranslator_SetTL(t *testing.T) {
114112
}
115113
}
116114

117-
tls := tr.languages[tt.lang].GetTranslations()
115+
tls := tr.languages[tt.lang].GetDomain().GetTranslations()
118116
for _, tl := range tls {
119117
fmt.Printf("%+v\n", tl)
120118
}

test_translations/en_US.po

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ msgstr "Third translation text"
2525
msgid "There is one apple"
2626
msgid_plural "There are many apples"
2727
msgstr[0] "There is one apple"
28-
msgstr[1] "There are many apples"
28+
msgstr[1] "There are many apples"
29+
30+
msgid "First translation text with %s"
31+
msgstr "First translation text with %s"
32+
33+
msgid "Second translation text with %s"
34+
msgstr "Second translation text with %s"
35+
36+
msgid "Third translation text with %s"
37+
msgstr "Third translation text with %s"

test_translations/translations.pot

+40
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,43 @@ msgid "First translation text"
3434
msgid_plural "First translation text plural"
3535
msgstr[0] ""
3636
msgstr[1] ""
37+
38+
msgid "Testing TN translation with amount"
39+
msgid_plural "Testing TN translations with amount"
40+
msgstr[0] ""
41+
msgstr[1] ""
42+
43+
msgid "Testing TN translation"
44+
msgid_plural "Testing TN translations"
45+
msgstr[0] ""
46+
msgstr[1] ""
47+
48+
msgid "Testing TN translation with amount and vars"
49+
msgid_plural "Testing %s translations with amount and vars"
50+
msgstr[0] ""
51+
msgstr[1] ""
52+
53+
msgid "Testing TL translation"
54+
msgstr ""
55+
56+
msgctxt "in context"
57+
msgid "Testing CTL translation"
58+
msgstr ""
59+
60+
msgctxt "in context"
61+
msgid "Testing CTN translation"
62+
msgid_plural "Testing CTN translations"
63+
msgstr[0] ""
64+
msgstr[1] ""
65+
66+
msgid "First translation text with %s"
67+
msgstr ""
68+
69+
msgid "Second translation text with %s"
70+
msgstr ""
71+
72+
msgid "Third translation text with %s"
73+
msgstr ""
74+
75+
msgid "Non-existent text with %s"
76+
msgstr ""

translator.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package translator
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/donseba/gotext"
76
"html/template"
87
"os"
98
"path"
109
"path/filepath"
1110
"regexp"
1211
"strings"
12+
13+
"github.com/leonelquinteros/gotext"
1314
)
1415

1516
var (
@@ -133,8 +134,8 @@ func (t *Translator) CheckMissingTranslations() error {
133134
}
134135

135136
var (
136-
tr = t.pot.GetTranslations()
137-
ctr = t.pot.GetCtxTranslations()
137+
tr = t.pot.GetDomain().GetTranslations()
138+
ctr = t.pot.GetDomain().GetCtxTranslations()
138139
)
139140

140141
for key, entry := range t.uniqueKeys {

translator_test.go

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package translator
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
var (
68
translationsDir = "test_translations"
@@ -97,6 +99,34 @@ func TestTlFunction(t *testing.T) {
9799
}
98100
}
99101

102+
func TestTlFunctionWithArgs(t *testing.T) {
103+
translator := NewTranslator(translationsDir, templateDir)
104+
err := translator.AddLanguage(lang)
105+
if err != nil {
106+
t.Errorf("AddLanguage() error = %v", err)
107+
}
108+
109+
loc := mockLocalizer{locale: lang}
110+
111+
tests := []struct {
112+
key string
113+
expected string
114+
args []any
115+
}{
116+
{"First translation text with %s", "First translation text with args", []any{"args"}},
117+
{"Second translation text with %s", "Second translation text with args", []any{"args"}},
118+
{"Third translation text with %s", "Third translation text with args", []any{"args"}},
119+
{"Non-existent text with %s", "*Non-existent text with %s*", []any{"args"}}, // Assuming the behavior for non-translated text
120+
}
121+
122+
for _, tt := range tests {
123+
result := translator.tl(loc, tt.key, tt.args...)
124+
if result != tt.expected {
125+
t.Errorf("tl() for key '%s' = %s, want %s", tt.key, result, tt.expected)
126+
}
127+
}
128+
}
129+
100130
func TestTlFunctionNL(t *testing.T) {
101131
translator := NewTranslator(translationsDir, templateDir)
102132
err := translator.AddLanguage(langNL)
@@ -152,7 +182,7 @@ func TestTnFunction(t *testing.T) {
152182
}
153183
}
154184

155-
func TestTnFunctionMultiplural(t *testing.T) {
185+
func TestTnFunctionMultiPlural(t *testing.T) {
156186
translator := NewTranslator("test_translations", "")
157187
err := translator.AddLanguage(multiplural)
158188
if err != nil {
@@ -166,12 +196,12 @@ func TestTnFunctionMultiplural(t *testing.T) {
166196
plural string
167197
count int
168198
expected string
169-
vars []interface{}
199+
vars []any
170200
}{
171201
{"There is one apple", "There are many apples", 1, "C'è una mela", nil},
172202
{"There is one apple", "There are many apples", 2, "Ci sono due mele", nil},
173-
{"There is one apple", "There are %d apples", 0, "Ci sono 0 mele", []interface{}{0}},
174-
{"There is one apple", "There are %d apples", 5, "Ci sono 5 mele", []interface{}{5}},
203+
{"There is one apple", "There are %d apples", 0, "Ci sono 0 mele", []any{0}},
204+
{"There is one apple", "There are %d apples", 5, "Ci sono 5 mele", []any{5}},
175205
// Add more test cases as needed
176206
}
177207

0 commit comments

Comments
 (0)