Skip to content

Commit

Permalink
Simplify i18n
Browse files Browse the repository at this point in the history
Remove mutex
Remove GetLocale and LoadLocale functions
Simplify SetLocale and T functions
  • Loading branch information
bugfloyd committed May 4, 2024
1 parent 45b5665 commit 34ee239
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 52 deletions.
5 changes: 4 additions & 1 deletion bot/common/i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func TestCompleteTranslations(t *testing.T) {
languages := []Language{EnUS, FaIR}

for _, lang := range languages {
LoadLocale(lang) // Ensure the locale is loaded for testing
// Ensure the locale is loaded for testing
if _, ok := locales[lang]; !ok {
locales[lang] = loadLanguage(lang)
}

texts, ok := locales[lang]
if !ok {
Expand Down
51 changes: 16 additions & 35 deletions bot/common/i18n/translation.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package i18n

import (
"sync"
)

// TextID represents the keys for internationalized texts
type TextID string

Expand Down Expand Up @@ -64,35 +60,27 @@ type LocaleTexts map[TextID]string
// Locales holds the loaded languages and their texts
var (
locales = make(map[Language]LocaleTexts)
loadLock sync.Mutex
currentLocale Language
)

// SetLocale sets the current language for the application.
func SetLocale(lang Language) {
loadLock.Lock()
defer loadLock.Unlock()

if _, ok := locales[lang]; !ok {
locales[lang] = loadLanguage(lang)
func SetLocale(userLanguage Language, clientLanguage string) {
var language Language
if userLanguage != "" {
language = userLanguage
} else {
if clientLanguage == "fa" {
language = FaIR
} else if clientLanguage == "en" {
language = EnUS
} else {
language = EnUS
}
}
currentLocale = lang
}

// GetLocale returns the currently set locale.
func GetLocale() Language {
return currentLocale
}

// LoadLocale loads the locale data for a given language if it hasn't been loaded already.
func LoadLocale(lang Language) {
loadLock.Lock()
defer loadLock.Unlock()

// Check if the locale is already loaded
if _, ok := locales[lang]; !ok {
locales[lang] = loadLanguage(lang)
if _, ok := locales[language]; !ok {
locales[language] = loadLanguage(language)
}
currentLocale = language
}

// loadLanguage is a helper function that loads translations for a given language.
Expand All @@ -108,14 +96,7 @@ func loadLanguage(lang Language) LocaleTexts {

// T retrieves a text based on the given language and text ID.
func T(textID TextID) string {
lang := GetLocale() // Use the globally set locale

// Ensure the locale is loaded
if _, ok := locales[lang]; !ok {
LoadLocale(lang)
}

if text, ok := locales[lang][textID]; ok {
if text, ok := locales[currentLocale][textID]; ok {
return text
}
return ""
Expand Down
2 changes: 1 addition & 1 deletion bot/common/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (r *RootHandler) languageCallback(b *gotgbot.Bot, ctx *ext.Context, action
if err != nil {
return fmt.Errorf("failed to update user language: %w", err)
}
i18n.SetLocale(language)
i18n.SetLocale(r.user.Language, ctx.EffectiveUser.LanguageCode)

// Send update status
_, err = ctx.EffectiveMessage.Reply(b, i18n.T(i18n.LanguageUpdatedSuccessfullyText), nil)
Expand Down
16 changes: 1 addition & 15 deletions bot/common/root_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,7 @@ func (r *RootHandler) runCommand(b *gotgbot.Bot, ctx *ext.Context, command inter
}
r.user = user
r.userRepo = *userRepo

// Load locale
var language i18n.Language
if user.Language != "" {
language = user.Language
} else {
if ctx.EffectiveUser.LanguageCode == "fa" {
language = i18n.FaIR
} else if ctx.EffectiveUser.LanguageCode == "en" {
language = i18n.EnUS
} else {
language = i18n.EnUS
}
}
i18n.SetLocale(language)
i18n.SetLocale(user.Language, ctx.EffectiveUser.LanguageCode)

switch c := command.(type) {
case Command:
Expand Down

0 comments on commit 34ee239

Please sign in to comment.