Forcing locale in Laravel Dusk

橙三吉。 提交于 2021-01-28 08:04:42

问题


How can we force the locale used by Dusk within our tests?

The browser is launched with FR locale set and thus my application is outputting French.

I would need to make all tests consistently, regardless of the locale set by the developer on his system when running tests.

In the middleware stack, I have a LocaleMiddleware responsible for picking a locale based on the request headers (sent automatically by the browser).

If possible, I would like to avoid having test-specific code within the application code (as suggested below, to force a locale when environment is 'testing')


回答1:


Use setLocale() method:

app()->setLocale('en');

Or:

App::setLocale('en');

You may also change the active language at runtime using the setLocale method

https://laravel.com/docs/5.5/localization#introduction

Update

You can also set locale in the middleware for testing environment:

if (env('APP_ENV') === 'testing') {
    app()->setLocale('en');

    return $next($request);
}


来源:https://stackoverflow.com/questions/46835334/forcing-locale-in-laravel-dusk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!