Mercurial > projects > dwt2
diff base/src/java/nonstandard/Locale.d @ 114:46539f5c5993
Added implementation of ResourceBundle.
author | kntroh |
---|---|
date | Fri, 08 Apr 2011 20:12:20 +0900 |
parents | |
children | 3daece18b48e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base/src/java/nonstandard/Locale.d Fri Apr 08 20:12:20 2011 +0900 @@ -0,0 +1,110 @@ +/** + * Locale.d + * Information of a locale. + * Author: knt.roh + * License: Public Domain + */ +module java.nonstandard.locale; + +import java.lang.String; +import java.lang.util : implMissing; + +version(Tango){ + private import tango.text.locale.Core; +} else { // Phobos + private import std.conv; + private import std.exception; +} +version (Windows) { + private import std.c.string; + private import std.c.windows.windows; + private bool W_VERSION; + static this() { + W_VERSION = GetVersion < 0x80000000; + } + private extern (Windows) { + enum LCID : DWORD { + /// The default locale for the user or process. + LOCALE_USER_DEFAULT = 0x0400, + } + enum LCTYPE : DWORD { + /// ISO639 language name. + LOCALE_SISO639LANGNAME = 0x0059, + /// ISO3166 country name. + LOCALE_SISO3166CTRYNAME = 0x005A + } + /// Retrieves information about a locale specified by identifier. + /// See_Also: GetLocaleInfo Function (Windows) + /// (http://msdn.microsoft.com/en-us/library/dd318101%28VS.85%29.aspx) + INT GetLocaleInfoW( + LCID Locale, + LCTYPE LCType, + LPWSTR lpLCData, + INT cchData + ); + /// ditto + INT GetLocaleInfoA( + LCID Locale, + LCTYPE LCType, + LPCSTR lpLCData, + INT cchData + ); + } + /// A purpose of this templete is switch of W or A in Windows. + private String caltureNameImpl(Char, alias GetLocalInfo)() { + INT len; + Char[] res; + Char[] buf; + len = GetLocalInfo(LCID.LOCALE_USER_DEFAULT, + LCTYPE.LOCALE_SISO639LANGNAME, null, 0); + enforce(len, new Exception("LOCALE_SISO639LANGNAME (len)", __FILE__, __LINE__)); + buf.length = len; + len = GetLocalInfo(LCID.LOCALE_USER_DEFAULT, + LCTYPE.LOCALE_SISO639LANGNAME, buf.ptr, buf.length); + enforce(len, new Exception("LOCALE_SISO639LANGNAME", __FILE__, __LINE__)); + res ~= buf[0 .. len - 1]; + res ~= "-"; + len = GetLocalInfo(LCID.LOCALE_USER_DEFAULT, + LCTYPE.LOCALE_SISO3166CTRYNAME, null, 0); + enforce(len, new Exception("LOCALE_SISO3166CTRYNAME (len)", __FILE__, __LINE__)); + buf.length = len; + len = GetLocalInfo(LCID.LOCALE_USER_DEFAULT, + LCTYPE.LOCALE_SISO3166CTRYNAME, buf.ptr, buf.length); + enforce(len, new Exception("LOCALE_SISO3166CTRYNAME", __FILE__, __LINE__)); + res ~= buf[0 .. len - 1]; + return to!(String)(res); + } +} else version (Posix) { + private import std.process : getenv; + private import std.string : indexOf, replace; +} + +/// Get a omitted calture name. for example: "en-US" +String caltureName() { + version(Tango){ + return Culture.current.name; + } else { // Phobos + version (Windows) { + if (W_VERSION) { + return caltureNameImpl!(wchar, GetLocaleInfoW)(); + } else { + return caltureNameImpl!(char, GetLocaleInfoA)(); + } + } else version (Posix) { + // LC_ALL is override to settings of all category. + // This is undefined in almost case. + String res = .getenv("LC_ALL"); + if (!res || !res.length) { + // LANG is basic Locale setting. + // A settings of each category override this. + res = .getenv("LANG"); + } + int dot = .indexOf(res, '.'); + if (dot != -1) res = res[0 .. dot]; + return .replace(res, "_", "-"); + } else { + implMissing(__FILE__, __LINE__); + return ""; + } + } +}