-
Notifications
You must be signed in to change notification settings - Fork 13
/
localize.go
56 lines (47 loc) · 1.63 KB
/
localize.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
package registry
import (
"errors"
"log"
"os"
"github.com/sarulabs/di"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/config"
errorlogger "github.com/coretrix/hitrix/service/component/error_logger"
"github.com/coretrix/hitrix/service/component/localize"
)
func ServiceProviderLocalize(projectNameEnvVar string) *service.DefinitionGlobal {
return &service.DefinitionGlobal{
Name: service.LocalizeService,
Build: func(ctn di.Container) (interface{}, error) {
configService := ctn.Get(service.ConfigService).(config.IConfig)
var apiSource localize.Source
if _, ok := configService.StringMap("translation.poeditor"); ok {
apiKey, ok := configService.String("translation.poeditor.api_key")
if !ok {
return nil, errors.New("missing translation.poeditor.api_key")
}
projectID, ok := configService.String("translation.poeditor.project_id")
if !ok {
return nil, errors.New("missing translation.poeditor.project_id")
}
language, ok := configService.String("translation.poeditor.language")
if !ok {
return nil, errors.New("missing translation.poeditor.language")
}
apiSource = localize.NewPoeditorSource(
apiKey,
projectID,
language,
)
}
var path string
if projectNameEnvVar != "" {
path = configService.GetFolderPath() + "/../locale/" + os.Getenv(projectNameEnvVar)
} else {
path = configService.GetFolderPath() + "/../locale"
}
log.Println("Loading locale files from " + path)
return localize.NewSimpleLocalizer(ctn.Get(service.ErrorLoggerService).(errorlogger.ErrorLogger), apiSource, path), nil
},
}
}