Jhin Posted October 6, 2023 Share Posted October 6, 2023 Hello all, I'm working on a website very focused on members and what I'm trying to do is to retrieve member desired language ("en", "it", "fr", "es", etc...) and then use this info to render correct language. language_01 is a page selector field which is linked to a $language object. I'm trying doing a redirect, but I'm really feeling frustrate regarding this feature. I take too long to understand that $user->language was based for backend and frontend is handeld via localName urls. $desired_language = $user->language_01->name; if (!isset($_SESSION['redirected']) OR $page->localName != $desired_language) { $_SESSION['redirected'] = true; $redirect_url = '/' . $desired_language . '/'; / header("Location: $redirect_url"); exit; } Do you have any other approach? I saw a lot of snippets but required the member input and I was looking to make it in a more automatic way. Link to comment Share on other sites More sharing options...
da² Posted October 6, 2023 Share Posted October 6, 2023 Hello, Did you read at least the first 3 sections here ? https://processwire.com/docs/multi-language-support/ You'll find different ways of managing languages. Link to comment Share on other sites More sharing options...
Jhin Posted October 9, 2023 Author Share Posted October 9, 2023 Hello, Indeed, I already readed it. But something I think is not working properly. Docs mentions user's language will define which language will be served. I, as a logged member, always receive the default language even I specify 'it', 'de' or 'ru'. Quote In order to present your site's content in a different language using language fields, the current user's language has to be set to the one you want the site to display in. This is a simple matter if you are dealing with users that can log-in, because they can set their language from their profile. However, we are assuming you want to use language fields for the purpose of having multi-language versions of your site available to anonymous visitors. https://processwire.com/docs/multi-language-support/multi-language-fields/ I have no way to force processwire to show the current user language option, it always show the 'default' language. I saw a lot of people looking how to prevent that, but I cannot find a working solution at the moment. ----------------------------------------------------------------- Solved, I figure it out looking at different forum snippets. This is the code I'm using on a new module. It also adds the $user->savedLanguage variable to receive the language name user has set on profile. I hope you find it useful. <?php class LanguageDefault extends WireData implements Module { /** * getModuleInfo is a module required by all modules to tell ProcessWire about them * * @return array * */ public static function getModuleInfo() { return array( 'title' => 'LanguageDefault', 'version' => 0, 'summary' => 'A work around to changing the default language.', 'singular' => true, 'autoload' => true, ); } /** * Initialize the module * * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called * when ProcessWire's API is ready. As a result, this is a good place to attach hooks. * */ public function init() { if ($this->wire("user")->language->name == "default") { $this->wire("user")->savedLanguage = "en"; } else { $this->wire("user")->savedLanguage = $this->wire("user")->language->name; } $this->session->addHookBefore('redirect', $this, 'setDefaultLanguage'); } public function setDefaultLanguage($event) { if ($this->page->id == 1 && $event->arguments(0) == $this->page->localUrl('default')) { $event->arguments(0, $this->page->localUrl($this->wire("user")->savedLanguage)); } } } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now