-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(v3): add cross-platform SystemLocale() API (BCP-47 language tag) #5894
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
mortenolsrud
wants to merge
1
commit into
wailsapp:master
Choose a base branch
from
mortenolsrud:feat/system-locale-api
base: master
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
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,14 @@ | ||
| //go:build android | ||
|
|
||
| package application | ||
|
|
||
| // SystemLocale returns the device's configured locale as a BCP-47 language tag | ||
| // (e.g. "nb-NO", "en-US"). On Android this calls Locale.getDefault().toLanguageTag() | ||
| // via the WailsBridge. | ||
| func SystemLocale() string { | ||
| s, _ := androidBridgeString("getLocale") | ||
| if s == "" { | ||
| return "en" | ||
| } | ||
| return s | ||
| } | ||
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,42 @@ | ||
| //go:build darwin && !ios && !server | ||
|
|
||
| package application | ||
|
|
||
| /* | ||
| #cgo CFLAGS: -x objective-c | ||
| #cgo LDFLAGS: -framework Foundation | ||
| #import <Foundation/Foundation.h> | ||
| #include <stdlib.h> | ||
|
|
||
| static const char* getSystemLocale() { | ||
| // Use languageIdentifier (macOS 13+ / iOS 16+) for clean BCP-47 output. | ||
| // localeIdentifier can include POSIX modifiers like @collation=pinyin. | ||
| if (@available(macOS 13, *)) { | ||
| NSString *tag = [[NSLocale currentLocale] languageIdentifier]; | ||
| return strdup([tag UTF8String]); | ||
| } | ||
| // Fallback for macOS < 13: build from components to preserve script subtags | ||
| // (e.g. zh-Hant-TW, not just zh-TW). | ||
| NSLocale *locale = [NSLocale currentLocale]; | ||
| NSString *lang = [locale languageCode]; | ||
| NSString *script = [locale scriptCode]; | ||
| NSString *country = [locale countryCode]; | ||
| NSMutableString *tag = [NSMutableString stringWithString:lang ?: @"en"]; | ||
| if (script.length > 0) [tag appendFormat:@"-%@", script]; | ||
| if (country.length > 0) [tag appendFormat:@"-%@", country]; | ||
| return strdup([tag UTF8String]); | ||
| } | ||
| */ | ||
| import "C" | ||
| import "unsafe" | ||
|
|
||
| // SystemLocale returns the system's configured locale as a BCP-47 language tag | ||
| // (e.g. "nb-NO", "en-US"). | ||
| func SystemLocale() string { | ||
| cStr := C.getSystemLocale() | ||
| if cStr == nil { | ||
| return "en" | ||
| } | ||
| defer C.free(unsafe.Pointer(cStr)) | ||
| return C.GoString(cStr) | ||
| } |
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,10 @@ | ||
| //go:build ios | ||
|
|
||
| package application | ||
|
|
||
| // SystemLocale returns the device's configured locale as a BCP-47 language tag | ||
| // (e.g. "nb-NO", "en-US"). Delegates to the iOS mobile manager which calls | ||
| // ios_system_locale() via the CGO preamble in mobile_features_ios.go. | ||
| func SystemLocale() string { | ||
| return IOS.SystemLocale() | ||
| } |
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,38 @@ | ||
| //go:build linux && !android && !server | ||
|
|
||
| package application | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| // SystemLocale returns the system's configured locale as a BCP-47 language tag | ||
| // (e.g. "nb-NO", "en-US"). Reads from LANG/LC_ALL/LC_MESSAGES environment | ||
| // variables and normalizes to BCP-47 format. | ||
| func SystemLocale() string { | ||
| // Check in order of specificity | ||
| for _, env := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} { | ||
| if val := os.Getenv(env); val != "" { | ||
| return parsePosixLocale(val) | ||
| } | ||
| } | ||
| return "en" | ||
| } | ||
|
|
||
| // parsePosixLocale converts a POSIX locale (e.g. "nb_NO.UTF-8") to BCP-47 ("nb-NO"). | ||
| func parsePosixLocale(posix string) string { | ||
| // Strip encoding (.UTF-8) and modifier (@euro) | ||
| if i := strings.IndexByte(posix, '.'); i >= 0 { | ||
| posix = posix[:i] | ||
| } | ||
| if i := strings.IndexByte(posix, '@'); i >= 0 { | ||
| posix = posix[:i] | ||
| } | ||
| // Replace underscore with hyphen (nb_NO → nb-NO) | ||
| posix = strings.ReplaceAll(posix, "_", "-") | ||
| if posix == "" || posix == "C" || posix == "POSIX" { | ||
| return "en" | ||
| } | ||
| return posix | ||
| } |
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,34 @@ | ||
| //go:build server | ||
|
|
||
| package application | ||
|
|
||
| import "os" | ||
| import "strings" | ||
|
|
||
| // SystemLocale returns the system's configured locale as a BCP-47 language tag. | ||
| // In server mode, reads from environment variables. | ||
| func SystemLocale() string { | ||
| for _, env := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} { | ||
| if val := os.Getenv(env); val != "" { | ||
| return parsePosixLocaleBCP47(val) | ||
| } | ||
| } | ||
| return "en" | ||
| } | ||
|
|
||
| // parsePosixLocaleBCP47 converts a POSIX locale (e.g. "nb_NO.UTF-8@euro") to BCP-47 ("nb-NO"). | ||
| func parsePosixLocaleBCP47(posix string) string { | ||
| // Strip encoding (.UTF-8) and modifier (@euro) | ||
| if i := strings.IndexByte(posix, '.'); i >= 0 { | ||
| posix = posix[:i] | ||
| } | ||
| if i := strings.IndexByte(posix, '@'); i >= 0 { | ||
| posix = posix[:i] | ||
| } | ||
| // Replace underscore with hyphen (nb_NO → nb-NO) | ||
| posix = strings.ReplaceAll(posix, "_", "-") | ||
| if posix == "" || posix == "C" || posix == "POSIX" { | ||
| return "en" | ||
| } | ||
| return posix | ||
| } |
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,27 @@ | ||
| //go:build windows && !server | ||
|
|
||
| package application | ||
|
|
||
| import ( | ||
| "syscall" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| var ( | ||
| kernel32 = syscall.NewLazyDLL("kernel32.dll") | ||
| procGetUserDefaultLocaleName = kernel32.NewProc("GetUserDefaultLocaleName") | ||
| ) | ||
|
|
||
| // SystemLocale returns the system's configured locale as a BCP-47 language tag | ||
| // (e.g. "nb-NO", "en-US"). | ||
| func SystemLocale() string { | ||
| buf := make([]uint16, 85) // LOCALE_NAME_MAX_LENGTH | ||
| r, _, _ := procGetUserDefaultLocaleName.Call( | ||
| uintptr(unsafe.Pointer(&buf[0])), | ||
| uintptr(len(buf)), | ||
| ) | ||
| if r == 0 { | ||
| return "en" | ||
| } | ||
| return syscall.UTF16ToString(buf) | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.