tembra / jsonapi-errors-php

A PHP implementation of JSON API Errors compliant format.
BSD 3-Clause "New" or "Revised" License
6 stars 0 forks source link

I18n for Errors #4

Open johannesschobel opened 8 years ago

johannesschobel commented 8 years ago

Hey @tembra , I just wanted to drop a line and ask, if it is possible to add an "internationalization" (i18n) feature for your plugin.

I really like it and use it in my current project. However, I need to output exceptions in different languages (depending on the user's Accept-Language).

Can you give me a feedback on this topic? Cheers from Germany

johannesschobel commented 7 years ago

Hey @tembra , I just wanted to ask, if there are any updates on this issue!?

tembra commented 7 years ago

@johannesschobel sorry for late answer. I'm currently finishing my application for production and for now the multi-language support isn't a need. But once I got some free time I'll make this improvement.

I like your idea of use Accept-Language header, but this can't be the only way. What I'm planning for this package is to have a new index named languages inside value of $errors associative array, that you can set on your Application Error Code class. This member will be an associative array with language code (e.g. en or pt_BR) as index and another array as value that will be an associative array too with title and detail members as index and the translated message as value.

Something like this:

<?php

use Mytdt\JsonApi\Errors\MyJsonApiErrors;

class AppErrorCodes extends MyJsonApiErrors
{
    const RESOURCE_NOT_FOUND = 1;

    protected static $errors = [
        self::RESOURCE_NOT_FOUND => [
            'title' => 'Resource not found',
            'detail' => 'The requested resource was not found'
            'languages' => [
                'pt_BR' => [
                    'title' => 'Recurso não encontrado',
                    'detail' => 'O recurso solicitado não foi encontrado.'
                ]
            ],
    ];
}

Then when return error object all we need to do is verify which language to use and get properly title and detail members, from root or from languages array.

If you feel comfort to implement something like this or have another vision/way to do so, you're welcome to contribute to this package and send a PR.