Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
vorlif committed Jun 1, 2022
1 parent 3daf9fa commit 7d55247
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 6 additions & 6 deletions catalog/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (jsonDecoder) Decode(lang language.Tag, domain string, data []byte) (Catalo
return nil, fmt.Errorf("spreak: File contains no translations lang=%v domain=%q", lang, domain)
}

catl := &JSONCatalog{
catl := &jsonCatalog{
lookupMap: make(map[string]map[string]*jsonMessage),
domain: domain,
language: lang,
Expand All @@ -50,7 +50,7 @@ func (jsonDecoder) Decode(lang language.Tag, domain string, data []byte) (Catalo
return catl, nil
}

type JSONCatalog struct {
type jsonCatalog struct {
// Map for a quick lookup of messages.
// First key is the context and second the msg key (e.g. lookup["context"]["app.name"]).
lookupMap map[string]map[string]*jsonMessage
Expand All @@ -59,7 +59,7 @@ type JSONCatalog struct {
pluralSet *cldrplural.RuleSet
}

func (m *JSONCatalog) GetTranslation(ctx, msgID string) (string, error) {
func (m *jsonCatalog) GetTranslation(ctx, msgID string) (string, error) {
tr, err := m.getTranslation(ctx, msgID, cldrplural.Other)
if err != nil {
return msgID, err
Expand All @@ -68,7 +68,7 @@ func (m *JSONCatalog) GetTranslation(ctx, msgID string) (string, error) {
return tr, nil
}

func (m *JSONCatalog) GetPluralTranslation(ctx, msgID string, n interface{}) (string, error) {
func (m *jsonCatalog) GetPluralTranslation(ctx, msgID string, n interface{}) (string, error) {
cat := m.pluralSet.Evaluate(n)
tr, err := m.getTranslation(ctx, msgID, cat)
if err != nil {
Expand All @@ -78,9 +78,9 @@ func (m *JSONCatalog) GetPluralTranslation(ctx, msgID string, n interface{}) (st
return tr, nil
}

func (m JSONCatalog) Language() language.Tag { return m.language }
func (m jsonCatalog) Language() language.Tag { return m.language }

func (m *JSONCatalog) getTranslation(ctx, key string, cat cldrplural.Category) (string, error) {
func (m *jsonCatalog) getTranslation(ctx, key string, cat cldrplural.Category) (string, error) {
if ctx != "" {
key += "_" + ctx
}
Expand Down
5 changes: 4 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// For JSON files only the CLDR plural rules are supported.
// For po and mo files both gettext plural forms and CLDR plural rules are supported.
// The CLDR rules provides better support when floating point numbers are used.
// When using the CLDR plural rules, a notation increasing from "Zero" to "Other" should be used.
// When using the CLDR plural rules with po files, a notation increasing from "Zero" to "Other" should be used.
// For example, if the used language supports "Zero", "Few" and "Other", Zero should be notated as entry 0, Few as entry 1 and Other as entry 2.
// It is also recommended to define a gettext compatible plural rule.
// On the website https://php-gettext.github.io/Languages/ you can find a list of gettext plural rules which are compatible to the CLDR plural rules.
Expand All @@ -43,4 +43,7 @@
// msgstr[0] "Translation with the plural form One"
// msgstr[1] "Translation with the plural form Few"
// msgstr[2] "Translation with the plural form Other"
//
// If floating point numbers are used, it is recommended to pass them formatted as strings as they will be displayed later.
// For example, if the number n is to be displayed with two numbers after the decimal point, it should be formatted with fmt.Sprintf("%.2f", n).
package spreak

0 comments on commit 7d55247

Please sign in to comment.