summaryrefslogtreecommitdiff
path: root/localizations/localizations.go
blob: 682ac6d5d7ca771ed7435459a268ed5124e050d7 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Code generated by go-localize; DO NOT EDIT.
// This file was generated by robots at
// 2021-05-12 12:51:18.836789452 -0600 MDT m=+0.005105757

package localizations

import (
	"bytes"
	"fmt"
	"strings"
	"text/template"
)

var localizations = map[string]string{
	"en-US.errors.bad_command": "Unrecognized Command.",
	"en-US.errors.bad_content_type": "Unintelligible content type: {{.mime}}",
	"en-US.errors.bad_feed": "Cannot interpret {{.meta}} as a feed",
	"en-US.errors.core_init": "Error in initialization: {{.error}}",
	"en-US.errors.corrupt_json_error": "comitium.json is corrupted: {{.error}}",
	"en-US.errors.gem_bad_response": "Unexpected Gemini response {{.code}} {{.meta}}",
	"en-US.errors.gopher_no_extract": "Gemini-style feeds served over gopher are currently unsupported.",
	"en-US.errors.http_bad_repsonse": "Unexpected HTTP response {{.status}}",
	"en-US.errors.http_no_extract": "Extracting feed from HTML pages is currently unsupported.",
	"en-US.errors.no_argument": "{{.command}} doesn't take an argument!",
	"en-US.errors.no_command": "Please provide a command.",
	"en-US.errors.no_data_dir": "Error finding a suitable directory! Falling back to the current working directory. See \"comitium help\" or comitium(1) for more information.",
	"en-US.errors.no_url_add": "Please provide a URL to add.",
	"en-US.errors.no_url_remove": "Please provide a URL to remove.",
	"en-US.errors.read_file_error": "error reading '{{.file}}': {{.error}}",
	"en-US.errors.saving": "Error writing '{{.file}}': {{.error}}",
	"en-US.errors.unsupported_protocol": "Unsupported protocol '{{.scheme}}'",
	"en-US.errors.url_add": "Error adding URL: {{.error}}",
	"en-US.errors.url_parse": "Error parsing URL: {{.error}}",
	"en-US.errors.version": "comitium v{{.version}}, commit {{.commit}}",
	"en-US.files.comitium_footer": "aggregated using comitium v{{.version}}-{{.commit}}",
	"en-US.files.feed_default_header": "comitium subscriptions",
	"en-US.files.feed_subscriptions_link": "Currently aggregating {{.num}} capsules, gopherholes, and websites.",
	"en-US.files.generated_on": "Generated on {{.date}}",
	"en-US.files.subscriptions_feeds_subtitle": "Gemini, Atom, RSS, and JSON feeds from all over.",
	"en-US.files.subscriptions_feeds_title": "feeds",
	"en-US.files.subscriptions_pages_subtitle": "Watching these pages for any changes that occur.",
	"en-US.files.subscriptions_pages_title": "pages",
	"en-US.files.subscriptions_subscribed_feeds": "subscribed feeds",
	"fr-FR.errors.bad_command": "Commande inconue.",
	"fr-FR.errors.bad_content_type": "Type de contenu inintelligible: {{.mime}}",
	"fr-FR.errors.bad_feed": "{{.meta}} n'est pas un flux reconnu",
	"fr-FR.errors.core_init": "Erreur d'initialisation: {{.error}}",
	"fr-FR.errors.corrupt_json_error": "comitium.json est corrompu: {{.error}}",
	"fr-FR.errors.gem_bad_response": "Réponse Gemini non attendue {{.code}} {{.meta}}",
	"fr-FR.errors.gopher_no_extract": "Les flux Gemini publiés via Gopher ne sont actuellement pas pris en charge.",
	"fr-FR.errors.http_bad_repsonse": "Réponse HTTP inattendue {{.status}}",
	"fr-FR.errors.http_no_extract": "L'extraction du flux à partir de pages HTML n'est actuellement pas prise en charge.",
	"fr-FR.errors.no_argument": "{{.command}} ne prend pas d'argument!",
	"fr-FR.errors.no_command": "Please provide a command.",
	"fr-FR.errors.no_data_dir": "Erreur lors de la recherche d'un répertoire approprié! Revenir au répertoire de travail actuel. Voir \"comitium help\" ou comitium (1) pour plus d'informations",
	"fr-FR.errors.no_url_add": "Merci d'indiquer une URL à ajouter.",
	"fr-FR.errors.no_url_remove": "Merci d'indiquer une URL à supprimer.",
	"fr-FR.errors.read_file_error": "erreur lors de la lecture de '{{.file}}': {{.error}}",
	"fr-FR.errors.saving": "erreur lors de l'écriture de '{{.file}}': {{.error}}",
	"fr-FR.errors.unsupported_protocol": "Protocol non supporté '{{.scheme}}'",
	"fr-FR.errors.url_add": "Erreur lors de l'ajout de l'URL: {{.error}}",
	"fr-FR.errors.url_parse": "Erreur lors de l'analyse de l'URL: {{.error}}",
	"fr-FR.errors.version": "comitium v{{.version}}, commit {{.commit}}",
	"fr-FR.files.comitium_footer": "Agrégé par comitium v{{.version}}-{{.commit}}",
	"fr-FR.files.feed_default_header": "Flux abonnés via comitium",
	"fr-FR.files.feed_subscriptions_link": "{{.num}} capsules, gopherholes et sites internet sont agrégés.",
	"fr-FR.files.generated_on": "Généré le {{.date}}",
	"fr-FR.files.subscriptions_feeds_subtitle": "Tous les flux Gemini, Atom, RSS et JSON.",
	"fr-FR.files.subscriptions_feeds_title": "flux",
	"fr-FR.files.subscriptions_pages_subtitle": "Ces pages sont surveillées pour tout changement.",
	"fr-FR.files.subscriptions_pages_title": "pages",
	"fr-FR.files.subscriptions_subscribed_feeds": "Abonnements",
}

