Ticket #7727: gettext_info.patch
File gettext_info.patch, 9.3 KB (added by , 10 years ago) |
---|
-
boost/locale.hpp
diff --git a/boost/locale.hpp b/boost/locale.hpp index 989bba6..d9d9d29 100644
a b 18 18 #include <boost/locale/formatting.hpp> 19 19 #include <boost/locale/generator.hpp> 20 20 #include <boost/locale/gnu_gettext.hpp> 21 #include <boost/locale/gnu_gettext_info.hpp> 21 22 #include <boost/locale/info.hpp> 22 23 #include <boost/locale/localization_backend.hpp> 23 24 #include <boost/locale/message.hpp> -
new file oost/locale/gnu_gettext_info.hpp
diff --git a/boost/locale/gnu_gettext_info.hpp b/boost/locale/gnu_gettext_info.hpp new file mode 100644 index 0000000..b1220ed
- + 1 // 2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See 5 // accompanying file LICENSE_1_0.txt or copy at 6 // http://www.boost.org/LICENSE_1_0.txt) 7 // 8 #ifndef BOOST_LOCALE_GNU_GETTEXT_INFO_HPP_INCLUDED 9 #define BOOST_LOCALE_GNU_GETTEXT_INFO_HPP_INCLUDED 10 #include <boost/locale/config.hpp> 11 #include <locale> 12 #include <string> 13 14 15 namespace boost { 16 namespace locale { 17 namespace gnu_gettext { 18 /// 19 /// \brief This class holds functions that provide access to GNU Gettext settings 20 /// 21 class info { 22 public: 23 virtual ~info() 24 { 25 } 26 27 /// 28 /// Gets the language the catalog is loaded for 29 /// 30 virtual std::string language() = 0; 31 32 /// 33 /// Get the domain the catalog is loaded for 34 /// 35 virtual std::string domain() = 0; 36 37 /// 38 /// Get the the search path the catalog was found in 39 /// 40 virtual std::string search_path() = 0; 41 42 /// 43 /// Get the path format the catalog used 44 /// 45 virtual std::string path_format() = 0; 46 47 /// 48 /// Get the path format to the catalog used 49 /// 50 virtual std::string full_path() = 0; 51 }; 52 53 template <typename char_type> 54 info* get_info(std::locale const& loc); 55 56 template <> 57 BOOST_LOCALE_DECL info* get_info<char>(std::locale const& loc); 58 59 #ifdef BOOST_HAS_CHAR16_T 60 61 template <> 62 BOOST_LOCALE_DECL info* get_info<char16_t>(std::locale const& loc); 63 64 #endif 65 66 #ifdef BOOST_HAS_CHAR32_T 67 68 template <> 69 BOOST_LOCALE_DECL info* get_info<char32_t>(std::locale const& loc); 70 71 #endif 72 } 73 } 74 } 75 76 #endif 77 78 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 -
libs/locale/src/shared/message.cpp
diff --git a/libs/locale/src/shared/message.cpp b/libs/locale/src/shared/message.cpp index ce4b97c..e1d1ee5 100644
a b 9 9 #include <boost/config.hpp> 10 10 #include <boost/locale/message.hpp> 11 11 #include <boost/locale/gnu_gettext.hpp> 12 #include <boost/locale/gnu_gettext_info.hpp> 12 13 #include <boost/locale/format.hpp> 13 14 #include <boost/shared_ptr.hpp> 14 15 #include <boost/locale/encoding.hpp> … … namespace boost { 481 482 return buffer.c_str(); 482 483 } 483 484 485 struct catalog_settings 486 { 487 std::string domain; 488 std::string path; 489 std::string search_path; 490 std::string format; 491 std::string full_path; 492 }; 493 484 494 template<typename CharType> 485 495 class mo_message : public message_format<CharType> { 486 496 … … namespace boost { 581 591 for(unsigned k=0;!found && k<formats.size();k++) { 582 592 std::string full_path = (format(formats[k]) % search_paths[i] % paths[j] % lc_cat % domain).str(std::locale::classic()); 583 593 found = load_file(full_path,encoding,key_encoding,id,inf.callback); 594 595 if(found) 596 { 597 settings_.domain = domain; 598 settings_.path = paths[j]; 599 settings_.search_path = search_paths[i]; 600 settings_.format = formats[i]; 601 settings_.full_path = full_path; 602 } 584 603 } 585 604 } 586 605 } … … namespace boost { 596 615 { 597 616 } 598 617 618 catalog_settings get_catalog_settings() const 619 { 620 return settings_; 621 } 622 599 623 private: 600 624 int compare_encodings(std::string const &left,std::string const &right) 601 625 { … … namespace boost { 747 771 std::string locale_encoding_; 748 772 std::string key_encoding_; 749 773 bool key_conversion_required_; 774 775 catalog_settings settings_; 750 776 }; 751 777 752 778 template<> … … namespace boost { 778 804 return new mo_message<char32_t>(data); 779 805 } 780 806 #endif 807 808 template <typename char_type> 809 class mo_info : public info { 810 typedef mo_message<char_type> message_type; 811 812 public: 813 mo_info(message_type const* Msg) 814 : msg(Msg) 815 { 816 settings = msg->get_catalog_settings(); 817 } 818 819 virtual std::string language() 820 { 821 return settings.path; 822 } 823 824 virtual std::string domain() 825 { 826 return settings.domain; 827 } 828 829 virtual std::string search_path() 830 { 831 return settings.search_path; 832 } 833 834 virtual std::string path_format() 835 { 836 return settings.format; 837 } 838 839 virtual std::string full_path() 840 { 841 return settings.full_path; 842 } 843 844 private: 845 message_type const* msg; 846 catalog_settings settings; 847 }; 848 849 template <typename char_type> 850 info* _get_info(std::locale const& loc) 851 { 852 typedef mo_message<char_type> facet_type; 853 854 if(!std::has_facet<facet_type>(loc)) 855 { 856 return 0; 857 } 858 859 facet_type const* facet = &(std::use_facet<facet_type>(loc)); 860 861 return new mo_info<char_type>(facet); 862 } 863 864 template <> 865 info* get_info<char>(std::locale const& loc) 866 { 867 return _get_info<char>(loc); 868 } 869 870 template <> 871 info* get_info<wchar_t>(std::locale const& loc) 872 { 873 return _get_info<wchar_t>(loc); 874 } 875 876 #ifdef BOOST_HAS_CHAR16_T 877 878 template <> 879 info* get_info<char16_t>(std::locale const& loc) 880 { 881 return _get_info<char16_t>(loc); 882 } 883 884 #endif 885 886 #ifdef BOOST_HAS_CHAR32_T 887 888 template <> 889 info* get_info<char32_t>(std::locale const& loc) 890 { 891 return _get_info<char32_t>(loc); 892 } 893 894 #endif 895 781 896 } /// gnu_gettext 782 897 783 898 } // locale -
libs/locale/test/test_message.cpp
diff --git a/libs/locale/test/test_message.cpp b/libs/locale/test/test_message.cpp index 4b7f750..5a19bc8 100644
a b 10 10 #include <boost/locale/localization_backend.hpp> 11 11 #include <boost/locale/message.hpp> 12 12 #include <boost/locale/gnu_gettext.hpp> 13 #include <boost/locale/gnu_gettext_info.hpp> 13 14 #include <boost/locale/encoding.hpp> 14 15 #include "test_locale.hpp" 15 16 #include "test_locale_tools.hpp" … … int main(int argc,char **argv) 523 524 bl::dnpgettext("",L"",L"",L"",1); 524 525 } 525 526 527 std::cout << "Testing Gettext Info" << std::endl; 528 { 529 std::string format = "{1}/{2}/{3}/{4}.mo"; 530 std::string search_path; 531 if(argc==2) 532 search_path = argv[1]; 533 else 534 search_path = "./"; 535 536 boost::locale::generator g; 537 g.add_messages_domain("full"); 538 g.add_messages_path(search_path); 539 540 std::locale l = g("he_IL.UTF-8"); 541 542 bl::gnu_gettext::info* info = bl::gnu_gettext::get_info<char>(l); 543 544 TEST(info->language() == "he_IL"); 545 TEST(info->domain() == "full"); 546 TEST(info->path_format() == format); 547 TEST(info->full_path() == search_path + "/he_IL/LC_MESSAGES/full.mo"); 548 549 delete info; 550 } 526 551 } 527 552 catch(std::exception const &e) { 528 553 std::cerr << "Failed " << e.what() << std::endl;