forked from jeandeaual/go-locale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocale_android.go
116 lines (94 loc) · 3.04 KB
/
locale_android.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//go:build android
// +build android
package locale
/*
#cgo LDFLAGS: -landroid -llog
#include <stdlib.h>
const char *getLocales(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx);
const char *getLocale(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx);
const char *getLanguage(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx);
const char *getRegion(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx);
*/
import "C"
import (
"errors"
"strings"
"unsafe"
)
var (
errRunOnJVMNotSet error = errors.New("you first need to call SetRunOnJVM")
runOnJVM func(fn func(vm, env, ctx uintptr) error) error
)
// SetRunOnJVM sets the RunOnJVM function that will be called by this library.
// This can either be "golang.org/x/mobile/app".RunOnJVM or "github.com/fyne-io/mobile/app".RunOnJVM,
// depending on the mobile framework you're using (both can't be imported at the same time).
//
// RunOnJVM runs fn on a new goroutine locked to an OS thread with a JNIEnv.
//
// RunOnJVM blocks until the call to fn is complete. Any Java
// exception or failure to attach to the JVM is returned as an error.
//
// The function fn takes vm, the current JavaVM*,
// env, the current JNIEnv*, and
// ctx, a jobject representing the global android.context.Context.
func SetRunOnJVM(fn func(fn func(vm, env, ctx uintptr) error) error) {
runOnJVM = fn
}
// GetLocale retrieves the IETF BCP 47 language tag set on the system.
func GetLocale() (string, error) {
if runOnJVM == nil {
return "", errRunOnJVMNotSet
}
locale := ""
err := runOnJVM(func(vm, env, ctx uintptr) error {
chars := C.getLocale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))
locale = C.GoString(chars)
C.free(unsafe.Pointer(chars))
return nil
})
return locale, err
}
// GetLocales retrieves the IETF BCP 47 language tags set on the system.
func GetLocales() ([]string, error) {
if runOnJVM == nil {
return nil, errRunOnJVMNotSet
}
locales := ""
err := runOnJVM(func(vm, env, ctx uintptr) error {
chars := C.getLocales(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))
locales = C.GoString(chars)
C.free(unsafe.Pointer(chars))
return nil
})
return strings.Split(locales, ","), err
}
// GetLanguage retrieves the IETF BCP 47 language tag set on the system and
// returns the language part of the tag.
func GetLanguage() (string, error) {
if runOnJVM == nil {
return "", errRunOnJVMNotSet
}
language := ""
err := runOnJVM(func(vm, env, ctx uintptr) error {
chars := C.getLanguage(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))
language = C.GoString(chars)
C.free(unsafe.Pointer(chars))
return nil
})
return language, err
}
// GetRegion retrieves the IETF BCP 47 language tag set on the system and
// returns the region part of the tag.
func GetRegion() (string, error) {
if runOnJVM == nil {
return "", errRunOnJVMNotSet
}
region := ""
err := runOnJVM(func(vm, env, ctx uintptr) error {
chars := C.getRegion(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))
region = C.GoString(chars)
C.free(unsafe.Pointer(chars))
return nil
})
return region, err
}