Skip to content
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

feat(native): add Locale module #938

Merged
merged 11 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi

if [[ $native_output ]]
then
printf "\nFormatted the following native stubs:\n%s" "$native_output"
printf "\nFormatted the following native stubs:\n%s\n" "$native_output"
fi

if [[ $caml_output != 0 ]] || [[ $native_output != "" ]]
Expand Down
2 changes: 2 additions & 0 deletions src/Core/App.re
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ let start = init => {

Revery_Native.initApp();

AppLog.infof(m => m("Operating in locale : %s", Environment.userLocale));

let appLoop = () => {
_flushEvents();

Expand Down
3 changes: 3 additions & 0 deletions src/Core/Environment.re
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,6 @@ let getAssetPath = p => {
let getWorkingDirectory = () => Sys.getcwd();

let getTempDirectory = () => Filename.get_temp_dir_name();

let getUserLocale = () => Revery_Native.Locale.getUser();
let userLocale = getUserLocale();
7 changes: 7 additions & 0 deletions src/Core/Environment.rei
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ type os =
| Unknown;

let os: os;

/**
[getUserLocale] returns the current user locale. Note that on some platforms
(including macOS) the locale can change during runtime
*/
let getUserLocale: unit => string;
zbaylin marked this conversation as resolved.
Show resolved Hide resolved
let userLocale: string;
1 change: 1 addition & 0 deletions src/Native/Locale.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
external getUser: unit => string = "revery_getUserLocale";
5 changes: 4 additions & 1 deletion src/Native/ReveryCocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void revery_scheduleNotificationFromNow_cocoa(const char *title,
const char *body,
long onClickFunc, int mute,
int seconds);
void* revery_getIconHandle_cocoa();
void *revery_getIconHandle_cocoa();

/* Icon progress bar functions */
void revery_setIconProgress_cocoa(void* dt, double progress);
Expand All @@ -22,3 +22,6 @@ void revery_hideIconProgress_cocoa(void* ip);
/* Open functions */
int revery_openURL_cocoa(const char *url_string);
int revery_openFile_cocoa(const char *path_string);

/* Locale functions */
char *revery_getUserLocale_cocoa();
1 change: 1 addition & 0 deletions src/Native/ReveryWin32.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ void revery_setIconProgress_win32(void *win, void *ih, float progress);
void revery_setIconProgressIndeterminate_win32(void *win, void *ih);
void revery_hideIconProgress_win32(void *win, void *ih);
int revery_openURL_win32(const char *url_string);
char *revery_getUserLocale_win32();
1 change: 1 addition & 0 deletions src/Native/Revery_Native.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module Dialog = Dialog;
module Icon = Icon;
module Notification = Notification;
module Shell = Shell;
module Locale = Locale;

include Initialization;
1 change: 1 addition & 0 deletions src/Native/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
notification notification_cocoa
icon icon_cocoa icon_win32
shell shell_cocoa shell_gtk shell_win32
locale locale_cocoa locale_win32
utilities
ReveryAppDelegate ReveryAppDelegate_func
ReveryProgressBar)
Expand Down
2 changes: 1 addition & 1 deletion src/Native/icon_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ void revery_hideIconProgress_win32(void *win, void *ih) {
iconHandle->lpVtbl->SetProgressState(iconHandle, window, TBPF_NOPROGRESS);
}

#endif
#endif
34 changes: 34 additions & 0 deletions src/Native/locale.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>

#include <caml/alloc.h>
#include <caml/callback.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>
#include <string.h>

#include "caml_values.h"

#ifdef WIN32
#include "ReveryWin32.h"
#elif __APPLE__
#include "ReveryCocoa.h"
#else
#include "ReveryGtk.h"
#include <locale.h>
#endif

CAMLprim value revery_getUserLocale() {
CAMLparam0();
CAMLlocal1(camlRet);
char *ret;
#ifdef __APPLE__
ret = revery_getUserLocale_cocoa();
#elif WIN32
ret = revery_getUserLocale_win32();
#else
setlocale(LC_CTYPE, "");
ret = setlocale(LC_CTYPE, NULL);
#endif
camlRet = caml_copy_string(ret);
CAMLreturn(camlRet);
}
10 changes: 10 additions & 0 deletions src/Native/locale_cocoa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifdef __APPLE__
#include <stdio.h>

#import <Cocoa/Cocoa.h>

char *revery_getUserLocale_cocoa() {
NSLocale *nsLocale = [NSLocale currentLocale];
return (char *)[[nsLocale localeIdentifier] UTF8String];
zbaylin marked this conversation as resolved.
Show resolved Hide resolved
}
#endif
18 changes: 18 additions & 0 deletions src/Native/locale_win32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifdef _WIN32

#include "win32_target.h"

#include <windows.h>
#include <stdlib.h>

char *revery_getUserLocale_win32() {
WCHAR locale[LOCALE_NAME_MAX_LENGTH];
char *localeStr = malloc(LOCALE_NAME_MAX_LENGTH * sizeof(char));
zbaylin marked this conversation as resolved.
Show resolved Hide resolved

GetUserDefaultLocaleName(locale, LOCALE_NAME_MAX_LENGTH);

wcstombs(localeStr, locale, sizeof(localeStr));
return localeStr;
}

#endif
2 changes: 2 additions & 0 deletions src/Native/win32_target.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define WINVER 0x0601
#define _WIN32_WINNT 0x0601