Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.3 KiB

  1. using System;
  2. using System.Threading;
  3. using System.Resources;
  4. using System.Globalization;
  5. using System.Web;
  6. using Microsoft.Win32;
  7. using UDDI.Diagnostics;
  8. namespace UDDI
  9. {
  10. public class Localization
  11. {
  12. protected static ResourceManager manager = null;
  13. static Localization()
  14. {
  15. try
  16. {
  17. Debug.VerifySetting( "InstallRoot" );
  18. manager = ResourceManager.CreateFileBasedResourceManager( "uddi",
  19. Config.GetString( "InstallRoot" ) + "resources",
  20. null );
  21. }
  22. catch
  23. {
  24. //
  25. // There may be a case where we get called before the database and hence our configuration
  26. // settings are setup. The only information we need is the install root. If we can't get
  27. // this information from the database, we'll try to get it from the registry.
  28. //
  29. //
  30. // TODO should make this field non-private on Config
  31. //
  32. string registryRoot = @"SOFTWARE\Microsoft\UDDI";
  33. RegistryKey uddiRegKey = Registry.LocalMachine.OpenSubKey( registryRoot );
  34. //
  35. // At this point, let any exceptions just propogate since we can't get the information that we need.
  36. //
  37. manager = ResourceManager.CreateFileBasedResourceManager( "uddi",
  38. uddiRegKey.GetValue( "InstallRoot" ) + "resources",
  39. null );
  40. }
  41. }
  42. public static CultureInfo GetCulture()
  43. {
  44. Thread thread = Thread.CurrentThread;
  45. CultureInfo culture = thread.CurrentUICulture;
  46. try
  47. {
  48. //
  49. // If this is an HTTP request, attempt to use the browser's
  50. // preferred language setting.
  51. //
  52. HttpContext context = HttpContext.Current;
  53. if( null != context && context.Request.UserLanguages.Length >= 1 )
  54. {
  55. string language = context.Request.UserLanguages[ 0 ];
  56. //
  57. // BUG: 778542. To fully support MUI Builds via the UI, we must
  58. // first attempt to create a specific culture, and if that failes, create a
  59. // primary culture.
  60. //
  61. try
  62. {
  63. culture = CultureInfo.CreateSpecificCulture( language );
  64. }
  65. catch
  66. {
  67. culture = new CultureInfo( language );
  68. }
  69. }
  70. }
  71. catch
  72. {
  73. culture = thread.CurrentUICulture;
  74. }
  75. return culture;
  76. }
  77. //
  78. // Return the given string using the local machine culture rather than the
  79. // caller's culture. TODO consider creating a new method, GetString( string name, bool useLocalMachine )
  80. //
  81. public static string GetStringLocalMachineCulture( string name )
  82. {
  83. string resource = null;
  84. try
  85. {
  86. resource = manager.GetString( name );
  87. }
  88. catch
  89. {}
  90. if( null == resource )
  91. resource = "[[" + name + "]]";
  92. return resource;
  93. }
  94. public static CultureInfo GetCultureWithFallback( )
  95. {
  96. CultureInfo localculture = GetCulture();
  97. string filebase = Config.GetString( "InstallRoot" ) + "resources\\uddi{0}resources";
  98. string file = string.Format( filebase, "." + localculture.Name + "." ) ;
  99. bool installed = System.IO.File.Exists( file );
  100. if( installed )
  101. return localculture;
  102. else
  103. {
  104. do
  105. {
  106. localculture = localculture.Parent;
  107. file = string.Format( filebase, "." + localculture.Name + "." ) ;
  108. installed = System.IO.File.Exists( file );
  109. }while( !installed && 127!=localculture.LCID );
  110. if( installed )
  111. return localculture;
  112. else
  113. return CultureInfo.InstalledUICulture;
  114. }
  115. }
  116. public static string GetString( string name )
  117. {
  118. CultureInfo culture = GetCulture();
  119. //
  120. // Retrieve the resource.
  121. //
  122. string resource = null;
  123. try
  124. {
  125. resource = manager.GetString( name, culture );
  126. }
  127. catch
  128. {
  129. }
  130. if( null == resource )
  131. resource = "[[" + name + "]]";
  132. return resource;
  133. }
  134. /// *****************************************************************************
  135. /// <summary>
  136. /// Check to see if a string is marked up with '[[' at the start and ']]' at
  137. /// the end. If so, return true, else return false.
  138. /// </summary>
  139. /// <param name="key">Key to validate</param>
  140. /// <returns>Boolean indicated if it is a valide key</returns>
  141. /// *****************************************************************************
  142. public static bool IsKey( string key )
  143. {
  144. return( null!=key && key.StartsWith( "[[" ) && key.EndsWith( "]]" ) );
  145. }
  146. /// *****************************************************************************
  147. /// <summary>
  148. /// Removes the UDDI Localization Markup tags from a string. It will remove
  149. /// '[[' from the begining and ']]' from the end.
  150. ///
  151. /// If the key doesn't contain markup tags, the string is returned in its
  152. /// original state.
  153. /// </summary>
  154. /// <param name="key">Key to remove markup on</param>
  155. /// <returns>Bare Localization key.</returns>
  156. /// *****************************************************************************
  157. public static string StripMarkup( string key )
  158. {
  159. return key.TrimEnd( "]]".ToCharArray() ).TrimStart( "[[".ToCharArray() );
  160. }
  161. /// *****************************************************************************
  162. /// <summary>
  163. /// Translation of kernel32:SetThreadUILanguage semantics into managed code.
  164. /// Used for overridding console CurrentUICulture when console code page
  165. /// doesn't match the current ANSI or OEM code page.
  166. /// </summary>
  167. /// *****************************************************************************
  168. public static void SetConsoleUICulture()
  169. {
  170. int iConsoleCodePage = System.Console.Out.Encoding.CodePage;
  171. System.Threading.Thread currentThread = Thread.CurrentThread;
  172. //
  173. // if no code page set, we're not in a console
  174. //
  175. if( 0 != iConsoleCodePage )
  176. {
  177. if( !((iConsoleCodePage == currentThread.CurrentCulture.TextInfo.ANSICodePage ||
  178. iConsoleCodePage == currentThread.CurrentCulture.TextInfo.OEMCodePage) &&
  179. (iConsoleCodePage == currentThread.CurrentUICulture.TextInfo.ANSICodePage ||
  180. iConsoleCodePage == currentThread.CurrentUICulture.TextInfo.OEMCodePage)) )
  181. {
  182. //
  183. // override with en-US culture
  184. //
  185. currentThread.CurrentUICulture = new CultureInfo( "en-US" );
  186. }
  187. }
  188. }
  189. }
  190. }