diff --git a/boost/locale.hpp b/boost/locale.hpp index 989bba6..d9d9d29 100644 --- a/boost/locale.hpp +++ b/boost/locale.hpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include diff --git a/boost/locale/gnu_gettext_info.hpp b/boost/locale/gnu_gettext_info.hpp new file mode 100644 index 0000000..b1220ed --- /dev/null +++ b/boost/locale/gnu_gettext_info.hpp @@ -0,0 +1,78 @@ +// +// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef BOOST_LOCALE_GNU_GETTEXT_INFO_HPP_INCLUDED +#define BOOST_LOCALE_GNU_GETTEXT_INFO_HPP_INCLUDED +#include +#include +#include + + +namespace boost { +namespace locale { +namespace gnu_gettext { + /// + /// \brief This class holds functions that provide access to GNU Gettext settings + /// + class info { + public: + virtual ~info() + { + } + + /// + /// Gets the language the catalog is loaded for + /// + virtual std::string language() = 0; + + /// + /// Get the domain the catalog is loaded for + /// + virtual std::string domain() = 0; + + /// + /// Get the the search path the catalog was found in + /// + virtual std::string search_path() = 0; + + /// + /// Get the path format the catalog used + /// + virtual std::string path_format() = 0; + + /// + /// Get the path format to the catalog used + /// + virtual std::string full_path() = 0; + }; + + template + info* get_info(std::locale const& loc); + + template <> + BOOST_LOCALE_DECL info* get_info(std::locale const& loc); + + #ifdef BOOST_HAS_CHAR16_T + + template <> + BOOST_LOCALE_DECL info* get_info(std::locale const& loc); + + #endif + + #ifdef BOOST_HAS_CHAR32_T + + template <> + BOOST_LOCALE_DECL info* get_info(std::locale const& loc); + + #endif +} +} +} + +#endif + +// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/libs/locale/src/shared/message.cpp b/libs/locale/src/shared/message.cpp index ce4b97c..e1d1ee5 100644 --- a/libs/locale/src/shared/message.cpp +++ b/libs/locale/src/shared/message.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -481,6 +482,15 @@ namespace boost { return buffer.c_str(); } + struct catalog_settings + { + std::string domain; + std::string path; + std::string search_path; + std::string format; + std::string full_path; + }; + template class mo_message : public message_format { @@ -581,6 +591,15 @@ namespace boost { for(unsigned k=0;!found && k @@ -778,6 +804,95 @@ namespace boost { return new mo_message(data); } #endif + + template + class mo_info : public info { + typedef mo_message message_type; + + public: + mo_info(message_type const* Msg) + : msg(Msg) + { + settings = msg->get_catalog_settings(); + } + + virtual std::string language() + { + return settings.path; + } + + virtual std::string domain() + { + return settings.domain; + } + + virtual std::string search_path() + { + return settings.search_path; + } + + virtual std::string path_format() + { + return settings.format; + } + + virtual std::string full_path() + { + return settings.full_path; + } + + private: + message_type const* msg; + catalog_settings settings; + }; + + template + info* _get_info(std::locale const& loc) + { + typedef mo_message facet_type; + + if(!std::has_facet(loc)) + { + return 0; + } + + facet_type const* facet = &(std::use_facet(loc)); + + return new mo_info(facet); + } + + template <> + info* get_info(std::locale const& loc) + { + return _get_info(loc); + } + + template <> + info* get_info(std::locale const& loc) + { + return _get_info(loc); + } + + #ifdef BOOST_HAS_CHAR16_T + + template <> + info* get_info(std::locale const& loc) + { + return _get_info(loc); + } + + #endif + + #ifdef BOOST_HAS_CHAR32_T + + template <> + info* get_info(std::locale const& loc) + { + return _get_info(loc); + } + + #endif + } /// gnu_gettext } // locale diff --git a/libs/locale/test/test_message.cpp b/libs/locale/test/test_message.cpp index 4b7f750..5a19bc8 100644 --- a/libs/locale/test/test_message.cpp +++ b/libs/locale/test/test_message.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "test_locale.hpp" #include "test_locale_tools.hpp" @@ -523,6 +524,30 @@ int main(int argc,char **argv) bl::dnpgettext("",L"",L"",L"",1); } + std::cout << "Testing Gettext Info" << std::endl; + { + std::string format = "{1}/{2}/{3}/{4}.mo"; + std::string search_path; + if(argc==2) + search_path = argv[1]; + else + search_path = "./"; + + boost::locale::generator g; + g.add_messages_domain("full"); + g.add_messages_path(search_path); + + std::locale l = g("he_IL.UTF-8"); + + bl::gnu_gettext::info* info = bl::gnu_gettext::get_info(l); + + TEST(info->language() == "he_IL"); + TEST(info->domain() == "full"); + TEST(info->path_format() == format); + TEST(info->full_path() == search_path + "/he_IL/LC_MESSAGES/full.mo"); + + delete info; + } } catch(std::exception const &e) { std::cerr << "Failed " << e.what() << std::endl;