]> git.saurik.com Git - wxWidgets.git/blob - tests/intl/intltest.cpp
Romanian translations update from Cătălin Răceanu.
[wxWidgets.git] / tests / intl / intltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/intl/intltest.cpp
3 // Purpose: wxLocale unit test
4 // Author: Vaclav Slavik
5 // Created: 2007-03-26
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vaclav Slavik
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/intl.h"
25
26 #if wxUSE_INTL
27
28 // ----------------------------------------------------------------------------
29 // test class
30 // ----------------------------------------------------------------------------
31
32 class IntlTestCase : public CppUnit::TestCase
33 {
34 public:
35 IntlTestCase() { m_locale=NULL; }
36
37 virtual void setUp();
38 virtual void tearDown();
39
40 private:
41 CPPUNIT_TEST_SUITE( IntlTestCase );
42 CPPUNIT_TEST( RestoreLocale );
43 CPPUNIT_TEST( Domain );
44 CPPUNIT_TEST( Headers );
45 CPPUNIT_TEST( DateTimeFmtFrench );
46 CPPUNIT_TEST( DateTimeFmtC );
47 CPPUNIT_TEST( IsAvailable );
48 CPPUNIT_TEST_SUITE_END();
49
50 void RestoreLocale();
51 void Domain();
52 void Headers();
53 void DateTimeFmtFrench();
54 void DateTimeFmtC();
55 void IsAvailable();
56
57 static wxString GetDecimalPoint()
58 {
59 return wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
60 }
61
62 wxLocale *m_locale;
63
64 DECLARE_NO_COPY_CLASS(IntlTestCase)
65 };
66
67 // register in the unnamed registry so that these tests are run by default
68 CPPUNIT_TEST_SUITE_REGISTRATION( IntlTestCase );
69
70 // also include in its own registry so that these tests can be run alone
71 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IntlTestCase, "IntlTestCase" );
72
73 void IntlTestCase::setUp()
74 {
75 // Check that French locale is supported, this test doesn't work without it
76 // and all the other function need to check whether m_locale is non-NULL
77 // before continuing
78 if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
79 return;
80
81 wxLocale::AddCatalogLookupPathPrefix("./intl");
82
83 m_locale = new wxLocale;
84 CPPUNIT_ASSERT( m_locale );
85
86 // don't load default catalog, it may be unavailable:
87 bool loaded = m_locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT);
88 CPPUNIT_ASSERT( loaded );
89
90 m_locale->AddCatalog("internat");
91 }
92
93 void IntlTestCase::tearDown()
94 {
95 if (m_locale)
96 {
97 delete m_locale;
98 m_locale = NULL;
99 }
100 }
101
102 void IntlTestCase::RestoreLocale()
103 {
104 if ( !m_locale )
105 return;
106
107 // We must be using the French locale now, it was changed in setUp().
108 CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() );
109
110 // Switch to the English locale.
111 {
112 wxLocale locEn(wxLANGUAGE_ENGLISH);
113 CPPUNIT_ASSERT_EQUAL( ".", GetDecimalPoint() );
114 }
115
116 // Verify that after destroying the English locale object, French locale is
117 // restored.
118 CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() );
119 }
120
121 void IntlTestCase::Domain()
122 {
123 if (!m_locale)
124 return;
125
126 // _() searches all domains by default:
127 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", _("&Open bogus file") );
128
129 // search in our domain only:
130 CPPUNIT_ASSERT_EQUAL( "&Ouvrir un fichier", wxGetTranslation("&Open bogus file", "internat") );
131
132 // search in a domain that doesn't have this string:
133 CPPUNIT_ASSERT_EQUAL( "&Open bogus file", wxGetTranslation("&Open bogus file", "BogusDomain") );
134 }
135
136 void IntlTestCase::Headers()
137 {
138 if ( !m_locale )
139 return;
140
141 CPPUNIT_ASSERT_EQUAL( "wxWindows 2.0 i18n sample", m_locale->GetHeaderValue("Project-Id-Version") );
142 CPPUNIT_ASSERT_EQUAL( "1999-01-13 18:19+0100", m_locale->GetHeaderValue("POT-Creation-Date") );
143 CPPUNIT_ASSERT_EQUAL( "YEAR-MO-DA HO:MI+ZONE", m_locale->GetHeaderValue("PO-Revision-Date") );
144 CPPUNIT_ASSERT_EQUAL( "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>", m_locale->GetHeaderValue("Last-Translator") );
145 CPPUNIT_ASSERT_EQUAL( "1.0", m_locale->GetHeaderValue("MIME-Version") );
146 CPPUNIT_ASSERT_EQUAL( "text/plain; charset=utf-8", m_locale->GetHeaderValue("Content-Type") );
147 CPPUNIT_ASSERT_EQUAL( "8bit", m_locale->GetHeaderValue("Content-Transfer-Encoding") );
148
149 // check that it fails with a bogus domain:
150 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("POT-Creation-Date", "Bogus") );
151
152 // and that it fails for nonexisting header:
153 CPPUNIT_ASSERT_EQUAL( "", m_locale->GetHeaderValue("X-Not-Here") );
154 }
155
156 static wxString
157 NormalizeFormat(const wxString& fmtOrig)
158 {
159 wxString fmt(fmtOrig);
160
161 #ifdef __GLIBC__
162 // glibc uses some extensions in its formats which we need to convert to
163 // standard form
164 fmt.Replace("%T", "%H:%M:%S");
165 fmt.Replace("%e", "%d");
166 #endif // __GLIBC__
167
168 return fmt;
169 }
170
171 #define WX_ASSERT_EQUAL_FORMAT(msg, expected, actual) \
172 if ( !actual.empty() ) \
173 CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, expected, NormalizeFormat(actual))
174
175 void IntlTestCase::DateTimeFmtFrench()
176 {
177 if ( !m_locale )
178 return;
179
180 #ifdef __GLIBC__
181 // Versions of glibc up to 2.7 wrongly used periods for French locale
182 // separator.
183 #if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 8
184 static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
185 #else
186 static const char *FRENCH_DATE_FMT = "%d.%m.%Y";
187 #endif
188 static const char *FRENCH_LONG_DATE_FMT = "%a %d %b %Y";
189 static const char *FRENCH_DATE_TIME_FMT = "%a %d %b %Y %H:%M:%S %Z";
190 #else
191 static const char *FRENCH_DATE_FMT = "%d/%m/%Y";
192 static const char *FRENCH_LONG_DATE_FMT = "%A %d %B %Y";
193 #ifdef __WXOSX__
194 static const char *FRENCH_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
195 #else
196 static const char *FRENCH_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
197 #endif
198 #endif
199
200 WX_ASSERT_EQUAL_FORMAT( "French short date", FRENCH_DATE_FMT,
201 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
202 WX_ASSERT_EQUAL_FORMAT( "French long date", FRENCH_LONG_DATE_FMT,
203 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
204 WX_ASSERT_EQUAL_FORMAT( "French date and time", FRENCH_DATE_TIME_FMT,
205 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
206 WX_ASSERT_EQUAL_FORMAT( "French time", "%H:%M:%S",
207 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
208 }
209
210 void IntlTestCase::DateTimeFmtC()
211 {
212 // again, glibc uses different defaults
213 #ifdef __GLIBC__
214 static const char *C_DATE_FMT = "%m/%d/%y";
215 static const char *C_LONG_DATE_FMT = "%a %b %d %Y";
216 static const char *C_DATE_TIME_FMT = "%a %b %d %H:%M:%S %Y";
217 #else
218 static const char *C_DATE_FMT = "%d/%m/%Y";
219 static const char *C_LONG_DATE_FMT = "%A %d %B %Y";
220 #ifdef __WXOSX__
221 static const char *C_DATE_TIME_FMT = "%A %d %B %Y %H:%M:%S";
222 #else
223 static const char *C_DATE_TIME_FMT = "%d/%m/%Y %H:%M:%S";
224 #endif
225 #endif
226
227 setlocale(LC_ALL, "C");
228
229 WX_ASSERT_EQUAL_FORMAT( "C short date", C_DATE_FMT,
230 m_locale->GetInfo(wxLOCALE_SHORT_DATE_FMT) );
231 WX_ASSERT_EQUAL_FORMAT( "C long date", C_LONG_DATE_FMT,
232 m_locale->GetInfo(wxLOCALE_LONG_DATE_FMT) );
233 WX_ASSERT_EQUAL_FORMAT( "C date and time", C_DATE_TIME_FMT,
234 m_locale->GetInfo(wxLOCALE_DATE_TIME_FMT) );
235 WX_ASSERT_EQUAL_FORMAT( "C time", "%H:%M:%S",
236 m_locale->GetInfo(wxLOCALE_TIME_FMT) );
237 }
238
239 void IntlTestCase::IsAvailable()
240 {
241 const wxString origLocale(setlocale(LC_ALL, NULL));
242
243 // Calling IsAvailable() shouldn't change the locale.
244 wxLocale::IsAvailable(wxLANGUAGE_ENGLISH);
245
246 CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, NULL) );
247 }
248
249 #endif // wxUSE_INTL