[8] | 1 | /*
|
---|
| 2 | * TOPPERS Software
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2007-2008 by TAKAGI Nobuhisa
|
---|
| 6 | *
|
---|
| 7 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
| 8 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
| 9 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
| 10 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
| 11 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
| 12 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
| 13 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
| 14 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
| 15 | gip
|
---|
| 16 | * Ò}j
|
---|
| 17 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
| 18 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
| 19 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
| 20 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
| 21 | * ÆD
|
---|
| 22 | * (a) Äzzɺ¤hL
|
---|
| 23 | gipÒ}j
|
---|
| 24 | AÈÇjÉCãLÌ
|
---|
| 25 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
| 26 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
| 27 | * ñ·é±ÆD
|
---|
| 28 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
| 29 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
| 30 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
| 31 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
| 32 | * ÆÓ·é±ÆD
|
---|
| 33 | *
|
---|
| 34 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
| 35 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
| 36 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
| 37 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
| 38 | * ÌÓCðíÈ¢D
|
---|
| 39 | *
|
---|
| 40 | */
|
---|
| 41 | #include <cstdlib>
|
---|
| 42 | #include <stdlib.h> // CygwinÎô
|
---|
| 43 | #include <cstring>
|
---|
| 44 | #include <clocale>
|
---|
| 45 | #include <string>
|
---|
| 46 | #include <map>
|
---|
| 47 | #include <fstream>
|
---|
| 48 | #include <stdexcept>
|
---|
| 49 | #include "toppers/gettext.hpp"
|
---|
| 50 | #include "toppers/cpp.hpp"
|
---|
| 51 | #include "toppers/global.hpp"
|
---|
| 52 | #include <boost/scoped_array.hpp>
|
---|
| 53 | #include <boost/any.hpp>
|
---|
| 54 | #include <boost/filesystem/path.hpp>
|
---|
| 55 | #include <boost/filesystem/operations.hpp>
|
---|
| 56 |
|
---|
| 57 | namespace toppers
|
---|
| 58 | {
|
---|
| 59 | namespace
|
---|
| 60 | {
|
---|
| 61 |
|
---|
| 62 | std::map< std::string, std::string > msgcat;
|
---|
| 63 |
|
---|
| 64 | void register_msgcat( std::string const& msgid, std::string const& msgstr )
|
---|
| 65 | {
|
---|
| 66 | std::string::size_type size = msgstr.size();
|
---|
| 67 | boost::scoped_array< wchar_t > wbuf( new wchar_t[ size + 1 ] );
|
---|
| 68 | boost::scoped_array< char > buf( new char[ size + 1 ] );
|
---|
| 69 | wchar_t* wcs = wbuf.get();
|
---|
| 70 | wchar_t wc = 0;
|
---|
| 71 | for ( std::string::const_iterator iter( msgstr.begin() ), last( msgstr.end() ); iter != last; ++iter )
|
---|
| 72 | {
|
---|
| 73 | int c = static_cast< unsigned char >( *iter );
|
---|
| 74 | if ( ( ( c & 0xc0 ) == 0xc0 ) || ( c < 0x80 ) ) // æsoCg
|
---|
| 75 | {
|
---|
| 76 | if ( wc != 0 )
|
---|
| 77 | {
|
---|
| 78 | *wcs++ = wc;
|
---|
| 79 | wc = 0;
|
---|
| 80 | }
|
---|
| 81 | if ( ( c & 0x80 ) == 0 )
|
---|
| 82 | {
|
---|
| 83 | wc = static_cast< wchar_t >( c );
|
---|
| 84 | }
|
---|
| 85 | else if ( ( c & 0xe0 ) == 0xc0 )
|
---|
| 86 | {
|
---|
| 87 | wc = static_cast< wchar_t >( c & 0x1f );
|
---|
| 88 | }
|
---|
| 89 | else if ( ( c & 0xf0 ) == 0xe0 )
|
---|
| 90 | {
|
---|
| 91 | wc = static_cast< wchar_t >( c & 0xf );
|
---|
| 92 | }
|
---|
| 93 | else
|
---|
| 94 | {
|
---|
| 95 | // TQ[gÍ¢Îi±±Åε½ÆµÄàA¶R[hϷɵ¶éÂ\«åj
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | else // ã±oCg
|
---|
| 99 | {
|
---|
| 100 | wc = static_cast< wchar_t >( ( wc << 6 ) | ( c & 0x3f ) );
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | if ( wc != 0 )
|
---|
| 104 | {
|
---|
| 105 | *wcs++ = wc;
|
---|
| 106 | }
|
---|
| 107 | *wcs = L'\0';
|
---|
| 108 |
|
---|
| 109 | // « ±ÌÔ͵ÄáOª¶µÈ¢
|
---|
| 110 | char const* locale = std::setlocale( LC_CTYPE, "" );
|
---|
| 111 | /* std:: */wcstombs( buf.get(), wbuf.get(), size + 1 ); // Unicode ©ç«˶̶R[hÖÏ·
|
---|
| 112 | std::setlocale( LC_CTYPE, locale );
|
---|
| 113 | // ª ±ÌÔ͵ÄáOª¶µÈ¢
|
---|
| 114 |
|
---|
| 115 | msgcat[ msgid ] = std::string( buf.get() );
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | bool msgcat_loaded = false;
|
---|
| 119 |
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /*!
|
---|
| 123 | * \brief bZ[WJ^OÌ[h
|
---|
| 124 | * \param[in] dir *.po t@Cª¶Ý·éfBNg
|
---|
| 125 | * \retval true ¬÷
|
---|
| 126 | * \retval false ¸s
|
---|
| 127 | *
|
---|
| 128 | * ÀðÈÖ»·é½ßA.pot@CÌLqû@ÉÍȺ̧ñª éB
|
---|
| 129 | * - msgid, msgstr ÍK¸sÌæªÉLq·éB
|
---|
| 130 | * - msgid, msgstr ̼ãÉÍAK¸óÞ¶ê¶ÆµA»Ì¼ãɶñðLq·éB
|
---|
| 131 | * - ¶ñÌÝðLq·ésÍK¸ " ÅnßéB
|
---|
| 132 | * - .pot@CÍ̶R[hÍK¸ UTF-8N Æ·éB
|
---|
| 133 | */
|
---|
| 134 | bool load_msgcat( std::string const& dir )
|
---|
| 135 | {
|
---|
| 136 | namespace fs = boost::filesystem;
|
---|
| 137 | char const* env = std::getenv( "TOPPERS_CFG_LANG" );
|
---|
| 138 | // env = "ja";
|
---|
| 139 | if ( env == 0 )
|
---|
| 140 | {
|
---|
| 141 | return false;
|
---|
| 142 | }
|
---|
| 143 | std::string lang( env );
|
---|
| 144 |
|
---|
| 145 | fs::path cfg_dir( dir, fs::native );
|
---|
| 146 | fs::path po_file( cfg_dir/fs::path( lang + ".po", fs::native ) );
|
---|
| 147 | std::ifstream ifs( po_file.native_file_string().c_str() );
|
---|
| 148 | std::string msgid;
|
---|
| 149 | std::string msgstr;
|
---|
| 150 |
|
---|
| 151 | while ( ifs )
|
---|
| 152 | {
|
---|
| 153 | std::string str;
|
---|
| 154 | std::getline( ifs, str );
|
---|
| 155 |
|
---|
| 156 | // üs¶Ìá¢ðzû
|
---|
| 157 | std::string::size_type pos = str.find_last_not_of( " \t\r\n" ); // ¢ÅÉsÌóÞà
|
---|
| 158 | if ( pos != std::string::npos && pos < str.size() - 1 )
|
---|
| 159 | {
|
---|
| 160 | char c = str[pos];
|
---|
| 161 | str.erase( pos + 1, std::string::npos );
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | if ( str.empty() || str[ 0 ] == '#' || str == "" )
|
---|
| 165 | {
|
---|
| 166 | ; // ósܽÍRgs
|
---|
| 167 | }
|
---|
| 168 | else
|
---|
| 169 | {
|
---|
| 170 | try
|
---|
| 171 | {
|
---|
| 172 | if ( std::strncmp( str.c_str(), "msgid", sizeof( "msgid" )-1 ) == 0 )
|
---|
| 173 | {
|
---|
| 174 | str.erase( 0, sizeof( "msgid" )-1+1 );
|
---|
| 175 | msgid = expand_quote( str );
|
---|
| 176 | }
|
---|
| 177 | else if ( std::strncmp( str.c_str(), "msgstr", sizeof( "msgstr" )-1 ) == 0 )
|
---|
| 178 | {
|
---|
| 179 | str.erase( 0, sizeof( "msgstr" )-1+1 );
|
---|
| 180 | msgstr = expand_quote( str );
|
---|
| 181 | }
|
---|
| 182 | else
|
---|
| 183 | {
|
---|
| 184 | msgstr += expand_quote( str );
|
---|
| 185 | }
|
---|
| 186 | if ( !msgid.empty() && !msgstr.empty() ) // ¼OÌ msgid / msgstr ðo^
|
---|
| 187 | {
|
---|
| 188 | register_msgcat( msgid, msgstr );
|
---|
| 189 | msgid.clear();
|
---|
| 190 | msgstr.clear();
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | catch ( std::invalid_argument& )
|
---|
| 194 | {
|
---|
| 195 | return false;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | msgcat_loaded = true;
|
---|
| 200 | return true;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /*!
|
---|
| 204 | * \brief bZ[WÌ|ó
|
---|
| 205 | * \param[in] message bZ[WID
|
---|
| 206 | * \return |óãÌbZ[W
|
---|
| 207 | */
|
---|
| 208 | std::string const& gettext( std::string const& message )
|
---|
| 209 | {
|
---|
| 210 | static bool f = load_msgcat( get_global< std::string >( "cfg-directory" ) );
|
---|
| 211 | if ( !msgcat_loaded )
|
---|
| 212 | {
|
---|
| 213 | return message;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | std::map< std::string, std::string >::const_iterator iter( msgcat.find( message ) ), last( msgcat.end() );
|
---|
| 217 | if ( iter != last )
|
---|
| 218 | {
|
---|
| 219 | std::string const& result( iter->second );
|
---|
| 220 | return result;
|
---|
| 221 | }
|
---|
| 222 | return message;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | }
|
---|