Skip to content

Commit

Permalink
Support nesting scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Mar 26, 2024
1 parent fd58233 commit 41f6911
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func N(ctx context.Context, key string, n int, args ...any) string {
// WithScope is used to add a new scope to the context. To use this,
// use a `.` at the beginning of keys.
func WithScope(ctx context.Context, key string) context.Context {
key = ExpandKey(ctx, key)
return context.WithValue(ctx, scopeKey, key)
}

Expand Down
3 changes: 3 additions & 0 deletions i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ func TestScopes(t *testing.T) {
assert.Equal(t, "!(MISSING: .qux)", i18n.T(ctx, ".qux"))

assert.Equal(t, "no mice", i18n.N(ctxScoped, ".mice", 0, i18n.M{"count": 0}))

ctxScoped = i18n.WithScope(ctxScoped, ".mice")
assert.Equal(t, "no mice", i18n.T(ctxScoped, ".zero"))
}

0 comments on commit 41f6911

Please sign in to comment.