shaggyz/laravel-gettext

This package is not compatible with nginx (including homestead) - strange behavior

Closed this issue · 15 comments

This package does not function properly on L5 + nginx.

Potential related issue: #20

I have created a test repo - a clean laravel 5.2 installation. See: https://github.com/canfiax/laravel-gettext-cache-issue - check the repo how to download and install the test app on your machine using homestead.

How to reproduce the error:

  • Navigate to http://test.app/. The content should be in english and say "Laravel 5"
  • Now click Danish below the header. You should now be navigated to http://test.app/da_DK and the header should say `Laravel 5 på dansk'. Hurray! Gettext seems to be working
  • Now refresh the page, using CMR+R on OSX or F5 on Windows. The string is back to english. What happend?
  • If you switch back to english, and then click danish again, the correct translation will show up.

Video that shows the error:

https://www.youtube.com/watch?v=KRYeDf6XIQU&feature=youtu.be

See this commit: https://github.com/canfiax/laravel-gettext-cache-issue/commit/d46e33621d046971422d2a9ccd63484e07c0bf78

A workaround is to call setLocale twice. Works:

    /**
     * The package need to be initialized, the locale will
     * be available after first method call. If you have
     * async calls in your project, this filter starts the 
     * locale environment before each request.
     */
    $old_locale = LaravelGettext::getLocale();

    LaravelGettext::setLocale(config('laravel-gettext.locale'));
    LaravelGettext::setLocale($old_locale);

Thank you for your mail @canfiax, I haven't made alot to this repo besides some fixes regarding a .gitkeep

I haven't had a similar issue on my end (using laravel 5.2.16).

We are setting it by a ServiceProvider, but i have been setting it in config earlier without any problems.
(using both laravel 5,1 and 5.2 on a homestead/vagrant setup on both php 5 and 7 without problems)

maybe try with some debugging where you set the locale, as it might be something with it being overwritten (don't ask me why or how).

thanks @KasperFranz. Would you believe there is anything wrong with the setup done in this repo? https://github.com/canfiax/laravel-gettext-cache-issue

I have set it up according to documentation, however, the faults caused by the package is obvious (seen from this video: https://www.youtube.com/watch?v=KRYeDf6XIQU). I have created a new branch in the project (workaround1: https://github.com/canfiax/laravel-gettext-cache-issue/tree/workaround1) that works (calling setLocale twice, first on a language that is different that the once you wish to set, then calling setLocale on the intended locale), but I still have no clue why this works.

Hi! It's a well known gettext issue, on some platform combinations (os + webserver + php) the gettext cache is very aggressive.
I think solution is #52 (basically not using the gettext extension), other workarounds may work in your current setup but not in all others.

There is no solution from php (trust me, I've tried all workarounds), gettext cache is an internal process without any interface.

Look this: #45 (comment) and this: http://stackoverflow.com/a/13629035

The task for this improvement is planned for next version of this package.
Help is always welcome.

@shaggyz
If it is you can make an issue with some tasks in, then i'm sure the community would come together and make some changes :)

@KasperFranz Sure! Tasks for this issue are here: #52, I'm gonna work on it, but the next week (I just need some free time :D). Thank you!

Ofc :)

I will look into it if I get time this week - Not 100% sure yet depends on what kind of progress I do on this project this week

Hm, interesting @shaggyz. Although the PHP package seems nice (no need to restart, etc), the issue still wonders me.

Because, although the gettext cache is aggressive, it doesn't look like this issue is cache related? As I've noted, the following code will fix the errors, and ensure language shows up as expected. I don't know performance wise though, how tough it is to set language 2 times:

    /**
     * The package need to be initialized, the locale will
     * be available after first method call. If you have
     * async calls in your project, this filter starts the 
     * locale environment before each request.
     */
    $old_locale = LaravelGettext::getLocale();

    LaravelGettext::setLocale(config('laravel-gettext.locale')); // calling setlocale on another language, than the current language
    LaravelGettext::setLocale($old_locale); // then setting it back to the "old" locale, i.e. the locale that is expected on the page. Now it works

did you see @shaggyz - I'm still not sure this is a gettext issue, but rather code-specific to this package.

@canfiax Yes, this will fix the issue in your current setup. And sure, it has a negative performance impact. Gettext was made with the aim to provide static locale files (desktop software basically), it's bad news for the web, where translation changes are more frequently. I'm still sure that #52 is the right way to fix it (I hope).
Thanks for the interest!

@shaggyz but why is this related to gettext? It seems to be working. I can get it working. Just with calling the same methods twice.

Does below lines clear any strange gettext cache?

$old_locale = LaravelGettext::getLocale();

LaravelGettext::setLocale(config('laravel-gettext.locale')); // calling setlocale on another language, than the current language
LaravelGettext::setLocale($old_locale); // then setting it back to the "old" locale, i.e. the locale that is expected on the page. Now it works

It's entirely related to gettext, as I said it's a well-known problem. Look the package code, when you call LaravelGettext::setLocale() you are calling the internal gettext function set_locale and nothing more relevant happens. Wait for #52

After getting this issue on a new production server I had to find a way to solve it :)

If the locale is not installed you have to install it first (you can check it by using locale -a)

This can be done with the command locale-gen da_DK.utf8 (utf8 version of danish)

when it is installed you have to restart php fpm and nginx if you have both installed.

(This also was a fix for a mac running homestead)

Working on nginx in 4.x, follow #52.