3 * Copyright © 2013 Hans de Goede <hdegoede@redhat.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #define strncasecmp _strnicmp
31 static size_t usbi_locale;
33 /** \ingroup libusb_misc
34 * How to add a new \ref libusb_strerror() translation:
36 * <li> Download the latest \c strerror.c from:<br>
37 * https://raw.github.com/libusb/libusb/master/libusb/strerror.c </li>
38 * <li> Open the file in an UTF-8 capable editor </li>
39 * <li> Add the 2 letter <a href="http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639-1</a>
40 * code for your locale at the end of \c usbi_locale_supported[]<br>
41 * Eg. for Chinese, you would add "zh" so that:
42 * \code... usbi_locale_supported[] = { "en", "nl", "fr" };\endcode
44 * \code... usbi_locale_supported[] = { "en", "nl", "fr", "zh" };\endcode </li>
45 * <li> Copy the <tt>{ / * English (en) * / ... }</tt> section and add it at the end of \c usbi_localized_errors<br>
46 * Eg. for Chinese, the last section of \c usbi_localized_errors could look like:
48 * }, { / * Chinese (zh) * /
54 * <li> Translate each of the English messages from the section you copied into your language </li>
55 * <li> Save the file (in UTF-8 format) and send it to \c libusb-devel\@lists.sourceforge.net </li>
59 static const char* usbi_locale_supported[] = { "en", "nl", "fr", "ru", "de", "hu" };
60 static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUSB_ERROR_COUNT] = {
65 "Access denied (insufficient permissions)",
66 "No such device (it may have been disconnected)",
69 "Operation timed out",
72 "System call interrupted (perhaps due to signal)",
73 "Insufficient memory",
74 "Operation not supported or unimplemented on this platform",
78 "Invoer-/uitvoerfout",
80 "Toegang geweigerd (onvoldoende toegangsrechten)",
81 "Apparaat bestaat niet (verbinding met apparaat verbroken?)",
83 "Apparaat of hulpbron is bezig",
87 "Onderbroken systeemaanroep",
88 "Onvoldoende geheugen beschikbaar",
89 "Bewerking wordt niet ondersteund",
91 }, { /* French (fr) */
93 "Erreur d'entrée/sortie",
95 "Accès refusé (permissions insuffisantes)",
96 "Périphérique introuvable (peut-être déconnecté)",
97 "Elément introuvable",
98 "Resource déjà occupée",
102 "Appel système abandonné (peut-être à cause d’un signal)",
103 "Mémoire insuffisante",
104 "Opération non supportée or non implémentée sur cette plateforme",
106 }, { /* Russian (ru) */
108 "Ошибка ввода/вывода",
110 "Доступ запрещён (не хватает прав)",
111 "Устройство отсутствует (возможно, оно было отсоединено)",
114 "Истекло время ожидания операции",
117 "Системный вызов прерван (возможно, сигналом)",
119 "Операция не поддерживается данной платформой",
122 }, { /* German (de) */
124 "Eingabe-/Ausgabefehler",
125 "Ungültiger Parameter",
126 "Keine Berechtigung (Zugriffsrechte fehlen)",
127 "Kein passendes Gerät gefunden (es könnte entfernt worden sein)",
128 "Entität nicht gefunden",
129 "Die Ressource ist belegt",
130 "Die Wartezeit für die Operation ist abgelaufen",
131 "Mehr Daten empfangen als erwartet",
132 "Datenübergabe unterbrochen (broken pipe)",
133 "Unterbrechung während des Betriebssystemaufrufs",
134 "Nicht genügend Hauptspeicher verfügbar",
135 "Die Operation wird nicht unterstützt oder ist auf dieser Platform nicht implementiert",
136 "Allgemeiner Fehler",
137 }, { /* Hungarian (hu) */
140 "Érvénytelen paraméter",
141 "Hozzáférés megtagadva",
142 "Az eszköz nem található (eltávolították?)",
144 "Az erőforrás foglalt",
147 "Törött adatcsatorna",
148 "Rendszerhívás megszakítva",
149 "Nincs elég memória",
150 "A művelet nem támogatott ezen a rendszeren",
155 /** \ingroup libusb_misc
156 * Set the language, and only the language, not the encoding! used for
157 * translatable libusb messages.
159 * This takes a locale string in the default setlocale format: lang[-region]
160 * or lang[_country_region][.codeset]. Only the lang part of the string is
161 * used, and only 2 letter ISO 639-1 codes are accepted for it, such as "de".
162 * The optional region, country_region or codeset parts are ignored. This
163 * means that functions which return translatable strings will NOT honor the
164 * specified encoding.
165 * All strings returned are encoded as UTF-8 strings.
167 * If libusb_setlocale() is not called, all messages will be in English.
169 * The following functions return translatable strings: libusb_strerror().
170 * Note that the libusb log messages controlled through libusb_set_debug()
171 * are not translated, they are always in English.
173 * For POSIX UTF-8 environments if you want libusb to follow the standard
174 * locale settings, call libusb_setlocale(setlocale(LC_MESSAGES, NULL)),
175 * after your app has done its locale setup.
177 * \param locale locale-string in the form of lang[_country_region][.codeset]
178 * or lang[-region], where lang is a 2 letter ISO 639-1 code
179 * \returns LIBUSB_SUCCESS on success
180 * \returns LIBUSB_ERROR_INVALID_PARAM if the locale doesn't meet the requirements
181 * \returns LIBUSB_ERROR_NOT_FOUND if the requested language is not supported
182 * \returns a LIBUSB_ERROR code on other errors
185 int API_EXPORTED libusb_setlocale(const char *locale)
189 if ( (locale == NULL) || (strlen(locale) < 2)
190 || ((strlen(locale) > 2) && (locale[2] != '-') && (locale[2] != '_') && (locale[2] != '.')) )
191 return LIBUSB_ERROR_INVALID_PARAM;
193 for (i=0; i<ARRAYSIZE(usbi_locale_supported); i++) {
194 if (strncasecmp(usbi_locale_supported[i], locale, 2) == 0)
197 if (i >= ARRAYSIZE(usbi_locale_supported)) {
198 return LIBUSB_ERROR_NOT_FOUND;
203 return LIBUSB_SUCCESS;
206 /** \ingroup libusb_misc
207 * Returns a constant string with a short description of the given error code,
208 * this description is intended for displaying to the end user and will be in
209 * the language set by libusb_setlocale().
211 * The returned string is encoded in UTF-8.
213 * The messages always start with a capital letter and end without any dot.
214 * The caller must not free() the returned string.
216 * \param errcode the error code whose description is desired
217 * \returns a short description of the error code in UTF-8 encoding
219 DEFAULT_VISIBILITY const char* LIBUSB_CALL libusb_strerror(int errcode)
221 int errcode_index = -errcode;
223 if ((errcode_index < 0) || (errcode_index >= LIBUSB_ERROR_COUNT)) {
224 /* "Other Error", which should always be our last message, is returned */
225 errcode_index = LIBUSB_ERROR_COUNT - 1;
228 return usbi_localized_errors[usbi_locale][errcode_index];