4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Units/UnitsGlue.hpp"
25 #include "Units/UnitsStore.hpp"
26 #include "LogFile.hpp"
36 #include "OS/DynamicLibrary.hpp"
40 #include "Java/Global.hpp"
41 #include "Java/Class.hpp"
42 #include "Java/Object.hpp"
45 struct language_unit_map
{
47 const TCHAR
* region_code
;
53 * Several fake WIN32 constants. These are not used on Android, but
54 * we need them or we have to have a separate version of
55 * #language_table on Android.
66 #define MAKELANGID(x,y) 0
70 #if !defined(HAVE_POSIX) || defined(ANDROID)
72 const struct language_unit_map language_table
[] = {
73 { MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_UK
), _T("en_UK"), 1 },
74 { MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), _T("en_US"), 2 },
75 { MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_AUS
), _T("en_AU"), 3 },
83 FindLanguage(LANGID lang
)
85 // Search for supported languages matching the language code
86 for (unsigned i
= 0; language_table
[i
].region_code
!= NULL
; ++i
)
87 if (language_table
[i
].region_id
== lang
)
88 return language_table
[i
].store_index
;
92 #elif defined(ANDROID)
94 FindLanguage(const TCHAR
* lang
)
96 // Search for supported languages matching the language code
97 for (unsigned i
= 0; language_table
[i
].region_code
!= NULL
; ++i
)
98 if (_tcscmp(language_table
[i
].region_code
, lang
) == 0)
99 return language_table
[i
].store_index
;
110 #if defined(_WIN32_WCE)
111 /* the GetUserDefaultUILanguage() prototype is missing on
112 mingw32ce, we have to look it up dynamically */
113 DynamicLibrary
coreloc_dll(_T("coredll"));
114 if (!coreloc_dll
.IsDefined()) {
115 LogFormat("Units: coredll.dll not found");
119 typedef LANGID
WINAPI (*GetUserDefaultUILanguage_t
)();
120 GetUserDefaultUILanguage_t GetUserDefaultUILanguage
=
121 (GetUserDefaultUILanguage_t
)
122 coreloc_dll
.Lookup(_T("GetUserDefaultUILanguage"));
123 if (GetUserDefaultUILanguage
== NULL
) {
124 LogFormat("Units: GetUserDefaultUILanguage() not available");
129 // Retrieve the default user language identifier from the OS
130 LANGID lang_id
= GetUserDefaultUILanguage();
131 LogFormat("Units: GetUserDefaultUILanguage() = 0x%x", (int)lang_id
);
135 return FindLanguage(lang_id
);
137 #elif defined(ANDROID)
138 JNIEnv
*env
= Java::GetEnv();
140 Java::Class
cls(env
, "java/util/Locale");
142 // Call static function Locale.getDefault() that
143 // returns the user's default Locale object
145 jmethodID cid
= env
->GetStaticMethodID(cls
, "getDefault",
146 "()Ljava/util/Locale;");
149 jobject _obj
= env
->CallStaticObjectMethod(cls
, cid
);
153 Java::LocalObject
obj(env
, _obj
);
155 // Call function Locale.getLanguage() that
156 // returns a two-letter language string
158 cid
= env
->GetMethodID(cls
, "toString", "()Ljava/lang/String;");
161 jstring language
= (jstring
)env
->CallObjectMethod(obj
, cid
);
162 if (language
== NULL
)
165 // Convert the jstring to a char string
166 const char *language2
= env
->GetStringUTFChars(language
, NULL
);
167 if (language2
== NULL
) {
168 env
->DeleteLocalRef(language
);
172 unsigned id
= FindLanguage(language2
);
174 // Clean up the memory
175 env
->ReleaseStringUTFChars(language
, language2
);
176 env
->DeleteLocalRef(language
);
178 // Return e.g. "de.mo"
182 // Metric default on Linux
188 Units::LoadFromOSLanguage()
190 unsigned index
= AutoDetect();
191 return Units::Store::Read(index
);