Skip to content

I18n for Errors #4

Open
Open

Description

@johannesschobel

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

Activity

  1. johannesschobel commented on Jan 31, 2017

    @johannesschobel
    Author

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

  2. tembra commented on Feb 6, 2017

    @tembra
    Owner

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions