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.

215 lines
8.1 KiB

  1. //====== Copyright © 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: languages definition
  4. //
  5. //=============================================================================
  6. #include "language.h"
  7. #include "tier0/dbg.h"
  8. #include "tier1/strtools.h"
  9. // NOTE: This has to be the last file included!
  10. #include "tier0/memdbgon.h"
  11. struct Language_t
  12. {
  13. const char *m_pchName;
  14. const char *m_pchShortName;
  15. const char *m_pchVGUILocalizationName;
  16. const char *m_pchICUName; // used by osx; ISO-639-1 + ISO-3166-1 alpha-2. http://userguide.icu-project.org/locale/examples
  17. ELanguage m_ELanguage;
  18. int m_LanguageCodeID;
  19. };
  20. // REVIEW
  21. // es_ES - use new world spanish country code instead?
  22. // zh_CN - validate that SC date formats come through
  23. // bt_BR - assume we should use Brazilian rather than Iberian portguese
  24. static const Language_t s_LanguageNames[] =
  25. {
  26. { "None", "none", "None", "none", k_Lang_None, 0 },
  27. { "English", "english", "#GameUI_Language_English", "en_US", k_Lang_English, 1033 },
  28. { "German", "german", "#GameUI_Language_German", "de_DE", k_Lang_German, 1031 } ,
  29. { "French", "french", "#GameUI_Language_French", "fr_FR", k_Lang_French, 1036 } ,
  30. { "Italian", "italian", "#GameUI_Language_Italian", "it_IT", k_Lang_Italian, 1040 } ,
  31. { "Korean", "koreana", "#GameUI_Language_Korean", "ko_KR", k_Lang_Korean, 1042 } ,
  32. { "Spanish", "spanish", "#GameUI_Language_Spanish", "es_ES", k_Lang_Spanish, 1034 },
  33. { "Simplified_Chinese", "schinese", "#GameUI_Language_Simplified_Chinese", "zh_CN", k_Lang_Simplified_Chinese, 2052 },
  34. { "Traditional_Chinese", "tchinese", "#GameUI_Language_Traditional_Chinese", "zh_TW", k_Lang_Traditional_Chinese, 1028 },
  35. { "Russian", "russian", "#GameUI_Language_Russian", "ru_RU", k_Lang_Russian, 1049 } ,
  36. { "Thai", "thai", "#GameUI_Language_Thai", "th_TH", k_Lang_Thai, 1054 } ,
  37. { "Japanese", "japanese", "#GameUI_Language_Japanese", "ja_JP", k_Lang_Japanese, 1041 } ,
  38. { "Portuguese", "portuguese", "#GameUI_Language_Portuguese", "pt_PT", k_Lang_Portuguese, 2070 } ,
  39. { "Polish", "polish", "#GameUI_Language_Polish", "pl_PL", k_Lang_Polish, 1045 } ,
  40. { "Danish", "danish", "#GameUI_Language_Danish", "da_DK", k_Lang_Danish, 1030 } ,
  41. { "Dutch", "dutch", "#GameUI_Language_Dutch", "nl_NL", k_Lang_Dutch, 1043 } ,
  42. { "Finnish", "finnish", "#GameUI_Language_Finnish", "fi_FI", k_Lang_Finnish, 1035 } ,
  43. { "Norwegian", "norwegian", "#GameUI_Language_Norwegian", "no_NO", k_Lang_Norwegian, 1044 } ,
  44. { "Swedish", "swedish", "#GameUI_Language_Swedish", "sv_SE", k_Lang_Swedish, 1053 } ,
  45. { "Romanian", "romanian", "#GameUI_Language_Romanian", "ro_RO", k_Lang_Romanian, 1048 } ,
  46. { "Turkish", "turkish", "#GameUI_Language_Turkish", "tr_TR", k_Lang_Turkish, 1055 } ,
  47. { "Hungarian", "hungarian", "#GameUI_Language_Hungarian", "hu_HU", k_Lang_Hungarian, 1038 } ,
  48. { "Czech", "czech", "#GameUI_Language_Czech", "cs_CZ", k_Lang_Czech, 1029 } ,
  49. { "Brazilian", "brazilian", "#GameUI_Language_Brazilian", "pt_BR", k_Lang_Brazilian, 1046 } ,
  50. { "Bulgarian", "bulgarian", "#GameUI_Language_Bulgarian", "bg_BG", k_Lang_Bulgarian, 1026 } ,
  51. };
  52. //-----------------------------------------------------------------------------
  53. // Purpose: translate language enum into closests windows language code ID
  54. //-----------------------------------------------------------------------------
  55. int GetLanguageCodeID(ELanguage eLang)
  56. {
  57. for ( uint iLang = 0; iLang < Q_ARRAYSIZE(s_LanguageNames); ++iLang )
  58. {
  59. if ( s_LanguageNames[iLang].m_ELanguage == eLang )
  60. return s_LanguageNames[iLang].m_LanguageCodeID;
  61. }
  62. // default to English
  63. return 1033;
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose: find the language by name
  67. //-----------------------------------------------------------------------------
  68. ELanguage PchLanguageToELanguage( const char *pchShortName, ELanguage eDefault )
  69. {
  70. COMPILE_TIME_ASSERT( Q_ARRAYSIZE(s_LanguageNames) == k_Lang_MAX + 1 );
  71. if ( !pchShortName )
  72. return eDefault;
  73. for ( uint iLang = 0; iLang < Q_ARRAYSIZE(s_LanguageNames); ++iLang )
  74. {
  75. if ( !Q_stricmp( pchShortName, s_LanguageNames[iLang].m_pchShortName ) )
  76. {
  77. return s_LanguageNames[iLang].m_ELanguage;
  78. }
  79. }
  80. // return default
  81. return eDefault;
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose: find the language by ICU short code
  85. //-----------------------------------------------------------------------------
  86. ELanguage PchLanguageICUCodeToELanguage( const char *pchICUCode, ELanguage eDefault )
  87. {
  88. COMPILE_TIME_ASSERT( Q_ARRAYSIZE(s_LanguageNames) == k_Lang_MAX + 1 );
  89. if ( !pchICUCode )
  90. return eDefault;
  91. // Match to no more than the param length so either a short 'en' or
  92. // full 'zh-Hant' can match
  93. int nLen = Q_strlen( pchICUCode );
  94. // we only have 5 character ICU codes so this should be enough room
  95. char rchCleanedCode[ 6 ];
  96. Q_strncpy( rchCleanedCode, pchICUCode, Q_ARRAYSIZE( rchCleanedCode ) );
  97. if( nLen >= 3 && rchCleanedCode[ 2 ] == '-' )
  98. {
  99. rchCleanedCode[ 2 ] = '_';
  100. }
  101. for ( uint iLang = 0; iLang < Q_ARRAYSIZE(s_LanguageNames); ++iLang )
  102. {
  103. if ( !Q_strnicmp( rchCleanedCode, s_LanguageNames[iLang].m_pchICUName, nLen ) )
  104. {
  105. return s_LanguageNames[iLang].m_ELanguage;
  106. }
  107. }
  108. // return default
  109. return eDefault;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Purpose: return the short string name used for this language by SteamUI
  113. //-----------------------------------------------------------------------------
  114. const char *GetLanguageShortName( ELanguage eLang )
  115. {
  116. COMPILE_TIME_ASSERT( Q_ARRAYSIZE(s_LanguageNames) == k_Lang_MAX + 1 );
  117. if ( s_LanguageNames[ eLang + 1 ].m_ELanguage == eLang )
  118. {
  119. return s_LanguageNames[ eLang + 1 ].m_pchShortName;
  120. }
  121. Assert( !"enum ELanguage order mismatched from Language_t s_LanguageNames, fix it!" );
  122. return s_LanguageNames[0].m_pchShortName;
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose: return the ICU code used for this language by SteamUI
  126. //-----------------------------------------------------------------------------
  127. const char *GetLanguageICUName( ELanguage eLang )
  128. {
  129. COMPILE_TIME_ASSERT( Q_ARRAYSIZE(s_LanguageNames) == k_Lang_MAX + 1 );
  130. if ( s_LanguageNames[ eLang + 1 ].m_ELanguage == eLang )
  131. {
  132. return s_LanguageNames[ eLang + 1 ].m_pchICUName;
  133. }
  134. Assert( !"enum ELanguage order mismatched from Language_t s_LanguageNames, fix it!" );
  135. return s_LanguageNames[0].m_pchICUName;
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose: return the CLocale name that works with setlocale()
  139. //-----------------------------------------------------------------------------
  140. const char *GetLangugeCLocaleName( ELanguage eLang )
  141. {
  142. if ( eLang == k_Lang_None )
  143. return "";
  144. #ifdef _WIN32
  145. // table for Win32 is here: http://msdn.microsoft.com/en-us/library/hzz3tw78(v=VS.80).aspx
  146. // shortname works except for chinese
  147. switch ( eLang )
  148. {
  149. case k_Lang_Simplified_Chinese:
  150. return "chs"; // or "chinese-simplified"
  151. case k_Lang_Traditional_Chinese:
  152. return "cht"; // or "chinese-traditional"
  153. case k_Lang_Korean:
  154. return "korean"; // steam likes "koreana" for the name for some reason.
  155. default:
  156. return GetLanguageShortName( eLang );
  157. }
  158. #else
  159. switch ( eLang )
  160. {
  161. case k_Lang_Simplified_Chinese:
  162. case k_Lang_Traditional_Chinese:
  163. return "zh_CN";
  164. default:
  165. ;
  166. }
  167. // ICU codes work on linux/osx
  168. return GetLanguageICUName( eLang );
  169. #endif
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Purpose: return the short string name used for this language by SteamUI
  173. //-----------------------------------------------------------------------------
  174. const char *GetLanguageVGUILocalization( ELanguage eLang )
  175. {
  176. COMPILE_TIME_ASSERT( Q_ARRAYSIZE(s_LanguageNames) == k_Lang_MAX + 1 );
  177. if ( s_LanguageNames[ eLang + 1 ].m_ELanguage == eLang )
  178. {
  179. return s_LanguageNames[ eLang + 1 ].m_pchVGUILocalizationName;
  180. }
  181. Assert( !"enum ELanguage order mismatched from Language_t s_LanguageNames, fix it!" );
  182. return s_LanguageNames[0].m_pchVGUILocalizationName;
  183. }