-
-
Notifications
You must be signed in to change notification settings - Fork 472
feat: Русская локализация Portmaster UI - полный перевод интерфейса + поддержка i18n #2139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CoreForgeLabs
wants to merge
1
commit into
safing:development
Choose a base branch
from
CoreForgeLabs:development
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // Package i18n provides internationalization support for the Portmaster. | ||
| package i18n | ||
|
|
||
| import ( | ||
| "embed" | ||
| "encoding/json" | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/safing/portmaster/base/log" | ||
| ) | ||
|
|
||
| //go:embed translations/*.json | ||
| var translationsFS embed.FS | ||
|
|
||
| // Translation represents a single translation entry. | ||
| type Translation struct { | ||
| Name string `json:"name"` | ||
| Description string `json:"description"` | ||
| Category string `json:"category,omitempty"` | ||
| } | ||
|
|
||
| // Translations holds all translations for a language. | ||
| type Translations struct { | ||
| Language string `json:"language"` | ||
| Config map[string]Translation `json:"config"` | ||
| UI map[string]string `json:"ui"` | ||
| } | ||
|
|
||
| var ( | ||
| currentLang = "en" | ||
| translations = make(map[string]*Translations) | ||
| translationsMu sync.RWMutex | ||
| initialized bool | ||
| ) | ||
|
|
||
| // SupportedLanguages returns list of supported languages. | ||
| var SupportedLanguages = []string{"en", "ru"} | ||
|
|
||
| // Init initializes the i18n system. | ||
| func Init() error { | ||
| translationsMu.Lock() | ||
| defer translationsMu.Unlock() | ||
|
|
||
| if initialized { | ||
| return nil | ||
| } | ||
|
|
||
| for _, lang := range SupportedLanguages { | ||
| data, err := translationsFS.ReadFile(fmt.Sprintf("translations/%s.json", lang)) | ||
| if err != nil { | ||
| log.Warningf("i18n: failed to load translations for %s: %s", lang, err) | ||
| continue | ||
| } | ||
|
|
||
| var t Translations | ||
| if err := json.Unmarshal(data, &t); err != nil { | ||
| log.Warningf("i18n: failed to parse translations for %s: %s", lang, err) | ||
| continue | ||
| } | ||
|
|
||
| t.Language = lang | ||
| translations[lang] = &t | ||
| log.Infof("i18n: loaded %d config translations for %s", len(t.Config), lang) | ||
| } | ||
|
|
||
| initialized = true | ||
| return nil | ||
| } | ||
|
|
||
| // SetLanguage sets the current language. | ||
| func SetLanguage(lang string) error { | ||
| translationsMu.Lock() | ||
| defer translationsMu.Unlock() | ||
|
|
||
| if _, ok := translations[lang]; !ok { | ||
| return fmt.Errorf("i18n: language %s not supported", lang) | ||
| } | ||
|
|
||
| currentLang = lang | ||
| log.Infof("i18n: language set to %s", lang) | ||
| return nil | ||
| } | ||
|
|
||
| // GetLanguage returns the current language. | ||
| func GetLanguage() string { | ||
| translationsMu.RLock() | ||
| defer translationsMu.RUnlock() | ||
| return currentLang | ||
| } | ||
|
|
||
| // T returns translated string for UI key. | ||
| func T(key string) string { | ||
| translationsMu.RLock() | ||
| defer translationsMu.RUnlock() | ||
|
|
||
| if t, ok := translations[currentLang]; ok { | ||
| if val, ok := t.UI[key]; ok { | ||
| return val | ||
| } | ||
| } | ||
|
|
||
| // Fallback to English | ||
| if t, ok := translations["en"]; ok { | ||
| if val, ok := t.UI[key]; ok { | ||
| return val | ||
| } | ||
| } | ||
|
|
||
| return key | ||
| } | ||
|
|
||
| // GetConfigTranslation returns translation for a config key. | ||
| func GetConfigTranslation(key string) *Translation { | ||
| translationsMu.RLock() | ||
| defer translationsMu.RUnlock() | ||
|
|
||
| if t, ok := translations[currentLang]; ok { | ||
| if val, ok := t.Config[key]; ok { | ||
| return &val | ||
| } | ||
| } | ||
|
|
||
| // Fallback to English | ||
| if t, ok := translations["en"]; ok { | ||
| if val, ok := t.Config[key]; ok { | ||
| return &val | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // GetConfigName returns translated name for a config key. | ||
| func GetConfigName(key, fallback string) string { | ||
| if t := GetConfigTranslation(key); t != nil && t.Name != "" { | ||
| return t.Name | ||
| } | ||
| return fallback | ||
| } | ||
|
|
||
| // GetConfigDescription returns translated description for a config key. | ||
| func GetConfigDescription(key, fallback string) string { | ||
| if t := GetConfigTranslation(key); t != nil && t.Description != "" { | ||
| return t.Description | ||
| } | ||
| return fallback | ||
| } | ||
|
|
||
| // GetConfigCategory returns translated category for a config key. | ||
| func GetConfigCategory(key, fallback string) string { | ||
| if t := GetConfigTranslation(key); t != nil && t.Category != "" { | ||
| return t.Category | ||
| } | ||
| return fallback | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't change the process-wide language from a request handler.
i18n.SetLanguage(lang)mutates global state, so concurrent/config/options?lang=ruand/config/options?lang=enrequests can bleed into each other and change unrelated responses. Please resolve translations through a request-scoped API instead of flipping the global language here.🤖 Prompt for AI Agents