summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/locale/examples
diff options
context:
space:
mode:
Diffstat (limited to 'src/boost/libs/locale/examples')
-rw-r--r--src/boost/libs/locale/examples/boundary.cpp72
-rw-r--r--src/boost/libs/locale/examples/calendar.cpp71
-rw-r--r--src/boost/libs/locale/examples/collate.cpp40
-rw-r--r--src/boost/libs/locale/examples/conversions.cpp44
-rw-r--r--src/boost/libs/locale/examples/hello.cpp41
-rw-r--r--src/boost/libs/locale/examples/wboundary.cpp104
-rw-r--r--src/boost/libs/locale/examples/wconversions.cpp73
-rw-r--r--src/boost/libs/locale/examples/whello.cpp45
8 files changed, 490 insertions, 0 deletions
diff --git a/src/boost/libs/locale/examples/boundary.cpp b/src/boost/libs/locale/examples/boundary.cpp
new file mode 100644
index 000000000..7c643f938
--- /dev/null
+++ b/src/boost/libs/locale/examples/boundary.cpp
@@ -0,0 +1,72 @@
+//
+// 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)
+//
+#include <boost/locale.hpp>
+#include <iostream>
+#include <cassert>
+#include <ctime>
+
+int main()
+{
+ using namespace boost::locale;
+ using namespace std;
+
+ generator gen;
+ // Make system default locale global
+ std::locale loc = gen("");
+ locale::global(loc);
+ cout.imbue(loc);
+
+
+ string text="Hello World! あにま! Linux2.6 and Windows7 is word and number. שָלוֹם עוֹלָם!";
+
+ cout<<text<<endl;
+
+ boundary::ssegment_index index(boundary::word,text.begin(),text.end());
+ boundary::ssegment_index::iterator p,e;
+
+ for(p=index.begin(),e=index.end();p!=e;++p) {
+ cout<<"Part ["<<*p<<"] has ";
+ if(p->rule() & boundary::word_number)
+ cout<<"number(s) ";
+ if(p->rule() & boundary::word_letter)
+ cout<<"letter(s) ";
+ if(p->rule() & boundary::word_kana)
+ cout<<"kana character(s) ";
+ if(p->rule() & boundary::word_ideo)
+ cout<<"ideographic character(s) ";
+ if(p->rule() & boundary::word_none)
+ cout<<"no word characters";
+ cout<<endl;
+ }
+
+ index.map(boundary::character,text.begin(),text.end());
+
+ for(p=index.begin(),e=index.end();p!=e;++p) {
+ cout<<"|" <<*p ;
+ }
+ cout<<"|\n\n";
+
+ index.map(boundary::line,text.begin(),text.end());
+
+ for(p=index.begin(),e=index.end();p!=e;++p) {
+ cout<<"|" <<*p ;
+ }
+ cout<<"|\n\n";
+
+ index.map(boundary::sentence,text.begin(),text.end());
+
+ for(p=index.begin(),e=index.end();p!=e;++p) {
+ cout<<"|" <<*p ;
+ }
+ cout<<"|\n\n";
+
+}
+
+// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
+
+// boostinspect:noascii
diff --git a/src/boost/libs/locale/examples/calendar.cpp b/src/boost/libs/locale/examples/calendar.cpp
new file mode 100644
index 000000000..d905511a7
--- /dev/null
+++ b/src/boost/libs/locale/examples/calendar.cpp
@@ -0,0 +1,71 @@
+//
+// 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)
+//
+#include <boost/locale.hpp>
+#include <iostream>
+#include <iomanip>
+#include <ctime>
+
+int main()
+{
+ using namespace boost::locale;
+
+ generator gen;
+ std::locale::global(gen(""));
+ std::cout.imbue(std::locale());
+ // Setup environment
+
+ boost::locale::date_time now;
+
+ date_time start=now;
+
+ // Set the first day of the first month of this year
+ start.set(period::month(),now.minimum(period::month()));
+ start.set(period::day(),start.minimum(period::day()));
+
+ int current_year = period::year(now);
+
+
+ // Display current year
+ std::cout << format("{1,ftime='%Y'}") % now << std::endl;
+
+ //
+ // Run forward untill current year is the date
+ //
+ for(now=start; period::year(now) == current_year;) {
+
+ // Print heading of month
+ if(calendar().is_gregorian())
+ std::cout << format("{1,ftime='%B'}") % now <<std::endl;
+ else
+ std::cout << format("{1,ftime='%B'} ({1,ftime='%Y-%m-%d',locale=en} - {2,locale=en,ftime='%Y-%m-%d'})")
+ % now
+ % date_time(now,now.maximum(period::day())*period::day()) << std::endl;
+
+ int first = calendar().first_day_of_week();
+
+ // Print weeks days
+ for(int i=0;i<7;i++) {
+ date_time tmp(now,period::day_of_week() * (first + i));
+ std::cout << format("{1,w=8,ftime='%a'} ") % tmp;
+ }
+ std::cout << std::endl;
+
+ int current_month = now / period::month();
+ int skip = now / period::day_of_week_local() - 1;
+ for(int i=0;i<skip*9;i++)
+ std::cout << ' ';
+ for(;now / period::month() == current_month ;now += period::day()) {
+ std::cout << format("{1,w=8,ftime='%e'} ") % now;
+ if(now / period::day_of_week_local() == 7)
+ std::cout << std::endl;
+ }
+ std::cout << std::endl;
+ }
+
+}
+// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
diff --git a/src/boost/libs/locale/examples/collate.cpp b/src/boost/libs/locale/examples/collate.cpp
new file mode 100644
index 000000000..9e14e43ab
--- /dev/null
+++ b/src/boost/libs/locale/examples/collate.cpp
@@ -0,0 +1,40 @@
+//
+// 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)
+//
+#include <iostream>
+#include <string>
+#include <set>
+
+#include <boost/locale.hpp>
+
+using namespace std;
+using namespace boost::locale;
+
+int main()
+{
+ generator gen;
+ std::locale::global(gen(""));
+ /// Set global locale to requested
+
+ /// Create a set that includes all strings sorted according to ABC order
+ /// std::locale can be used as object for comparison
+ typedef std::set<std::string,std::locale> set_type;
+ set_type all_strings;
+
+ /// Read all strings into the set
+ while(!cin.eof()) {
+ std::string tmp;
+ getline(cin,tmp);
+ all_strings.insert(tmp);
+ }
+ /// Print them out
+ for(set_type::iterator p=all_strings.begin();p!=all_strings.end();++p) {
+ cout<<*p<<endl;
+ }
+
+}
+// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
diff --git a/src/boost/libs/locale/examples/conversions.cpp b/src/boost/libs/locale/examples/conversions.cpp
new file mode 100644
index 000000000..74037f235
--- /dev/null
+++ b/src/boost/libs/locale/examples/conversions.cpp
@@ -0,0 +1,44 @@
+//
+// 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)
+//
+#include <boost/locale.hpp>
+#include <boost/algorithm/string/case_conv.hpp>
+#include <iostream>
+
+#include <ctime>
+
+
+
+int main()
+{
+ using namespace boost::locale;
+ using namespace std;
+ // Create system default locale
+ generator gen;
+ locale loc=gen("");
+ locale::global(loc);
+ cout.imbue(loc);
+
+
+ cout<<"Correct case conversion can't be done by simple, character by character conversion"<<endl;
+ cout<<"because case conversion is context sensitive and not 1-to-1 conversion"<<endl;
+ cout<<"For example:"<<endl;
+ cout<<" German grüßen correctly converted to "<<to_upper("grüßen")<<", instead of incorrect "
+ <<boost::to_upper_copy(std::string("grüßen"))<<endl;
+ cout<<" where ß is replaced with SS"<<endl;
+ cout<<" Greek ὈΔΥΣΣΕΎΣ is correctly converted to "<<to_lower("ὈΔΥΣΣΕΎΣ")<<", instead of incorrect "
+ <<boost::to_lower_copy(std::string("ὈΔΥΣΣΕΎΣ"))<<endl;
+ cout<<" where Σ is converted to σ or to ς, according to position in the word"<<endl;
+ cout<<"Such type of conversion just can't be done using std::toupper that work on character base, also std::toupper is "<<endl;
+ cout<<"not even applicable when working with variable character length like in UTF-8 or UTF-16 limiting the correct "<<endl;
+ cout<<"behavior to unicode subset BMP or ASCII only"<<endl;
+
+}
+
+// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
+
+// boostinspect:noascii
diff --git a/src/boost/libs/locale/examples/hello.cpp b/src/boost/libs/locale/examples/hello.cpp
new file mode 100644
index 000000000..e1a2d93b9
--- /dev/null
+++ b/src/boost/libs/locale/examples/hello.cpp
@@ -0,0 +1,41 @@
+//
+// 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)
+//
+#include <boost/locale.hpp>
+#include <iostream>
+
+#include <ctime>
+
+int main()
+{
+ using namespace boost::locale;
+ using namespace std;
+ generator gen;
+ locale loc=gen("");
+ // Create system default locale
+
+ locale::global(loc);
+ // Make it system global
+
+ cout.imbue(loc);
+ // Set as default locale for output
+
+ cout <<format("Today {1,date} at {1,time} we had run our first localization example") % time(0)
+ <<endl;
+
+ cout<<"This is how we show numbers in this locale "<<as::number << 103.34 <<endl;
+ cout<<"This is how we show currency in this locale "<<as::currency << 103.34 <<endl;
+ cout<<"This is typical date in the locale "<<as::date << std::time(0) <<endl;
+ cout<<"This is typical time in the locale "<<as::time << std::time(0) <<endl;
+ cout<<"This is upper case "<<to_upper("Hello World!")<<endl;
+ cout<<"This is lower case "<<to_lower("Hello World!")<<endl;
+ cout<<"This is title case "<<to_title("Hello World!")<<endl;
+ cout<<"This is fold case "<<fold_case("Hello World!")<<endl;
+
+}
+
+// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
diff --git a/src/boost/libs/locale/examples/wboundary.cpp b/src/boost/libs/locale/examples/wboundary.cpp
new file mode 100644
index 000000000..a14325faa
--- /dev/null
+++ b/src/boost/libs/locale/examples/wboundary.cpp
@@ -0,0 +1,104 @@
+//
+// 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)
+//
+
+//
+// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
+//
+// BIG FAT WARNING FOR Microsoft Visual Studio Users
+//
+// YOU NEED TO CONVERT THIS SOURCE FILE ENCODING TO UTF-8 WITH BOM ENCODING.
+//
+// Unfortunately MSVC understands that the source code is encoded as
+// UTF-8 only if you add useless BOM in the beginning.
+//
+// So, before you compile "wide" examples with MSVC, please convert them to text
+// files with BOM. There are two very simple ways to do it:
+//
+// 1. Open file with Notepad and save it from there. It would convert
+// it to file with BOM.
+// 2. In Visual Studio go File->Advances Save Options... and select
+// Unicode (UTF-8 with signature) Codepage 65001
+//
+// Note: once converted to UTF-8 with BOM, this source code would not
+// compile with other compilers, because no-one uses BOM with UTF-8 today
+// because it is absolutely meaningless in context of UTF-8.
+//
+// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
+//
+#include <boost/locale.hpp>
+#include <iostream>
+#include <cassert>
+#include <ctime>
+
+int main()
+{
+ using namespace boost::locale;
+ using namespace std;
+
+ // Create system default locale
+ generator gen;
+ locale loc=gen("");
+ locale::global(loc);
+ wcout.imbue(loc);
+
+ // This is needed to prevent C library to
+ // convert strings to narrow
+ // instead of C++ on some platforms
+ std::ios_base::sync_with_stdio(false);
+
+
+ wstring text=L"Hello World! あにま! Linux2.6 and Windows7 is word and number. שָלוֹם עוֹלָם!";
+
+ wcout<<text<<endl;
+
+ boundary::wssegment_index index(boundary::word,text.begin(),text.end());
+ boundary::wssegment_index::iterator p,e;
+
+ for(p=index.begin(),e=index.end();p!=e;++p) {
+ wcout<<L"Part ["<<*p<<L"] has ";
+ if(p->rule() & boundary::word_number)
+ wcout<<L"number(s) ";
+ if(p->rule() & boundary::word_letter)
+ wcout<<L"letter(s) ";
+ if(p->rule() & boundary::word_kana)
+ wcout<<L"kana character(s) ";
+ if(p->rule() & boundary::word_ideo)
+ wcout<<L"ideographic character(s) ";
+ if(p->rule() & boundary::word_none)
+ wcout<<L"no word characters";
+ wcout<<endl;
+ }
+
+ index.map(boundary::character,text.begin(),text.end());
+
+ for(p=index.begin(),e=index.end();p!=e;++p) {
+ wcout<<L"|" <<*p ;
+ }
+ wcout<<L"|\n\n";
+
+ index.map(boundary::line,text.begin(),text.end());
+
+ for(p=index.begin(),e=index.end();p!=e;++p) {
+ wcout<<L"|" <<*p ;
+ }
+ wcout<<L"|\n\n";
+
+ index.map(boundary::sentence,text.begin(),text.end());
+
+ for(p=index.begin(),e=index.end();p!=e;++p) {
+ wcout<<L"|" <<*p ;
+ }
+ wcout<<"|\n\n";
+
+}
+
+
+// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
+
+// boostinspect:noascii
+
diff --git a/src/boost/libs/locale/examples/wconversions.cpp b/src/boost/libs/locale/examples/wconversions.cpp
new file mode 100644
index 000000000..d26a2a477
--- /dev/null
+++ b/src/boost/libs/locale/examples/wconversions.cpp
@@ -0,0 +1,73 @@
+//
+// 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)
+//
+
+//
+// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
+//
+// BIG FAT WARNING FOR Microsoft Visual Studio Users
+//
+// YOU NEED TO CONVERT THIS SOURCE FILE ENCODING TO UTF-8 WITH BOM ENCODING.
+//
+// Unfortunately MSVC understands that the source code is encoded as
+// UTF-8 only if you add useless BOM in the beginning.
+//
+// So, before you compile "wide" examples with MSVC, please convert them to text
+// files with BOM. There are two very simple ways to do it:
+//
+// 1. Open file with Notepad and save it from there. It would convert
+// it to file with BOM.
+// 2. In Visual Studio go File->Advances Save Options... and select
+// Unicode (UTF-8 with signature) Codepage 65001
+//
+// Note: once converted to UTF-8 with BOM, this source code would not
+// compile with other compilers, because no-one uses BOM with UTF-8 today
+// because it is absolutely meaningless in context of UTF-8.
+//
+// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
+//
+#include <boost/locale.hpp>
+#include <boost/algorithm/string/case_conv.hpp>
+#include <iostream>
+
+#include <ctime>
+
+
+int main()
+{
+ using namespace boost::locale;
+ using namespace std;
+ // Create system default locale
+ generator gen;
+ locale loc=gen("");
+ locale::global(loc);
+ wcout.imbue(loc);
+
+ // This is needed to prevent C library to
+ // convert strings to narrow
+ // instead of C++ on some platforms
+ std::ios_base::sync_with_stdio(false);
+
+
+ wcout<<L"Correct case conversion can't be done by simple, character by character conversion"<<endl;
+ wcout<<L"because case conversion is context sensitive and not 1-to-1 conversion"<<endl;
+ wcout<<L"For example:"<<endl;
+ wcout<<L" German grüßen correctly converted to "<<to_upper(L"grüßen")<<L", instead of incorrect "
+ <<boost::to_upper_copy(std::wstring(L"grüßen"))<<endl;
+ wcout<<L" where ß is replaced with SS"<<endl;
+ wcout<<L" Greek ὈΔΥΣΣΕΎΣ is correctly converted to "<<to_lower(L"ὈΔΥΣΣΕΎΣ")<<L", instead of incorrect "
+ <<boost::to_lower_copy(std::wstring(L"ὈΔΥΣΣΕΎΣ"))<<endl;
+ wcout<<L" where Σ is converted to σ or to ς, according to position in the word"<<endl;
+ wcout<<L"Such type of conversion just can't be done using std::toupper that work on character base, also std::toupper is "<<endl;
+ wcout<<L"not fully applicable when working with variable character length like in UTF-8 or UTF-16 limiting the correct "<<endl;
+ wcout<<L"behavoir to BMP or ASCII only"<<endl;
+
+}
+
+// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
+
+// boostinspect:noascii
diff --git a/src/boost/libs/locale/examples/whello.cpp b/src/boost/libs/locale/examples/whello.cpp
new file mode 100644
index 000000000..a224eaf42
--- /dev/null
+++ b/src/boost/libs/locale/examples/whello.cpp
@@ -0,0 +1,45 @@
+//
+// 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)
+//
+#include <boost/locale.hpp>
+#include <iostream>
+
+#include <ctime>
+
+int main()
+{
+ using namespace boost::locale;
+ using namespace std;
+
+ // Create system default locale
+ generator gen;
+ locale loc=gen("");
+ locale::global(loc);
+ wcout.imbue(loc);
+
+ // This is needed to prevent C library to
+ // convert strings to narrow
+ // instead of C++ on some platforms
+ std::ios_base::sync_with_stdio(false);
+
+
+ wcout <<wformat(L"Today {1,date} at {1,time} we had run our first localization example") % time(0)
+ <<endl;
+
+ wcout<<L"This is how we show numbers in this locale "<<as::number << 103.34 <<endl;
+ wcout<<L"This is how we show currency in this locale "<<as::currency << 103.34 <<endl;
+ wcout<<L"This is typical date in the locale "<<as::date << std::time(0) <<endl;
+ wcout<<L"This is typical time in the locale "<<as::time << std::time(0) <<endl;
+ wcout<<L"This is upper case "<<to_upper(L"Hello World!")<<endl;
+ wcout<<L"This is lower case "<<to_lower(L"Hello World!")<<endl;
+ wcout<<L"This is title case "<<to_title(L"Hello World!")<<endl;
+ wcout<<L"This is fold case "<<fold_case(L"Hello World!")<<endl;
+
+}
+
+
+// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4