Add German translation
[platform/upstream/libusb.git] / libusb / strerror.c
1 /*
2  * libusb strerror code
3  * Copyright © 2013 Hans de Goede <hdegoede@redhat.com>
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <config.h>
21
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #if defined(HAVE_STRINGS_H)
26 #include <strings.h>
27 #endif
28
29 #include "libusbi.h"
30
31 #if defined(_MSC_VER)
32 #define strncasecmp _strnicmp
33 #endif
34
35 static size_t usbi_locale = 0;
36
37 /** \ingroup libusb_misc
38  * How to add a new \ref libusb_strerror() translation:
39  * <ol>
40  * <li> Download the latest \c strerror.c from:<br>
41  *      https://raw.github.com/libusb/libusb/master/libusb/sterror.c </li>
42  * <li> Open the file in an UTF-8 capable editor </li>
43  * <li> Add the 2 letter <a href="http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639-1</a>
44  *      code for your locale at the end of \c usbi_locale_supported[]<br>
45  *    Eg. for Chinese, you would add "zh" so that:
46  *    \code... usbi_locale_supported[] = { "en", "nl", "fr" };\endcode
47  *    becomes:
48  *    \code... usbi_locale_supported[] = { "en", "nl", "fr", "zh" };\endcode </li>
49  * <li> Copy the <tt>{ / * English (en) * / ... }</tt> section and add it at the end of \c usbi_localized_errors<br>
50  *    Eg. for Chinese, the last section of \c usbi_localized_errors could look like:
51  *    \code
52  *     }, { / * Chinese (zh) * /
53  *         "Success",
54  *         ...
55  *         "Other error",
56  *     }
57  * };\endcode </li>
58  * <li> Translate each of the English messages from the section you copied into your language </li>
59  * <li> Save the file (in UTF-8 format) and send it to \c libusb-devel\@lists.sourceforge.net </li>
60  * </ol>
61  */
62
63 static const char* usbi_locale_supported[] = { "en", "nl", "fr", "ru", "de" };
64 static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUSB_ERROR_COUNT] = {
65         { /* English (en) */
66                 "Success",
67                 "Input/Output Error",
68                 "Invalid parameter",
69                 "Access denied (insufficient permissions)",
70                 "No such device (it may have been disconnected)",
71                 "Entity not found",
72                 "Resource busy",
73                 "Operation timed out",
74                 "Overflow",
75                 "Pipe error",
76                 "System call interrupted (perhaps due to signal)",
77                 "Insufficient memory",
78                 "Operation not supported or unimplemented on this platform",
79                 "Other error",
80         }, { /* Dutch (nl) */
81                 "Gelukt",
82                 "Invoer-/uitvoerfout",
83                 "Ongeldig argument",
84                 "Toegang geweigerd (onvoldoende toegangsrechten)",
85                 "Apparaat bestaat niet (verbinding met apparaat verbroken?)",
86                 "Niet gevonden",
87                 "Apparaat of hulpbron is bezig",
88                 "Bewerking verlopen",
89                 "Waarde is te groot",
90                 "Gebroken pijp",
91                 "Onderbroken systeemaanroep",
92                 "Onvoldoende geheugen beschikbaar",
93                 "Bewerking wordt niet ondersteund",
94                 "Andere fout",
95         }, { /* French (fr) */
96                 "Succès",
97                 "Erreur d'entrée/sortie",
98                 "Paramètre invalide",
99                 "Accès refusé (permissions insuffisantes)",
100                 "Périphérique introuvable (peut-être déconnecté)",
101                 "Elément introuvable",
102                 "Resource déjà occupée",
103                 "Operation expirée",
104                 "Débordement",
105                 "Erreur de pipe",
106                 "Appel système abandonné (peut-être à cause d’un signal)",
107                 "Mémoire insuffisante",
108                 "Opération non supportée or non implémentée sur cette plateforme",
109                 "Autre erreur",
110         }, { /* Russian (ru) */
111                 "Успех",
112                 "Ошибка ввода/вывода",
113                 "Неверный параметр",
114                 "Доступ запрещён (не хватает прав)",
115                 "Устройство отсутствует (возможно, оно было отсоединено)",
116                 "Элемент не найден",
117                 "Ресурс занят",
118                 "Истекло время ожидания операции",
119                 "Переполнение",
120                 "Ошибка канала",
121                 "Системный вызов прерван (возможно, сигналом)",
122                 "Память исчерпана",
123                 "Операция не поддерживается данной платформой",
124                 "Неизвестная ошибка"
125         
126         }, { /* German (de) */
127                 "Erfolgreich",
128                 "Eingabe-/Ausgabefehler",
129                 "Ungültiger Parameter",
130                 "Keine Berechtigung (Zugriffsrechte fehlen)",
131                 "Kein passendes Gerät gefunden (es könnte entfernt worden sein)",
132                 "Entität nicht gefunden",
133                 "Die Ressource ist belegt",
134                 "Die Wartezeit für die Operation ist abgelaufen",
135                 "Mehr Daten empfangen als erwartet",
136                 "Datenübergabe unterbrochen (broken pipe)",
137                 "Unterbrechung während des Betriebssystemaufrufs",
138                 "Nicht genügend Hauptspeicher verfügbar",
139                 "Die Operation wird nicht unterstützt oder ist auf dieser Platform nicht implementiert",
140                 "Allgemeiner Fehler",
141         }
142 };
143
144 /** \ingroup libusb_misc
145  * Set the language, and only the language, not the encoding! used for
146  * translatable libusb messages.
147  *
148  * This takes a locale string in the default setlocale format: lang[-region]
149  * or lang[_country_region][.codeset]. Only the lang part of the string is
150  * used, and only 2 letter ISO 639-1 codes are accepted for it, such as "de".
151  * The optional region, country_region or codeset parts are ignored. This
152  * means that functions which return translatable strings will NOT honor the
153  * specified encoding. 
154  * All strings returned are encoded as UTF-8 strings.
155  *
156  * If libusb_setlocale() is not called, all messages will be in English.
157  *
158  * The following functions return translatable strings: libusb_strerror().
159  * Note that the libusb log messages controlled through libusb_set_debug()
160  * are not translated, they are always in English.
161  *
162  * For POSIX UTF-8 environments if you want libusb to follow the standard
163  * locale settings, call libusb_setlocale(setlocale(LC_MESSAGES, NULL)),
164  * after your app has done its locale setup.
165  *
166  * \param locale locale-string in the form of lang[_country_region][.codeset]
167  * or lang[-region], where lang is a 2 letter ISO 639-1 code
168  * \returns LIBUSB_SUCCESS on success
169  * \returns LIBUSB_ERROR_INVALID_PARAM if the locale doesn't meet the requirements
170  * \returns LIBUSB_ERROR_NOT_FOUND if the requested language is not supported
171  * \returns a LIBUSB_ERROR code on other errors
172  */
173
174 int API_EXPORTED libusb_setlocale(const char *locale)
175 {
176         size_t i;
177
178         if ( (locale == NULL) || (strlen(locale) < 2)
179           || ((strlen(locale) > 2) && (locale[2] != '-') && (locale[2] != '_') && (locale[2] != '.')) )
180                 return LIBUSB_ERROR_INVALID_PARAM;
181
182         for (i=0; i<ARRAYSIZE(usbi_locale_supported); i++) {
183                 if (strncasecmp(usbi_locale_supported[i], locale, 2) == 0)
184                         break;
185         }
186         if (i >= ARRAYSIZE(usbi_locale_supported)) {
187                 return LIBUSB_ERROR_NOT_FOUND;
188         }
189
190         usbi_locale = i;
191
192         return LIBUSB_SUCCESS;
193 }
194
195 /** \ingroup libusb_misc
196  * Returns a constant string with a short description of the given error code,
197  * this description is intended for displaying to the end user and will be in
198  * the language set by libusb_setlocale().
199  *
200  * The returned string is encoded in UTF-8.
201  *
202  * The messages always start with a capital letter and end without any dot.
203  * The caller must not free() the returned string.
204  *
205  * \param errcode the error code whose description is desired
206  * \returns a short description of the error code in UTF-8 encoding
207  */
208 DEFAULT_VISIBILITY const char* LIBUSB_CALL libusb_strerror(enum libusb_error errcode)
209 {
210         int errcode_index = -errcode;
211
212         if ((errcode_index < 0) || (errcode_index >= LIBUSB_ERROR_COUNT)) {
213                 /* "Other Error", which should always be our last message, is returned */
214                 errcode_index = LIBUSB_ERROR_COUNT - 1;
215         }
216
217         return usbi_localized_errors[usbi_locale][errcode_index];
218 }