-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from tetsuya28/i18y
i18y
- Loading branch information
Showing
9 changed files
with
190 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package config | ||
|
||
import "github.com/kelseyhightower/envconfig" | ||
|
||
type Config struct { | ||
SlackToken string `required:"true" envconfig:"SLACK_TOKEN"` | ||
SlackChannel string `required:"true" envconfig:"SLACK_CHANNEL"` | ||
Language string `required:"true" envconfig:"LANGUAGE" default:"ja"` | ||
} | ||
|
||
func New() (Config, error) { | ||
config := Config{} | ||
if err := envconfig.Process("", &config); err != nil { | ||
return Config{}, err | ||
} | ||
return config, nil | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package i18y | ||
|
||
import ( | ||
"embed" | ||
_ "embed" | ||
"fmt" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
|
||
"golang.org/x/text/language" | ||
"golang.org/x/text/message" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const ( | ||
LanguagesDir = "languages" | ||
) | ||
|
||
var ( | ||
languages = []language.Tag{ | ||
language.Japanese, | ||
language.English, | ||
} | ||
//go:embed languages/*.yaml | ||
configByte embed.FS | ||
) | ||
|
||
type Messages map[string]map[string]string | ||
|
||
func Init() error { | ||
files, err := configByte.ReadDir(LanguagesDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range files { | ||
data, err := fs.ReadFile(configByte, fmt.Sprintf("%s/%s", LanguagesDir, file.Name())) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var m map[string]string | ||
err = yaml.Unmarshal(data, &m) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Make language tag from file name | ||
l := language.Make(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))) | ||
for k, v := range m { | ||
err = message.SetString(l, k, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Translate(acceptLanguage string, msg string, args ...interface{}) string { | ||
t, _, err := language.ParseAcceptLanguage(acceptLanguage) | ||
if err != nil { | ||
return msg | ||
} | ||
|
||
matcher := language.NewMatcher(languages) | ||
tag, _, _ := matcher.Match(t...) | ||
p := message.NewPrinter(tag) | ||
return p.Sprintf(msg, args...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package i18y | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
func TestTranslate(t *testing.T) { | ||
err := Init() | ||
assert.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
acceptLanguage string | ||
key string | ||
out string | ||
}{ | ||
{ | ||
name: "japanese", | ||
acceptLanguage: language.Japanese.String(), | ||
key: "cost", | ||
out: "料金", | ||
}, | ||
{ | ||
name: "english", | ||
acceptLanguage: language.English.String(), | ||
key: "cost", | ||
out: "Cost", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
out := Translate(tt.acceptLanguage, tt.key) | ||
assert.Equal(t, tt.out, out) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
title: "%s cost of %s is `$%.3f`" | ||
cost: Cost | ||
usage: Usage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
title: "%s : %s のコスト : `$%.3f`" | ||
cost: 料金 | ||
usage: 利用量 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters