tractorcow-farm / silverstripe-fluent

Multi-language translate module for Silverstripe, without having to manage separate site trees.
BSD 3-Clause "New" or "Revised" License
93 stars 111 forks source link

How to access CurrentLocale in CustomPageController? #715

Closed jrson83 closed 3 years ago

jrson83 commented 3 years ago

Hey, the extension is working fine, but I can't figure out how to access the variable CurrentLocale inside my CustomPageController.

I found out I can use in template:

<% if $CurrentLocale == 'de_DE' %>Mehr erfahren<% else %>Learn more<% end_if %>

But if I try the variable $CurrentLocale in my CustomPageController it returns nothing.

I have a CustomForm inside my PageController, for which I could easy translate the output if I could access the variable.

tractorcow commented 3 years ago

Hiya, you can get the current locale like this in PHP.

$locale = FluentState::singleton()->getLocale();

Make sure to import FluentState.

use TractorCow\Fluent\State\FluentState;