Skip to content

Commit

Permalink
Sort languages automatically
Browse files Browse the repository at this point in the history
Simplify other parts of the related code.

Signed-off-by: Jo Vandeginste <[email protected]>
  • Loading branch information
jovandeginste committed Jan 15, 2025
1 parent 412f36e commit 0eb4a8c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
54 changes: 38 additions & 16 deletions views/helpers/language.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package helpers

import (
"cmp"
"context"
"fmt"
"slices"
"strings"
"time"

Expand All @@ -13,7 +15,23 @@ import (
"golang.org/x/text/language/display"
)

var englishTag = display.English.Languages()
var (
englishTag = display.English.Languages()

languagesSorted = false
languages = []language.Tag{
language.Dutch,
language.English,
language.Finnish,
language.French,
language.German,
language.Indonesian,
language.Italian,
language.Norwegian,
language.Persian,
language.Russian,
}
)

func THas(ctx context.Context, key string, args ...any) string {
if i18n.Has(ctx, key) {
Expand All @@ -34,22 +52,26 @@ func Language(ctx context.Context) string {
return translator(ctx).Code().String()
}

func SupportedLanguages(ctx context.Context) []language.Tag {
return []language.Tag{
language.Dutch,
language.English,
language.French,
language.German,
language.Indonesian,
language.Italian,
language.Norwegian,
language.Persian,
language.Russian,
func SupportedLanguages() []language.Tag {
sortLanguages()

return languages
}

func sortLanguages() {
if languagesSorted {
return
}

slices.SortFunc(languages, func(a, b language.Tag) int {
return cmp.Compare(a.String(), b.String())
})

languagesSorted = true
}

func ToLanguageInformation(code string) LanguageInformation {
cc := code
func ToLanguageInformation(code language.Tag) LanguageInformation {
cc := code.String()
if strings.Contains(cc, "-") {
cc = strings.Split(cc, "-")[1]
}
Expand All @@ -59,15 +81,15 @@ func ToLanguageInformation(code string) LanguageInformation {
}

l := LanguageInformation{
Code: code,
Code: code.String(),
Flag: emojiflag.GetFlag(cc),
}

if l.Flag == "" {
l.Flag = "👽"
}

localTag := language.MustParse(code)
localTag := language.MustParse(code.String())
l.LocalName = display.Self.Name(localTag)
l.EnglishName = englishTag.Name(localTag)

Expand Down
4 changes: 2 additions & 2 deletions views/partials/partials.templ
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ templ Alerts() {

templ Language() {
<select class="border-0" onchange="changeLanguage(this.value)">
for _, sl := range helpers.SupportedLanguages(ctx) {
{{ linf := helpers.ToLanguageInformation(sl.String()) }}
for _, sl := range helpers.SupportedLanguages() {
{{ linf := helpers.ToLanguageInformation(sl) }}
<option value={ linf.Code } selected?={ linf.Code == helpers.Language(ctx) }>
{ linf.Flag } { linf.LocalName }
if linf.EnglishName != "" && linf.EnglishName != linf.LocalName {
Expand Down
4 changes: 2 additions & 2 deletions views/partials/partials_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions views/user/profile_preferred_units.templ
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ templ profileLanguage(ulang string) {
<option value="browser" selected?={ ulang == "browser" }>
🌐 Browser language
</option>
for _, sl := range helpers.SupportedLanguages(ctx) {
{{ linf := helpers.ToLanguageInformation(sl.String()) }}
for _, sl := range helpers.SupportedLanguages() {
{{ linf := helpers.ToLanguageInformation(sl) }}
<option value={ linf.Code } selected?={ linf.Code == ulang }>
{ linf.Flag } { linf.LocalName }
if linf.EnglishName != "" {
Expand Down
4 changes: 2 additions & 2 deletions views/user/profile_preferred_units_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0eb4a8c

Please sign in to comment.