Please upgrade here. These earlier versions are no longer being updated and have security issues.

[Solved] Language Switcher Plugin for Vanilla 2?

edited August 2011 in Vanilla 2.0 - 2.8
I want to have a forum with multiple languages, which I plan to separate using categories. That's no problem. But I need a way to allow users to change the language of the forum interface. I have this functionality there by adding the following code to /conf/config.php.

if ($_GET['locale'] == 'de') { setcookie('vanilla_locale', 'de-DE', time()+2592000); $Configuration['Garden']['Locale'] = 'de-DE'; } elseif ($_GET['locale'] == 'en') { setcookie('vanilla_locale', 'en-CA', time()+2592000); $Configuration['Garden']['Locale'] = 'en-CA'; } elseif (isset($_COOKIE['vanilla_locale'])) { $Configuration['Garden']['Locale'] = $_COOKIE['vanilla_locale']; }

But that's a rather bad solution because it always gets overwritten when changing some settings. (And of course, you still have to add links in the theme for changing the language and the code could still be improved by detecting the language from the users browser for example.)

Is there some way to add a language switcher as a plugin?
Tagged:

Comments

  • ToddTodd Chief Product Officer Vanilla Staff
    edited July 2010
    You can get a reference to the config object using:
    $Config = Gdn::Factory(Gdn::AliasConfig);
    You can then set the current locale in the config:
    $Config->Set('Garden.Locale', 'de-DE');
    Unfortunately, that value will get saved to file on the next save so I'll have to put support for not setting it into the object. For now you should be fine though.

    In order to put this in a plugin you would want to set the locale rather early. Try using the following method in your plugin:
    public function Dispatcher_BeforeDispatch_Handler($Sender) {
    // Set locale here.
    }
  • edited July 2010
    After changing the function name a little bit I can at least get the Config object and change the language, but all that doesn't have any effect on my forum. The system messages always stay English.

    Here's my code:
    public function Gdn_Dispatcher_BeforeDispatch_Handler($Sender) { $Config = Gdn::Factory(Gdn::AliasConfig); if ($_GET['locale'] == 'de') { setcookie('vanilla_locale', 'de-DE', time()+2592000); $Config->Set('Garden.Locale', 'de-DE'); } elseif ($_GET['locale'] == 'en') { setcookie('vanilla_locale', 'en-CA', time()+2592000); $Config->Set('Garden.Locale', 'en-CA'); } elseif (isset($_COOKIE['vanilla_locale'])) { $Config->Set('Garden.Locale', $_COOKIE['vanilla_locale']); } }
  • edited August 2010
    I found a way to make it work. Instead of changing the Config object, I recreate the Locale object. So here's my code that works fine:

    public function Gdn_Dispatcher_BeforeDispatch_Handler($Sender) { if ($_GET['locale'] == 'de') { $CurrentLocale = 'de-DE'; } elseif ($_GET['locale'] == 'en') { $CurrentLocale = 'en-CA'; } elseif (isset($_COOKIE['vanilla_locale'])) { $CurrentLocale = $_COOKIE['vanilla_locale']; } else { $CurrentLocale = 'en-CA'; } $Path = Gdn::Config('Garden.Cookie.Path', '/'); $Domain = Gdn::Config('Garden.Cookie.Domain', ''); setcookie('vanilla_locale', $CurrentLocale, time()+2592000, $Path, $Domain); //$Config = Gdn::Factory(Gdn::AliasConfig); //$Config->Set('Garden.Locale', $locale); require_once(PATH_LIBRARY_CORE.DS.'class.locale.php'); $Codeset = Gdn::Config('Garden.LocaleCodeset', 'UTF8'); $SetLocale = str_replace('-', '_', $CurrentLocale).'.'.$Codeset; setlocale(LC_ALL, $SetLocale); $Gdn_Locale = new Gdn_Locale($CurrentLocale, Gdn::Config('EnabledApplications'), Gdn::Config('EnabledPlugins')); Gdn::FactoryInstall(Gdn::AliasLocale, 'Gdn_Locale', PATH_LIBRARY_CORE.DS.'class.locale.php', Gdn::FactorySingleton, $Gdn_Locale); }

    When I have the time, I'll make a plugin with sidebar module and upload it.
  • TimTim Operations Vanilla Staff
    Sounds cool... we'll have to take a look at making this less backwards

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Is there a way to display a module on every page of the forum? I couldn't find any event that is suitable for this.

    And I need a list of the installed languages as well for allowing the user to select his language. As far as I can see, there's no way to get the human-readable language names. But is it at least possible to get the language codes such as "en-CA"?
  • 有谁知道我写的文字是哪国家的?
  • this public function Gdn_Dispatcher_BeforeDispatch_Handler

    when put in a plugin class it does not run at all, not in the backend, not in the frontend
  • OK NVM it was a problem with the machine that has been solved today. The code works.
  • Hi all. Anyone know how to make this work again in version 2.0.18b2? For some reason the language is not changing anymore after updating from 2.0.17.10. I'm using a custom made plugin that is based on _jan_'s code above.
  • Got it!

    Gdn::Locale()->Set("en-CA", Gdn::ApplicationManager()->EnabledApplicationFolders(), Gdn::PluginManager()->EnabledPluginFolders(), TRUE);

    :)
    egfriends
  • Please, can you explain more detailed (files to edit and code) how to do?
    I want a locale switcher.
    Thanks very much!

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited February 2013

    Try this it is easier and it can translate in 52 languages, this way users select the language they want.You would put this in the default.master.php file at the foot after foot div

    http://translateth.is

  • KasperKasper Scholar of the Bits Copenhagen Vanilla Staff

    Or, simply use the Multilingual plugin which will put a locale selector in your site's footer: https://github.com/vanillaforums/Addons/tree/master/plugins/Multilingual

    Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub

    vrijvlinder
Sign In or Register to comment.