Counter Strike : Global Offensive Source Code
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.

262 lines
9.3 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: provide a layer of abstraction between GC and vgui localization systems
  4. //
  5. //=============================================================================
  6. #ifndef LOCALIZATION_PROVIDER_H
  7. #define LOCALIZATION_PROVIDER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "language.h"
  12. typedef wchar_t locchar_t;
  13. //#define loc_snprintf V_snwprintf
  14. #define loc_sprintf_safe V_swprintf_safe
  15. //#define loc_sncat V_wcsncat
  16. #define loc_scat_safe V_wcscat_safe
  17. #define loc_sncpy Q_wcsncpy
  18. #define loc_scpy_safe V_wcscpy_safe
  19. #define loc_strlen Q_wcslen
  20. #define loc_strchr wcschr
  21. #define LOCCHAR(x) L ## x
  22. // interface matches a subset of VGUI functions
  23. class CLocalizationProvider
  24. {
  25. public:
  26. virtual locchar_t *Find( const char *pchKey ) = 0;
  27. virtual void ConstructString( OUT_Z_BYTECAP(unicodeBufferSizeInBytes) locchar_t *unicodeOutput, int unicodeBufferSizeInBytes, const locchar_t *formatString, int numFormatParameters, ... ) = 0;
  28. virtual void ConstructString( OUT_Z_BYTECAP(unicodeBufferSizeInBytes) locchar_t *unicodeOutput, int unicodeBufferSizeInBytes, const locchar_t *formatString, KeyValues *localizationVariables) = 0;
  29. virtual void ConvertLoccharToANSI ( const locchar_t *loc_In, CUtlConstString *out_ansi ) const = 0;
  30. virtual void ConvertLoccharToUnicode( const locchar_t *loc_In, CUtlConstWideString *out_unicode ) const = 0;
  31. virtual void ConvertUTF8ToLocchar ( const char *utf8_In, CUtlConstStringBase<locchar_t> *out_loc ) const = 0;
  32. virtual int ConvertLoccharToANSI( const locchar_t *loc, OUT_Z_CAP(ansiBufferSize) char *ansi, int ansiBufferSize ) = 0;
  33. virtual int ConvertLoccharToUnicode( const locchar_t *loc, OUT_Z_BYTECAP(unicodeBufferSizeInBytes) wchar_t *unicode, int unicodeBufferSizeInBytes ) = 0;
  34. virtual void ConvertUTF8ToLocchar( const char *utf8, OUT_Z_BYTECAP(cubDestSizeInBytes) locchar_t *locchar, int cubDestSizeInBytes ) = 0;
  35. virtual ELanguage GetELang() = 0;
  36. };
  37. CLocalizationProvider *GLocalizationProvider();
  38. #include "vgui/ILocalize.h"
  39. extern vgui::ILocalize *g_pVGuiLocalize;
  40. // Game localization is handled by vgui
  41. class CVGUILocalizationProvider : public CLocalizationProvider
  42. {
  43. public:
  44. CVGUILocalizationProvider();
  45. virtual locchar_t *Find( const char *pchKey );
  46. virtual void ConstructString( OUT_Z_BYTECAP(unicodeBufferSizeInBytes) locchar_t *unicodeOutput, int unicodeBufferSizeInBytes, const locchar_t *formatString, int numFormatParameters, ... );
  47. virtual void ConstructString( OUT_Z_BYTECAP(unicodeBufferSizeInBytes) locchar_t *unicodeOutput, int unicodeBufferSizeInBytes, const locchar_t *formatString, KeyValues *localizationVariables );
  48. virtual void ConvertLoccharToANSI ( const locchar_t *loc_In, CUtlConstString *out_ansi ) const;
  49. virtual void ConvertLoccharToUnicode( const locchar_t *loc_In, CUtlConstWideString *out_unicode ) const;
  50. virtual void ConvertUTF8ToLocchar ( const char *utf8_In, CUtlConstStringBase<locchar_t> *out_loc ) const;
  51. virtual int ConvertLoccharToANSI( const locchar_t *loc, OUT_Z_CAP(ansiBufferSize) char *ansi, int ansiBufferSize );
  52. virtual int ConvertLoccharToUnicode( const locchar_t *loc, OUT_Z_BYTECAP(unicodeBufferSizeInBytes) wchar_t *unicode, int unicodeBufferSizeInBytes );
  53. virtual void ConvertUTF8ToLocchar( const char *utf8, OUT_Z_BYTECAP(cubDestSizeInBytes) locchar_t *locchar, int cubDestSizeInBytes );
  54. virtual ELanguage GetELang() { return k_Lang_None; }
  55. };
  56. // --------------------------------------------------------------------------
  57. // Purpose: CLocalizedStringArg<> is a class that will take a variable of any
  58. // arbitary type and convert it to a string of whatever character type
  59. // we're using for localization (locchar_t).
  60. //
  61. // Independently it isn't very useful, though it can be used to sort-of-
  62. // intelligently fill out the correct format string. It's designed to be
  63. // used for the arguments of CConstructLocalizedString, which can be of
  64. // arbitrary number and type.
  65. //
  66. // If you pass in a (non-specialized) pointer, the code will assume that
  67. // you meant that pointer to be used as a localized string. This will
  68. // still fail to compile if some non-string type is passed in, but will
  69. // handle weird combinations of const/volatile/whatever automatically.
  70. // --------------------------------------------------------------------------
  71. // The base implementation doesn't do anything except fail to compile if you
  72. // use it. Getting an "incomplete type" error here means that you tried to construct
  73. // a localized string with a type that doesn't have a specialization.
  74. template < typename T >
  75. class CLocalizedStringArg;
  76. // --------------------------------------------------------------------------
  77. template < typename T >
  78. class CLocalizedStringArgStringImpl
  79. {
  80. public:
  81. enum { kIsValid = true };
  82. CLocalizedStringArgStringImpl( const locchar_t *pStr ) : m_pStr( pStr ) { }
  83. const locchar_t *GetLocArg() const { return m_pStr; }
  84. private:
  85. const locchar_t *m_pStr;
  86. };
  87. // --------------------------------------------------------------------------
  88. template < typename T >
  89. class CLocalizedStringArg<T *> : public CLocalizedStringArgStringImpl<T>
  90. {
  91. public:
  92. CLocalizedStringArg( const locchar_t *pStr ) : CLocalizedStringArgStringImpl<T>( pStr ) { }
  93. };
  94. // --------------------------------------------------------------------------
  95. template < typename T >
  96. class CLocalizedStringArgPrintfImpl
  97. {
  98. public:
  99. enum { kIsValid = true };
  100. CLocalizedStringArgPrintfImpl( T value, const locchar_t *loc_Format ) { loc_sprintf_safe( m_cBuffer, loc_Format, value ); }
  101. const locchar_t *GetLocArg() const { return m_cBuffer; }
  102. private:
  103. enum { kBufferSize = 128, };
  104. locchar_t m_cBuffer[ kBufferSize ];
  105. };
  106. // --------------------------------------------------------------------------
  107. template < >
  108. class CLocalizedStringArg<uint32> : public CLocalizedStringArgPrintfImpl<uint32>
  109. {
  110. public:
  111. CLocalizedStringArg( uint32 unValue ) : CLocalizedStringArgPrintfImpl<uint32>( unValue, LOCCHAR("%u") ) { }
  112. };
  113. // --------------------------------------------------------------------------
  114. template < >
  115. class CLocalizedStringArg<float> : public CLocalizedStringArgPrintfImpl<float>
  116. {
  117. public:
  118. // Display one decimal point if we've got a value less than one, and no point
  119. // if we're greater.
  120. CLocalizedStringArg( float fValue )
  121. : CLocalizedStringArgPrintfImpl<float>( fValue,
  122. fabsf( fValue ) < 1.0f ? LOCCHAR("%.1f") : LOCCHAR("%.0f") )
  123. {
  124. //
  125. }
  126. };
  127. // --------------------------------------------------------------------------
  128. // Purpose:
  129. // --------------------------------------------------------------------------
  130. class CConstructLocalizedString
  131. {
  132. public:
  133. template < typename T >
  134. CConstructLocalizedString( CLocalizationProvider* pLocalizationProvider, const locchar_t *loc_Format, T arg0 )
  135. {
  136. Assert( CLocalizedStringArg<T>::kIsValid );
  137. m_loc_Buffer[0] = '\0';
  138. if ( loc_Format )
  139. {
  140. pLocalizationProvider->ConstructString( m_loc_Buffer, sizeof( m_loc_Buffer ), loc_Format, 1, CLocalizedStringArg<T>( arg0 ).GetLocArg() );
  141. }
  142. }
  143. template < typename T, typename U >
  144. CConstructLocalizedString( CLocalizationProvider* pLocalizationProvider, const locchar_t *loc_Format, T arg0, U arg1 )
  145. {
  146. Assert( CLocalizedStringArg<T>::kIsValid );
  147. Assert( CLocalizedStringArg<U>::kIsValid );
  148. m_loc_Buffer[0] = '\0';
  149. if ( loc_Format )
  150. {
  151. pLocalizationProvider->ConstructString( m_loc_Buffer, sizeof( m_loc_Buffer ), loc_Format, 2, CLocalizedStringArg<T>( arg0 ).GetLocArg(), CLocalizedStringArg<U>( arg1 ).GetLocArg() );
  152. }
  153. }
  154. template < typename T, typename U, typename V >
  155. CConstructLocalizedString( CLocalizationProvider* pLocalizationProvider, const locchar_t *loc_Format, T arg0, U arg1, V arg2 )
  156. {
  157. Assert( CLocalizedStringArg<T>::kIsValid );
  158. Assert( CLocalizedStringArg<U>::kIsValid );
  159. Assert( CLocalizedStringArg<V>::kIsValid );
  160. m_loc_Buffer[0] = '\0';
  161. if ( loc_Format )
  162. {
  163. pLocalizationProvider->ConstructString( m_loc_Buffer, sizeof( m_loc_Buffer ), loc_Format, 3,
  164. CLocalizedStringArg<T>( arg0 ).GetLocArg(),
  165. CLocalizedStringArg<U>( arg1 ).GetLocArg(),
  166. CLocalizedStringArg<V>( arg2 ).GetLocArg() );
  167. }
  168. }
  169. template < typename T, typename U, typename V, typename W >
  170. CConstructLocalizedString( CLocalizationProvider* pLocalizationProvider, const locchar_t *loc_Format, T arg0, U arg1, V arg2, W arg3 )
  171. {
  172. Assert( CLocalizedStringArg<T>::kIsValid );
  173. Assert( CLocalizedStringArg<U>::kIsValid );
  174. Assert( CLocalizedStringArg<V>::kIsValid );
  175. Assert( CLocalizedStringArg<W>::kIsValid );
  176. m_loc_Buffer[0] = '\0';
  177. if ( loc_Format )
  178. {
  179. pLocalizationProvider->ConstructString( m_loc_Buffer,
  180. sizeof( m_loc_Buffer ),
  181. loc_Format,
  182. 4,
  183. CLocalizedStringArg<T>( arg0 ).GetLocArg(),
  184. CLocalizedStringArg<U>( arg1 ).GetLocArg(),
  185. CLocalizedStringArg<V>( arg2 ).GetLocArg(),
  186. CLocalizedStringArg<W>( arg3 ).GetLocArg() );
  187. }
  188. }
  189. CConstructLocalizedString( CLocalizationProvider* pLocalizationProvider, const locchar_t *loc_Format, KeyValues *pKeyValues )
  190. {
  191. m_loc_Buffer[0] = '\0';
  192. if ( loc_Format && pKeyValues )
  193. {
  194. pLocalizationProvider->ConstructString( m_loc_Buffer, sizeof( m_loc_Buffer ), loc_Format, pKeyValues );
  195. }
  196. }
  197. operator const locchar_t *() const
  198. {
  199. return Get();
  200. }
  201. const locchar_t *Get() const
  202. {
  203. return m_loc_Buffer;
  204. }
  205. private:
  206. enum { kBufferSize = 512, };
  207. locchar_t m_loc_Buffer[ kBufferSize ];
  208. };
  209. #endif // LOCALIZATION_PROVIDER_H