Skip to content

Commit

Permalink
Simplify by doing it like on ios
Browse files Browse the repository at this point in the history
  • Loading branch information
sdassow committed Nov 21, 2024
1 parent a6166fd commit 4e80f8b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
17 changes: 7 additions & 10 deletions locale_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ package locale
#include <AppKit/AppKit.h>
char *preferredLocalization();
int preferredLocalizations(char ***);
const char * preferredLocalization();
const char * preferredLocalizations();
*/
import "C"
import (
Expand All @@ -19,7 +19,6 @@ import (
"regexp"
"strings"
"syscall"
"unsafe"
)

func execCommand(cmd string, args ...string) (status int, out string, err error) {
Expand Down Expand Up @@ -70,14 +69,12 @@ var appleLanguagesRegex = regexp.MustCompile(`([a-z]{2}(?:-[A-Z]{2})?)`)

// GetLocales retrieves the IETF BCP 47 language tags set on the system.
func GetLocales() ([]string, error) {
var locs **C.char

if n := C.preferredLocalizations(&locs); n > 0 {
r := make([]string, n)
for m, v := range unsafe.Slice(locs, n) {
r[m] = strings.Replace(C.GoString(v), "_", "-", 1)
str := C.preferredLocalizations()
if output := C.GoString(str); output != "" {
r := []string{}
for _, s := range strings.Split(output, ",") {
r = append(r, strings.Replace(s, "_", "-", 1))
}

return r, nil
}

Expand Down
33 changes: 6 additions & 27 deletions locale_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,14 @@

#import <Foundation/Foundation.h>

char* preferredLocalization() {
NSString *localization = [NSBundle mainBundle].preferredLocalizations.firstObject;
const char * preferredLocalization() {
NSString *locale = [[[NSBundle mainBundle] preferredLocalizations] firstObject];

return (char *) [localization UTF8String];
return [locale UTF8String];
}

int preferredLocalizations(char ***list) {
NSArray *locs = [NSBundle mainBundle].preferredLocalizations;
int nlocs = [locs count];
char **r = calloc(nlocs, sizeof(char *));
const char * preferredLocalizations() {
NSString *locales = [[[NSBundle mainBundle] preferredLocalizations] componentsJoinedByString:@","];

if (r == NULL)
return -1;

for (int n = 0; n < nlocs; n++) {
r[n] = strdup([locs[n] UTF8String]);
if (r[n] == NULL)
goto fail;
}

*list = r;

return nlocs;

fail:
for (int n = 0; n < nlocs; n++) {
if (r[n] != NULL)
free(r[n]);
}
free(r);
return -1;
return [locales UTF8String];
}

0 comments on commit 4e80f8b

Please sign in to comment.