home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / core / i18n.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-09-26  |  3.0 KB  |  119 lines

  1. /***************************************************************************
  2.  * i18n.cpp  -  internationalization with gettext
  3.  *
  4.  * Copyright (C) 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #include "../core/i18n.h"
  17.  
  18. void I18N_Init( void )
  19. {
  20.     const char *sys_locale = setlocale( LC_ALL, "" );
  21.  
  22.     if( sys_locale == NULL )
  23.     {
  24.         debug_print( "Failed to set translation locale\n", );
  25.     }
  26.     else
  27.     {
  28.         debug_print( "Translation locale is %s\n", sys_locale);
  29.     }
  30.  
  31.     const char *textdomain_directory = bindtextdomain( CAPTION, DATA_DIR "/" GAME_TRANSLATION_DIR );
  32.  
  33.     if( !textdomain_directory )
  34.     {
  35.         printf( "Warning: bindtextdomain failed for %s\n", DATA_DIR "/" GAME_TRANSLATION_DIR );
  36.     }
  37.  
  38.     const char *textdomain_codeset = bind_textdomain_codeset( CAPTION, "UTF-8" );
  39.     const char *textdomain_default = textdomain( CAPTION );
  40.  
  41.     debug_print( "Translation support with gettext set to:\n\tDirectory %s\n\tCodeset: %s\n\tText domain: %s\n",
  42.         textdomain_directory, textdomain_codeset, textdomain_default );
  43. }
  44.  
  45. #ifdef _WIN32
  46. int Get_Windows_Primary_Language( const std::string &language )
  47. {
  48.     if( language.compare( "en" ) == 0 )
  49.     {
  50.         return LANG_ENGLISH;
  51.     }
  52.     else if( language.compare( "de" ) == 0 )
  53.     {
  54.         return LANG_GERMAN;
  55.     }
  56.     else if( language.compare( "es" ) == 0 )
  57.     {
  58.         return LANG_SPANISH;
  59.     }
  60.     else if( language.compare( "fi" ) == 0 )
  61.     {
  62.         return LANG_FINNISH;
  63.     }
  64.     else if( language.compare( "fr" ) == 0 )
  65.     {
  66.         return LANG_FRENCH;
  67.     }
  68.     else if( language.compare( "it" ) == 0 )
  69.     {
  70.         return LANG_ITALIAN;
  71.     }
  72.     else if( language.compare( "nb" ) == 0 )
  73.     {
  74.         return LANG_NORWEGIAN;
  75.     }
  76.     else if( language.compare( "nn" ) == 0 )
  77.     {
  78.         return LANG_NORWEGIAN;
  79.     }
  80.     else if( language.compare( "sv" ) == 0 )
  81.     {
  82.         return LANG_SWEDISH;
  83.     }
  84.  
  85.     return LANG_NEUTRAL;
  86. }
  87.  
  88. int Get_Windows_Sub_Language( const std::string &language )
  89. {
  90.     if( language.compare( "nn" ) == 0 )
  91.     {
  92.         return SUBLANG_NORWEGIAN_NYNORSK;
  93.     }
  94.  
  95.     return SUBLANG_NEUTRAL;
  96. }
  97. #endif
  98.  
  99. void I18N_Set_Language( const std::string &language )
  100. {
  101. #ifdef _WIN32
  102.     int primary_language = Get_Windows_Primary_Language( language );
  103.     int sub_language = Get_Windows_Sub_Language( language );
  104.  
  105.     bool success = SUCCEEDED( SetThreadLocale( MAKELCID( MAKELANGID( primary_language, sub_language ), SORT_DEFAULT ) ) );
  106.  
  107.     if( !success )
  108.     {
  109.         debug_print("Failed to set translation locale to %d %d\n", primary_language, sub_language);
  110.     }
  111.     else
  112.     {
  113.         debug_print("Translation locale set to %d %d\n", primary_language, sub_language);
  114.     }
  115. #else
  116.     setenv( "LANGUAGE", language.c_str(), 1 );
  117. #endif
  118. }
  119.