-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Updated go-i18n API to support v2 and v1 #99
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package i18n | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
|
@@ -24,14 +25,17 @@ import ( | |
|
||
"github.com/pivotal-cf-experimental/jibber_jabber" | ||
|
||
go_i18n "github.com/nicksnyder/go-i18n/i18n" | ||
go_i18n "github.com/nicksnyder/go-i18n/v2/i18n" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
const ( | ||
DEFAULT_LOCALE = "en_US" | ||
DEFAULT_LANGUAGE = "en" | ||
) | ||
|
||
type TranslateFunc func(translateID string, args ...interface{}) string | ||
|
||
var SUPPORTED_LOCALES = map[string]string{ | ||
"de": "de_DE", | ||
"en": "en_US", | ||
|
@@ -44,24 +48,29 @@ var SUPPORTED_LOCALES = map[string]string{ | |
"ru": "ru_RU", | ||
"zh": "zh_CN", | ||
} | ||
var RESOUCES_PATH = filepath.Join("cf", "i18n", "resources") | ||
var ( | ||
RESOUCES_PATH = filepath.Join("cf", "i18n", "resources") | ||
bundle *go_i18n.Bundle | ||
) | ||
|
||
func GetResourcesPath() string { | ||
return RESOUCES_PATH | ||
} | ||
|
||
func Init(packageName string, i18nDirname string) go_i18n.TranslateFunc { | ||
userLocale, err := initWithUserLocale(packageName, i18nDirname) | ||
if err != nil { | ||
userLocale = mustLoadDefaultLocale(packageName, i18nDirname) | ||
func init() { | ||
if bundle == nil { | ||
bundle = go_i18n.NewBundle(language.AmericanEnglish) | ||
bundle.RegisterUnmarshalFunc("json", json.Unmarshal) | ||
} | ||
} | ||
|
||
T, err := go_i18n.Tfunc(userLocale, DEFAULT_LOCALE) | ||
func Init(packageName string, i18nDirname string) TranslateFunc { | ||
userLocale, err := initWithUserLocale(packageName, i18nDirname) | ||
if err != nil { | ||
panic(err) | ||
userLocale = mustLoadDefaultLocale(packageName, i18nDirname) | ||
} | ||
|
||
return T | ||
return Tfunc(userLocale, DEFAULT_LOCALE) | ||
} | ||
|
||
func initWithUserLocale(packageName, i18nDirname string) (string, error) { | ||
|
@@ -127,7 +136,7 @@ func loadFromAsset(packageName, assetPath, locale, language string) error { | |
return err | ||
} | ||
|
||
go_i18n.MustLoadTranslationFile(fileName) | ||
bundle.MustLoadMessageFile(fileName) | ||
|
||
os.RemoveAll(fileName) | ||
|
||
|
@@ -149,3 +158,72 @@ func saveLanguageFileToDisk(tmpDir, assetName string, byteArray []byte) (fileNam | |
|
||
return | ||
} | ||
|
||
func isNumber(value interface{}) bool { | ||
switch value.(type) { | ||
case int, int8, int16, int32, int64: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func SupportedLocaleLanguageTags() []language.Tag { | ||
tags := []language.Tag{language.English} | ||
for _, locale := range SUPPORTED_LOCALES { | ||
tag, _ := language.Parse(locale) | ||
tags = append(tags, tag) | ||
} | ||
|
||
return tags | ||
} | ||
|
||
// translate is a wrapper function that is based on the translate method for v1.3.0 | ||
// To allow compatibility v2.0+ and older a wrapper method was created | ||
// @see https://github.com/nicksnyder/go-i18n/blob/v1.3.0/i18n/bundle/bundle.go#L227-L257 | ||
func translate(loc *go_i18n.Localizer) TranslateFunc { | ||
return func(messageId string, args ...interface{}) string { | ||
var ( | ||
count interface{} | ||
data interface{} | ||
) | ||
|
||
if argc := len(args); argc > 0 { | ||
if isNumber(args[0]) { | ||
count = args[0] | ||
if argc > 1 { | ||
data = args[1] | ||
} | ||
} else { | ||
data = args[0] | ||
} | ||
} | ||
|
||
return loc.MustLocalize(&go_i18n.LocalizeConfig{ | ||
MessageID: messageId, | ||
TemplateData: data, | ||
PluralCount: count, | ||
}) | ||
} | ||
} | ||
|
||
// Tfunc will return a method of TranslateFunc type to be used to tranlation messages | ||
func Tfunc(sources ...string) TranslateFunc { | ||
localizer := go_i18n.NewLocalizer(bundle, DEFAULT_LOCALE) | ||
tfunc := translate(localizer) | ||
for _, s := range sources { | ||
if s == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Callout: If we can't determine the language source we will just skip it. I am open to other suggestions. |
||
continue | ||
} | ||
|
||
if s == DEFAULT_LOCALE { | ||
return tfunc | ||
} | ||
localizer := go_i18n.NewLocalizer(bundle, s) | ||
t := translate(localizer) | ||
return func(translationID string, args ...interface{}) string { | ||
return t(translationID, args...) | ||
} | ||
} | ||
|
||
return tfunc | ||
} |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Callout: To avoid disruption the API
i18n.TranslateFunc
method looks the same as theTranslateFunc
fromgoi18n
from version 1