-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Description
Is your feature request related to a problem? Please describe.
Imagine multilingual API. There are hardcoded messages which should be translated. The first thing comes to mind is error messages.
components:
schemas:
ClientError:
error_code:
type: integer
example: 400
error_message:
type: string
example: 'Bad request.'
Describe the solution you'd like
en_US.po
file:
# it's not enough for all errors, but for 400 bad request as a start
msgid "ClientError.error_message"
msgstr "Bad request."
# obviously error_message field is dynamic then you might need more messages
# uncomment more strings and specify IDs for them
# msgid "ClientError.error_message.2"
# msgstr ""
# msgid "ClientError.error_message.3"
# msgstr ""
ru_RU.po
will be identical but user fills in russian translations.
Describe alternatives you've considered
And even more crazy idea. It's really boring to document hundreds of errors in every project. What about multilingual API Error Codes Book file? I think that 99% of APIs use static codes for errors and most of time they are hardcoded. We can generate .po
file with error codes mapped to HTTP Status Codes
from official standard. We can add template for custom errors like msgid "error_555"
etc. If developer uses error code 555 he uncomments this line and adds appropriate message translation.
After that PHP user can use translations with gettext
extension like:
$language = "ru_RU";
putenv("LANG=".$language);
setlocale(LC_ALL, $language);
$domain = "error_messages";
bindtextdomain($domain, "locale");
textdomain($domain);
echo gettext("error_400");
/* prints Неверный запрос. */
cc special invite to @jimschubert