-
Notifications
You must be signed in to change notification settings - Fork 14
/
translaton.go
67 lines (52 loc) · 1.63 KB
/
translaton.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package translation
import (
"fmt"
"github.com/coretrix/hitrix/service/component/app"
"strings"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/entity"
)
type ITranslationService interface {
GetText(ormService *beeorm.Engine, lang entity.TranslationTextLang, key entity.TranslationTextKey) string
GetTextWithVars(
ormService *beeorm.Engine,
lang entity.TranslationTextLang,
key entity.TranslationTextKey,
variables map[string]interface{},
) string
}
type translationService struct {
appService *app.App
}
func NewTranslationService(appService *app.App) ITranslationService {
return &translationService{appService}
}
func (u *translationService) GetText(ormService *beeorm.Engine, lang entity.TranslationTextLang, key entity.TranslationTextKey) string {
var found bool
translationTextEntity := &entity.TranslationTextEntity{}
found = ormService.CachedSearchOne(
translationTextEntity,
"CachedQueryKey",
key)
if !found {
if !u.appService.IsInTestMode() {
translationTextEntity.Status = entity.TranslationStatusNew.String()
translationTextEntity.Lang = lang.String()
translationTextEntity.Key = key.String()
}
ormService.Flush(translationTextEntity)
return key.String()
}
return translationTextEntity.Text
}
func (u *translationService) GetTextWithVars(
ormService *beeorm.Engine,
lang entity.TranslationTextLang,
key entity.TranslationTextKey,
variables map[string]interface{}) string {
text := u.GetText(ormService, lang, key)
for paramName, value := range variables {
text = strings.Replace(text, fmt.Sprintf("[[%s]]", paramName), fmt.Sprintf("%v", value), -1)
}
return text
}