aboutsummaryrefslogtreecommitdiffhomepage
path: root/locale/printer.go
blob: ef04e050cf3d4cf855026d7f5a08419a6662cd35 (plain)
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
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package locale // import "miniflux.app/locale"

import "fmt"

// Printer converts translation keys to language-specific strings.
type Printer struct {
	language string
}

// Printf is like fmt.Printf, but using language-specific formatting.
func (p *Printer) Printf(key string, args ...interface{}) string {
	var translation string

	str, found := defaultCatalog[p.language][key]
	if !found {
		translation = key
	} else {
		var valid bool
		translation, valid = str.(string)
		if !valid {
			translation = key
		}
	}

	return fmt.Sprintf(translation, args...)
}

// Plural returns the translation of the given key by using the language plural form.
func (p *Printer) Plural(key string, n int, args ...interface{}) string {
	choices, found := defaultCatalog[p.language][key]

	if found {
		var plurals []string

		switch v := choices.(type) {
		case []interface{}:
			for _, v := range v {
				plurals = append(plurals, fmt.Sprint(v))
			}
		case []string:
			plurals = v
		default:
			return key
		}

		pluralForm, found := pluralForms[p.language]
		if !found {
			pluralForm = pluralForms["default"]
		}

		index := pluralForm(n)
		if len(plurals) > index {
			return fmt.Sprintf(plurals[index], args...)
		}
	}

	return key
}

// NewPrinter creates a new Printer.
func NewPrinter(language string) *Printer {
	return &Printer{language}
}