Build : 396
Translate Pl : 1.2.9
When I run an Ajax handler and update a page partial the text is not translated instead shown in the default language ( EN)
When I first load the page there's not a problem - Translation is shown as per the User's locale
User plugin:
public function boot()
{
App::before(function() {
if (App::runningInBackend()) {
return;
}
if (!Auth::check()) {
return ;
}
if($user = Auth::getUser()){
$locale = trim($user->lang_code);
//set the app to locale
App::setLocale($locale);
//Set translator locale
$translator = Translator::instance();
$translator->setLocale($locale);
}
});
}
Then in my support Plugin
Default.htm
<div class="col-sm-12 col-lg-8" id="tickets-wrapper">
{% partial __SELF__ ~ "::ticketslist" tickets=tickets %}
</div>
ticketslist.htm
{% for ticket in tickets %}
<a href="{{ticket.url}}">
<div class="info-section">
<p> {{ 'Department'|_ }} : {{ ticket.department.name }} </p>
<p> {{ 'Last Updated'|_ }} : {{ ticket.updated_at.diffForHumans }} </p>
<p> {{ 'Assigned to'|_ }} : {{ ticket.admin.full_name }} </p>
</div>
</a>
{% endfor %}
and I am Using the JS API
$.request('onFilterTickets',
{
data: {status: btns.filter(':checked').val()},
update: {'Tickets::ticketslist': '#tickets-wrapper'},
error: function(jqXHR, textStatus, error){
$.showAjaxError(jqXHR);
return false;
}
});
Am I missing something ?
I found a dirty solution ;
In the Message Model : RainLab\Translate\Models\Message
public static function get($messageId)
{
if (!self::$locale) { // self::$locale -> null
return $messageId;
}
.......
}
For normal requests the self::$locale returns the value set in my Middleware but during an Ajax call it is null , so the original $messageId is returned not the translated version..
a quick fix was adding Message::$locale = $locale in the Middleware ;
class LocalizationManager
{
public function handle($request, Closure $next)
{
App::before(function () {
if ( App::runningInBackend() || App::runningInConsole()) {
return;
}
$translator = Translator::instance();
if (!$translator->isConfigured())
return;
if ( $user = Auth::getUser() ){
$locale = trim($user->lang_code);
$translator->setLocale($locale);
Message::$locale = $locale; // added this and now it works
return;
}
});
return $next($request);
}
}
What do you think ?
I have same problem!
+1 same here.
+1 same here
+1 same here
+1 same here
Same problem, I posted it here before
https://github.com/rainlab/translate-plugin/issues/204#issuecomment-281353673
@tombien @TarasZakus created a PR with a potential fix with #225. I'll merge it as soon as I can find some time to test it. If you would like to test it to see if it resolves your issue and then let me know, that would be great as well.
@LukeTowers yes this fix it. Now it works for me, thank you.
I can confirm that PR #225 fixes this issue. I've tested this fix on several projects.
Yeah, it worked for me!
Most helpful comment
@tombien @TarasZakus created a PR with a potential fix with #225. I'll merge it as soon as I can find some time to test it. If you would like to test it to see if it resolves your issue and then let me know, that would be great as well.