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