1 /* 2 * Copyright © 2010 Jens Oknelid, paskharen@gmail.com 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * In addition, as a special exception, compiling, linking, and/or 19 * using OpenSSL with this program is allowed. 20 */ 21 22 #ifndef LINUXDCPP_INTL_UTIL_HH 23 #define LINUXDCPP_INTL_UTIL_HH 24 25 #include <boost/format.hpp> 26 #include <string> 27 #include <exception> 28 #include <glib/gi18n.h> 29 #include <errno.h> 30 31 class IntlUtil 32 { 33 public: 34 // Initialize i18n support initialize()35 static void initialize() 36 { 37 if (bindtextdomain(PACKAGE, _DATADIR "/locale") == NULL) 38 throw std::runtime_error(strerror(errno)); 39 40 if (textdomain(PACKAGE) == NULL) 41 throw std::runtime_error(strerror(errno)); 42 43 if (bind_textdomain_codeset(PACKAGE, "UTF-8") == NULL) 44 throw std::runtime_error(strerror(errno)); 45 } 46 message_format(const char * text)47 static inline boost::format message_format(const char *text) 48 { 49 boost::format fmt(text); 50 fmt.exceptions(boost::io::no_error_bits); 51 return fmt; 52 } 53 message_format(const std::string & text)54 static inline boost::format message_format(const std::string &text) 55 { 56 return message_format(text.c_str()); 57 } 58 }; 59 60 #define F_(text, params) (IntlUtil::message_format(_(text)) params).str() 61 #define P_(text, text_plural, params, n) (IntlUtil::message_format(g_dngettext(NULL, text, text_plural, n)) params).str() 62 63 #endif /* LINUXDCPP_INTL_UTIL_HH */ 64 65