Skip to content

Commit

Permalink
Optimize a map creation
Browse files Browse the repository at this point in the history
No need to re-create the AvailableLanguages map every time the function is
called, we can simply change it to a global variable. Unfortunately, this means
that it's now writeable, instead of being immutable, but as nothing is ever
modifying it, it's ok.
  • Loading branch information
jvoisin committed Dec 8, 2024
1 parent b5e28b4 commit d1633b9
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
4 changes: 2 additions & 2 deletions internal/locale/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type translationDict map[string]interface{}
type catalog map[string]translationDict

var defaultCatalog = make(catalog, len(AvailableLanguages()))
var defaultCatalog = make(catalog, len(AvailableLanguages))

//go:embed translations/*.json
var translationFiles embed.FS
Expand All @@ -31,7 +31,7 @@ func GetTranslationDict(language string) (translationDict, error) {
func LoadCatalogMessages() error {
var err error

for language := range AvailableLanguages() {
for language := range AvailableLanguages {
defaultCatalog[language], err = loadTranslationFile(language)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions internal/locale/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestLoadCatalog(t *testing.T) {
}

func TestAllKeysHaveValue(t *testing.T) {
for language := range AvailableLanguages() {
for language := range AvailableLanguages {
messages, err := loadTranslationFile(language)
if err != nil {
t.Fatalf(`Unable to load translation messages for language %q`, language)
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestMissingTranslations(t *testing.T) {
t.Fatal(`Unable to parse reference language`)
}

for language := range AvailableLanguages() {
for language := range AvailableLanguages {
if language == refLang {
continue
}
Expand All @@ -90,7 +90,7 @@ func TestMissingTranslations(t *testing.T) {
}

func TestTranslationFilePluralForms(t *testing.T) {
for language := range AvailableLanguages() {
for language := range AvailableLanguages {
messages, err := loadTranslationFile(language)
if err != nil {
t.Fatalf(`Unable to load translation messages for language %q`, language)
Expand Down
42 changes: 20 additions & 22 deletions internal/locale/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,24 @@ var numberOfPluralFormsPerLanguage = map[string]int{
"id_ID": 1,
}

// AvailableLanguages returns the list of available languages.
func AvailableLanguages() map[string]string {
return map[string]string{
"en_US": "English",
"es_ES": "Español",
"fr_FR": "Français",
"de_DE": "Deutsch",
"pl_PL": "Polski",
"pt_BR": "Português Brasileiro",
"zh_CN": "简体中文",
"zh_TW": "繁體中文",
"nl_NL": "Nederlands",
"ru_RU": "Русский",
"it_IT": "Italiano",
"ja_JP": "日本語",
"tr_TR": "Türkçe",
"el_EL": "Ελληνικά",
"fi_FI": "Suomi",
"hi_IN": "हिन्दी",
"uk_UA": "Українська",
"id_ID": "Bahasa Indonesia",
}
// AvailableLanguages is the list of available languages.
var AvailableLanguages = map[string]string{
"en_US": "English",
"es_ES": "Español",
"fr_FR": "Français",
"de_DE": "Deutsch",
"pl_PL": "Polski",
"pt_BR": "Português Brasileiro",
"zh_CN": "简体中文",
"zh_TW": "繁體中文",
"nl_NL": "Nederlands",
"ru_RU": "Русский",
"it_IT": "Italiano",
"ja_JP": "日本語",
"tr_TR": "Türkçe",
"el_EL": "Ελληνικά",
"fi_FI": "Suomi",
"hi_IN": "हिन्दी",
"uk_UA": "Українська",
"id_ID": "Bahasa Indonesia",
}
2 changes: 1 addition & 1 deletion internal/locale/locale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package locale // import "miniflux.app/v2/internal/locale"
import "testing"

func TestAvailableLanguages(t *testing.T) {
results := AvailableLanguages()
results := AvailableLanguages
for k, v := range results {
if k == "" {
t.Errorf(`Empty language key detected`)
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/settings_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (h *handler) showSettingsPage(w http.ResponseWriter, r *http.Request) {
"MarkAsReadOnlyOnPlayerCompletion": form.MarkAsReadOnlyOnPlayerCompletion,
})
view.Set("themes", model.Themes())
view.Set("languages", locale.AvailableLanguages())
view.Set("languages", locale.AvailableLanguages)
view.Set("timezones", timezones)
view.Set("menu", "settings")
view.Set("user", user)
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/settings_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h *handler) updateSettings(w http.ResponseWriter, r *http.Request) {
view := view.New(h.tpl, r, sess)
view.Set("form", settingsForm)
view.Set("themes", model.Themes())
view.Set("languages", locale.AvailableLanguages())
view.Set("languages", locale.AvailableLanguages)
view.Set("timezones", timezones)
view.Set("menu", "settings")
view.Set("user", loggedUser)
Expand Down
2 changes: 1 addition & 1 deletion internal/validator/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func validateTheme(theme string) *locale.LocalizedError {
}

func validateLanguage(language string) *locale.LocalizedError {
languages := locale.AvailableLanguages()
languages := locale.AvailableLanguages
if _, found := languages[language]; !found {
return locale.NewLocalizedError("error.invalid_language")
}
Expand Down

0 comments on commit d1633b9

Please sign in to comment.