00001 /******************************************************************************* 00002 00003 @file ULocale.d 00004 00005 Copyright (C) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, October 2004 00031 @author Kris 00032 00033 Note that this package and documentation is built around the ICU 00034 project (http://oss.software.ibm.com/icu/). Below is the license 00035 statement as specified by that software: 00036 00037 00038 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00039 00040 00041 ICU License - ICU 1.8.1 and later 00042 00043 COPYRIGHT AND PERMISSION NOTICE 00044 00045 Copyright (c) 1995-2003 International Business Machines Corporation and 00046 others. 00047 00048 All rights reserved. 00049 00050 Permission is hereby granted, free of charge, to any person obtaining a 00051 copy of this software and associated documentation files (the 00052 "Software"), to deal in the Software without restriction, including 00053 without limitation the rights to use, copy, modify, merge, publish, 00054 distribute, and/or sell copies of the Software, and to permit persons 00055 to whom the Software is furnished to do so, provided that the above 00056 copyright notice(s) and this permission notice appear in all copies of 00057 the Software and that both the above copyright notice(s) and this 00058 permission notice appear in supporting documentation. 00059 00060 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00061 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00062 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00063 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00064 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00065 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00066 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00067 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00068 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00069 00070 Except as contained in this notice, the name of a copyright holder 00071 shall not be used in advertising or otherwise to promote the sale, use 00072 or other dealings in this Software without prior written authorization 00073 of the copyright holder. 00074 00075 ---------------------------------------------------------------------- 00076 00077 All trademarks and registered trademarks mentioned herein are the 00078 property of their respective owners. 00079 00080 *******************************************************************************/ 00081 00082 module mango.icu.ULocale; 00083 00084 private import mango.icu.ICU; 00085 00086 /******************************************************************************* 00087 00088 Note that this is a struct rather than a class. This is so 00089 that one can easily construct these on the stack, plus the 00090 'convenience' instances can be created statically. 00091 00092 *******************************************************************************/ 00093 00094 struct ULocale 00095 { 00096 public char[] name; 00097 00098 /*********************************************************************** 00099 00100 ***********************************************************************/ 00101 00102 public static ULocale Root = {""}; 00103 public static ULocale Default = {null}; 00104 public static ULocale English = {"en"}; 00105 public static ULocale Chinese = {"zh"}; 00106 public static ULocale French = {"fr"}; 00107 public static ULocale German = {"de"}; 00108 public static ULocale Italian = {"it"}; 00109 public static ULocale Japanese = {"ja"}; 00110 public static ULocale Korean = {"ko"}; 00111 public static ULocale SimplifiedChinese = {"zh_CN"}; 00112 public static ULocale TraditionalChinese = {"zh_TW"}; 00113 public static ULocale Canada = {"en_CA"}; 00114 public static ULocale CanadaFrench = {"fr_CA"}; 00115 public static ULocale China = {"zh_CN"}; 00116 public static ULocale PRC = {"zh_CN"}; 00117 public static ULocale France = {"fr_FR"}; 00118 public static ULocale Germany = {"de_DE"}; 00119 public static ULocale Italy = {"it_IT"}; 00120 public static ULocale Japan = {"jp_JP"}; 00121 public static ULocale Korea = {"ko_KR"}; 00122 public static ULocale Taiwan = {"zh_TW"}; 00123 public static ULocale UK = {"en_GB"}; 00124 public static ULocale US = {"en_US"}; 00125 00126 /*********************************************************************** 00127 00128 ***********************************************************************/ 00129 00130 public const uint LanguageCapacity = 12; 00131 public const uint CountryCapacity = 4; 00132 public const uint FullNameCapacity = 56; 00133 public const uint ScriptCapacity = 6; 00134 public const uint KeywordsCapacity = 50; 00135 public const uint KeywordAndValuesCapacity = 100; 00136 public const char KeywordItemSeperator = ':'; 00137 public const char KeywordSeperator = '@'; 00138 public const char KeywordAssign = '='; 00139 00140 /*********************************************************************** 00141 00142 ***********************************************************************/ 00143 00144 static void getDefault (inout ULocale locale) 00145 { 00146 locale.name = ICU.toArray (uloc_getDefault()); 00147 if (! locale.name) 00148 ICU.exception ("failed to get default locale"); 00149 } 00150 00151 /*********************************************************************** 00152 00153 ***********************************************************************/ 00154 00155 static void setDefault (inout ULocale locale) 00156 { 00157 ICU.Error e; 00158 00159 uloc_setDefault (ICU.toString(locale.name), e); 00160 if (ICU.isError (e)) 00161 ICU.exception ("invalid locale '"~locale.name~"'"); 00162 } 00163 00164 00165 /*********************************************************************** 00166 00167 Bind the ICU functions from a shared library. This is 00168 complicated by the issues regarding D and DLLs on the 00169 Windows platform 00170 00171 ***********************************************************************/ 00172 00173 version (Win32) 00174 { 00175 private static void* library; 00176 private static char[] libraryName = "icuuc30.dll"; 00177 00178 /*************************************************************** 00179 00180 ***************************************************************/ 00181 00182 private static extern (C) 00183 { 00184 char* function () uloc_getDefault; 00185 void function (char*, inout ICU.Error) uloc_setDefault; 00186 } 00187 00188 /*************************************************************** 00189 00190 ***************************************************************/ 00191 00192 static FunctionLoader.Bind[] targets = 00193 [ 00194 {cast(void**) &uloc_getDefault, "uloc_getDefault"}, 00195 {cast(void**) &uloc_setDefault, "uloc_setDefault"}, 00196 ]; 00197 00198 /*************************************************************** 00199 00200 ***************************************************************/ 00201 00202 static this () 00203 { 00204 library = FunctionLoader.bind (libraryName, targets); 00205 } 00206 00207 /*************************************************************** 00208 00209 ***************************************************************/ 00210 00211 static ~this () 00212 { 00213 FunctionLoader.unbind (library); 00214 } 00215 } 00216 00217 version (linux) 00218 { 00219 /*************************************************************** 00220 00221 ***************************************************************/ 00222 00223 private static extern (C) 00224 { 00225 char* uloc_getDefault (); 00226 void uloc_setDefault (char*, uint*); 00227 } 00228 } 00229 }