Skip to content

Commit

Permalink
feat: add errort
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmunillas committed Sep 8, 2024
1 parent 815817c commit 6004452
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions i18n/translations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package i18n
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -171,14 +172,17 @@ func t(ctx context.Context, key string, raw bool, replacements ...Replacements)
return chunksRender(chunks)
}

// T returns the translated translation as a component
func T(ctx context.Context, key string, replacements ...Replacements) templ.Component {
return t(ctx, key, false, replacements...)
}

// RawT returns the translated translation as a component and allows raw html to be embedded
func RawT(ctx context.Context, key string, replacements ...Replacements) templ.Component {
return t(ctx, key, true, replacements...)
}

// Translation returns the translated translation as a string
func Translation(ctx context.Context, key string) string {
locale := FromCtx(ctx)
content := translations[locale][key]
Expand All @@ -187,3 +191,13 @@ func Translation(ctx context.Context, key string) string {
}
return content
}

// ErrorT returns an error type with the translated translation as content
func ErrorT(ctx context.Context, key string) error {
locale := FromCtx(ctx)
content := translations[locale][key]
if content == "" {
return errors.New(key)
}
return errors.New(content)
}

0 comments on commit 6004452

Please sign in to comment.