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?
Comments
$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
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']);
}
}
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.
Vanilla Forums Senior Developer [GitHub, Twitter, About.me]
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"?
when put in a plugin class it does not run at all, not in the backend, not in the frontend
Gdn::Locale()->Set("en-CA", Gdn::ApplicationManager()->EnabledApplicationFolders(), Gdn::PluginManager()->EnabledPluginFolders(), TRUE);
:)