Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
[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.
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?
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?
1
Comments
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:
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 COO [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);
Please, can you explain more detailed (files to edit and code) how to do?
I want a locale switcher.
Thanks very much!
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
❌ ✊ ♥. ¸. ••. ¸♥¸. ••. ¸♥ ✊ ❌
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