Giter Site home page Giter Site logo

stevegrunwell / lost-in-translation Goto Github PK

View Code? Open in Web Editor NEW
36.0 36.0 8.0 101 KB

Uncover missing translations and localization strings in Laravel applications.

Home Page: https://stevegrunwell.com/blog/find-missing-laravel-translations/

License: MIT License

PHP 100.00%
laravel laravel-package localization translation

lost-in-translation's Introduction

👋 Hello, there!

I'm Steve, and I love building tools to help people build better software.

By day, I'm a Staff Software Engineer at Mailchimp, improving the way we handle audience information and our general involvement in the PHP ecosystem.

PHP is my bread-and-butter, and I love building out SaaS and other products using Laravel. I'm also a WordPress plugin author and core contributor, but my real passion is building code quality tools; it shouldn't come as any surprise that I've spent a lot of time working with CI/CD pipelines and all of the tooling that gets run as part of them.

I'm also a regular conference speaker (or at least was, pre-pandemic), so you may have met me at any number of PHP, WordPress, or other software-related events.

You can learn more about me over at stevegrunwell.com, follow me on Mastodon, or connect with me on LinkedIn.

lost-in-translation's People

Contributors

plinioalmeidafde avatar stevegrunwell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

lost-in-translation's Issues

Upgrade to Laravel 5.6

Hi,

Thanks for the great package! I've used it with much pleasure in a couple of projects.

However, when upgrading my projects to Laravel 5.6 I noticed that this package doens't play nice with it due to the new way logging works with Laravel (https://laravel.com/docs/5.6/logging).
I got the following error every time a missing translation was found: Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Call to undefined method Monolog\Logger::useFiles()".

After some debugging; I found out a way to fix this.
The new logging method (using Monolog) doesn't allow for changing the file "on the fly" anymore. This is not the intended use of the new logging system. Therefore, the issue originates in the Translator.php file on line 71 where the logging file location is changed (https://github.com/stevegrunwell/lost-in-translation/blob/develop/src/Translator.php#L71).

This could be fixed by changing the logMissingTranslation method to:

    protected function logMissingTranslation($key, $replace, $locale, $fallback)
    {
        if (! $this->logger) {
            $this->logger = logger()->channel('lost-in-translation')->getLogger();
        }

        $this->logger->notice('Missing translation: ' . $key, [
            'replacements' => $replace,
            'locale' => $locale ? $locale : config('app.locale'),
            'fallback' => $fallback ? config('app.fallback_locale') : '',
        ]);
    }

and adding the following to the channels array of the config/logging.php configuration file:

        'lost-in-translation' => [
            'driver' => 'single',
            'path' => storage_path('logs/lost-in-translation.log'),
            'level' => 'debug',
        ],

I started making a PR for this. When creating this PR (and modifying the README) I noticed that some of the functionality of this package can (and should?) now be handled by the new Laravel Logging.
The MissingTranslationException can be added to a stack channel in the channels array of theconfig/logging.php configuration file. Furthermore, extending the logging can be make use of the new system as well. The whole Event based approach can be replaced by making use of custom Monolog Handler Channels (https://laravel.com/docs/5.6/logging#creating-monolog-handler-channels).

So I started making a small PR but quickly noticed that the whole package could be refactored, benefiting from the new logging system.
With such a big change it could be very useful to discuss it first before making the changes. So I'ld like to hear what you think.

Only php array translations

When I have only php array translations (and no json translations) the exception is thrown because the loaded[][] is empty

Handle Rules

When I use a custom rule, Laravel uses the correct translation, but lost-in-translation logs this:
[2020-05-29 18:05:47] local.NOTICE: Missing translation: validation.values.from_date.10-10-2020 {"replacements":[],"locale":"nl","fallback":"en"}

When translation is same as the string to be translated, it is logged as missing

Ea. Project, Week, Dashboard, Percentage. These 4 words are the same in English and in Dutch. This makes the logfile filled with missing translations that aren't missing:
[2020-05-29 13:11:32] local.NOTICE: Missing translation: Project {"replacements":[],"locale":"nl","fallback":"en"}
[2020-05-29 13:13:35] local.NOTICE: Missing translation: Week {"replacements":[],"locale":"nl","fallback":"en"}
[2020-05-29 13:13:35] local.NOTICE: Missing translation: Dashboard {"replacements":[],"locale":"nl","fallback":"en"}
[2020-05-29 13:13:35] local.NOTICE: Missing translation: Percentage {"replacements":[],"locale":"nl","fallback":"en"}

Laravel value translations

Laravel allows for custom user input translations in FormatsMessages->getDisplayableValue()
This wants to translate : validation.values.{$attribute}.{$value}
and results in something like 'validation.values.iban.12321412412412421"
This is ofcourse not translated and then throws an exception

Error using TRANS_ERROR_ON_MISSING with phpunit

1) Tests\Unit\CustomValidationTest::validate_user_status_cannot_be_updated_from_locked_to_active
LostInTranslation\Exceptions\MissingTranslationException: Could not find translation for "validation.attributes".

F:\lamp\proj\THE_APP\vendor\stevegrunwell\lost-in-translation\src\Translator.php:49
F:\lamp\proj\THE_APP\vendor\laravel\framework\src\Illuminate\Translation\Translator.php:101
F:\lamp\proj\THE_APP\vendor\laravel\framework\src\Illuminate\Validation\Concerns\FormatsMessages.php:251
F:\lamp\proj\THE_APP\vendor\laravel\framework\src\Illuminate\Validation\Concerns\FormatsMessages.php:228
F:\lamp\proj\THE_APP\vendor\laravel\framework\src\Illuminate\Validation\Concerns\FormatsMessages.php:190
F:\lamp\proj\THE_APP\vendor\laravel\framework\src\Illuminate\Validation\Validator.php:578
F:\lamp\proj\THE_APP\vendor\laravel\framework\src\Illuminate\Validation\Validator.php:360
F:\lamp\proj\THE_APP\vendor\laravel\framework\src\Illuminate\Validation\Validator.php:268
F:\lamp\proj\THE_APP\vendor\laravel\framework\src\Illuminate\Validation\Validator.php:293
F:\lamp\proj\THE_APP\tests\Unit\CustomValidationTest.php:32

[en,fr]/validation.php

[
  'attributes' => [],
]

phpunit.xml

<php>
        <env name="TRANS_ERROR_ON_MISSING" value="true"/>
</php>

Can't test it, but i think changing this line: Translator to something like this: if ($translation === $key && !in_array($key, ['validation.attributes'])) {... should fix the problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.