Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID Sign In with Twitter

Categories

In this Discussion

Who's Online 9

CurtisOdenmimi59zachs +6 guests

[Solved] Language Switcher Plugin for Vanilla 2?

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

  • Posts: 1,586
    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.
    }

    Vanilla co-founder

  • Posts: 9
    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']);
    }
    }
  • Posts: 9
    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
    Posts: 1,573
    Sounds cool... we'll have to take a look at making this less backwards

    Vanilla Forums Senior Developer [GitHub, Twitter, About.me]

  • Posts: 9
    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"?
  • Posts: 18
    有谁知道我写的文字是哪国家的?
  • Posts: 14
    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
  • Posts: 14
    OK NVM it was a problem with the machine that has been solved today. The code works.
  • lp_lp_
    Posts: 3
    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.
  • lp_lp_
    Posts: 3
    Got it!

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

    :)
Sign In or Register to comment.