type Replacements map[string]interface{}

type Localizer struct {
	Locale	 string
	FallbackLocale string
	Localizations  map[string]string
}

func New(locale string, fallbackLocale string) *Localizer {
	t := &Localizer{Locale: locale, FallbackLocale: fallbackLocale}
	t.Localizations = localizations
	return t
}

func (t Localizer) SetLocales(locale, fallback string) Localizer {
	t.Locale = locale
	t.FallbackLocale = fallback
	return t
}

func (t Localizer) SetLocale(locale string) Localizer {
	t.Locale = locale
	return t
}

func (t Localizer) SetFallbackLocale(fallback string) Localizer {
	t.FallbackLocale = fallback
	return t
}

func (t Localizer) GetWithLocale(locale, key string, replacements ...*Replacements) string {
	str, ok := t.Localizations[t.getLocalizationKey(locale, key)]
	if !ok {
		str, ok = t.Localizations[t.getLocalizationKey(t.FallbackLocale, key)]
		if !ok {
			return key
		}
	}

        // If the str doesn't have any substitutions, no need to
        // template.Execute.
	if strings.Index(str, "}}") == -1 {
                return str
        }

	return t.replace(str, replacements...)
}

func (t Localizer) Get(key string, replacements ...*Replacements) string {
	str := t.GetWithLocale(t.Locale, key, replacements...)
	return str
}

func (t Localizer) getLocalizationKey(locale string, key string) string {
	return fmt.Sprintf("%v.%v", locale, key)
}

func (t Localizer) replace(str string, replacements ...*Replacements) string {
	b := &bytes.Buffer{}
	tmpl, err := template.New("").Parse(str)
	if err != nil {
		return str
	}

	replacementsMerge := Replacements{}
	for _, replacement := range replacements {
		for k, v := range *replacement {
			replacementsMerge[k] = v
		}
	}

	err = template.Must(tmpl, err).Execute(b, replacementsMerge)
	if err != nil {
		return str
	}
	buff := b.String()
	return buff
}