Initial implemention of lime.system.Locale

This commit is contained in:
Joshua Granick
2016-09-02 14:17:28 -07:00
parent 87573bb261
commit 8502635421
5 changed files with 229 additions and 1 deletions

130
lime/system/Locale.hx Normal file
View File

@@ -0,0 +1,130 @@
package lime.system;
import lime.system.CFFI;
#if flash
import flash.system.Capabilities;
#end
abstract Locale(String) from String to String {
public static var currentLocale:Locale;
public static var systemLocale (default, null):Locale;
public var language (get, never):String;
public var region (get, never):String;
private static function __init__ ():Void {
var locale = null;
#if flash
locale = Capabilities.language;
#elseif (js && html5)
locale = untyped navigator.language;
#elseif (lime_cffi && !macro)
locale = CFFI.load ("lime", "lime_locale_get_system_locale", 0) ();
#end
if (locale != null) {
systemLocale = locale;
} else {
systemLocale = "en-US";
}
currentLocale = systemLocale;
}
public function new (value:String) {
this = value;
}
// Get & Set Methods
private function get_language ():String {
if (this != null) {
var index = this.indexOf ("_");
if (index > -1) {
return this.substring (0, index);
}
index = this.indexOf ("-");
if (index > -1) {
return this.substring (0, index);
}
}
return this;
}
private function get_region ():String {
if (this != null) {
var underscoreIndex = this.indexOf ("_");
var dotIndex = this.indexOf (".");
var dashIndex = this.indexOf ("-");
if (underscoreIndex > -1) {
if (dotIndex > -1) {
return this.substring (underscoreIndex + 1, dotIndex);
} else {
return this.substring (underscoreIndex + 1);
}
} else if (dashIndex > -1) {
if (dotIndex > -1) {
return this.substring (dashIndex + 1, dotIndex);
} else {
return this.substring (dashIndex + 1);
}
}
}
return null;
}
}

View File

@@ -44,7 +44,6 @@
<compilerflag value="-Iinclude" />
<file name="src/ExternalInterface.cpp" />
<file name="src/hx/CFFIExt.cpp" unless="static_link" />
<section if="LIME_CAIRO">
@@ -227,6 +226,7 @@
<file name="src/math/Vector2.cpp" />
<file name="src/system/CFFIPointer.cpp" />
<file name="src/system/JNI.cpp" if="android" />
<file name="src/system/Locale.cpp" />
<file name="src/system/SensorEvent.cpp" />
<file name="src/ui/DropEvent.cpp" />
<file name="src/ui/GamepadEvent.cpp" />
@@ -238,6 +238,8 @@
<file name="src/ui/WindowEvent.cpp" />
<file name="src/utils/Bytes.cpp" />
<file name="src/hx/CFFIExt.cpp" unless="static_link" />
</files>
<include name="lib/cairo/files.xml" />

View File

@@ -0,0 +1,22 @@
#ifndef LIME_SYSTEM_LOCALE_H
#define LIME_SYSTEM_LOCALE_H
namespace lime {
class Locale {
public:
static char* GetSystemLocale ();
};
}
#endif

View File

@@ -24,6 +24,7 @@
#include <system/CFFIPointer.h>
#include <system/Clipboard.h>
#include <system/JNI.h>
#include <system/Locale.h>
#include <system/SensorEvent.h>
#include <system/System.h>
#include <text/Font.h>
@@ -1030,6 +1031,25 @@ namespace lime {
}
value lime_locale_get_system_locale () {
char* locale = Locale::GetSystemLocale ();
if (!locale) {
return alloc_null ();
} else {
value result = alloc_string (locale);
free (locale);
return result;
}
}
value lime_lzma_compress (value buffer) {
#ifdef LIME_LZMA
@@ -1699,6 +1719,7 @@ namespace lime {
DEFINE_PRIME2 (lime_jpeg_decode_bytes);
DEFINE_PRIME2 (lime_jpeg_decode_file);
DEFINE_PRIME2v (lime_key_event_manager_register);
DEFINE_PRIME0 (lime_locale_get_system_locale);
DEFINE_PRIME1 (lime_lzma_compress);
DEFINE_PRIME1 (lime_lzma_decompress);
DEFINE_PRIME2v (lime_mouse_event_manager_register);

View File

@@ -0,0 +1,53 @@
#include <system/Locale.h>
#if defined(HX_WINDOWS)
#include <windows.h>
#elif defined(HX_LINUX)
#include <clocale>
#endif
namespace lime {
char* Locale::GetSystemLocale () {
#if defined(HX_WINDOWS)
char locale[8];
int length = GetLocaleInfo (GetSystemDefaultUILanguage (), LOCALE_SISO639LANGNAME, locale, sizeof (locale));
char *result = (char*)malloc (length + 1);
strcpy (result, locale);
return result;
#elif defined(HX_LINUX)
const char* locale = getenv ("LANG");
if (!locale) {
locale = setlocale (LC_ALL, "");
}
if (locale) {
char *result = (char*)malloc (sizeof (locale));
strcpy (result, locale);
return result;
}
return 0;
#else
return 0;
#endif
}
}