diff --git a/lime/system/Locale.hx b/lime/system/Locale.hx new file mode 100644 index 000000000..f291ce31f --- /dev/null +++ b/lime/system/Locale.hx @@ -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; + + } + + +} \ No newline at end of file diff --git a/project/Build.xml b/project/Build.xml index e5cd401ae..4b73bc383 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -44,7 +44,6 @@ -
@@ -227,6 +226,7 @@ + @@ -238,6 +238,8 @@ + + diff --git a/project/include/system/Locale.h b/project/include/system/Locale.h new file mode 100644 index 000000000..ef0cf3669 --- /dev/null +++ b/project/include/system/Locale.h @@ -0,0 +1,22 @@ +#ifndef LIME_SYSTEM_LOCALE_H +#define LIME_SYSTEM_LOCALE_H + + +namespace lime { + + + class Locale { + + + public: + + static char* GetSystemLocale (); + + + }; + + +} + + +#endif \ No newline at end of file diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index 19261c9c7..06c25c72b 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -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); diff --git a/project/src/system/Locale.cpp b/project/src/system/Locale.cpp new file mode 100644 index 000000000..add9aaded --- /dev/null +++ b/project/src/system/Locale.cpp @@ -0,0 +1,53 @@ +#include + +#if defined(HX_WINDOWS) +#include +#elif defined(HX_LINUX) +#include +#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 + + } + + +} \ No newline at end of file