HannaP Posted November 1, 2015 Share Posted November 1, 2015 Hello, I'm trying to setup a multi-language site in German|English|Polish ( German being the default). In _main.php template that comes with the installation I've found this line: <html lang="<?php echo _x('en', 'HTML language code'); ?>"> I don't quite understand what this line should output. Can somebody help, please? Link to comment Share on other sites More sharing options...
Can Posted November 1, 2015 Share Posted November 1, 2015 Hola Hanna, actually I dont't know _x() probably this function but __("string to translate") (two underscores) translates strings in template files, or better to say let's you translate via /processwire/setup/languages/ So in your case you would either change the line from <html lang="<?php echo _x('en', 'HTML language code'); ?>"> to <html lang="<?php echo _x('de', 'HTML language code'); ?>"> or you in admin you go to /setup/languages/ select your default (german) language and click on_main.php to translate, somehwere there should be written "HTML language code" and there you enter "de" I used a different approach in _init.php I defined the following // define var $lang for use in tpl files if($user->language->name == 'default') $lang = 'de'; else $lang = $user->language->name; then you can just put this in _main.php <html lang="<?= $lang; ?>"> and of course re-use wherever you like. Or, you could use the languages title field or add another custom field to language template and name it for example "language_short_code" or maybe you got an already created textfiel which you could re-use here. then you don't need to override the default languages name but just output your shortcode field like <html lang="<?= $user->language->language_short_code; ?>"> Hope it helps, enjoy Processwire 4 Link to comment Share on other sites More sharing options...
SiNNuT Posted November 1, 2015 Share Posted November 1, 2015 Can is mostly right. _x is one of the tags that indicates a translatable string, which you can then translate via PW admin. The _x means that it is a context sensitive string. Read all about it here http://processwire.com/api/multi-language-support/code-i18n/#context It should output the language code for the current user language. If you're doing multilang sites it's wise to study all of the docs linked above. 2 Link to comment Share on other sites More sharing options...
ukyo Posted November 1, 2015 Share Posted November 1, 2015 Check example codes on profiles come with ProcessWire. Here is an example about getting language code from homepage url : https://github.com/ryancramerdesign/ProcessWire/blob/dev/site-languages/templates/_main.php#L53 Link to comment Share on other sites More sharing options...
Can Posted November 2, 2015 Share Posted November 2, 2015 Check example codes on profiles come with ProcessWire. Here is an example about getting language code from homepage url : https://github.com/ryancramerdesign/ProcessWire/blob/dev/site-languages/templates/_main.php#L53 But the name of the default language is "default" so she won't get "de", or am I missing something here? Ah thanks SiNNuT, I remember Link to comment Share on other sites More sharing options...
tpr Posted November 2, 2015 Share Posted November 2, 2015 What I usually do is adding extra fields to system language pages like language code, locale, etc on multilang sites. This way there is no ambiguity on such things. 1 Link to comment Share on other sites More sharing options...
HannaP Posted November 2, 2015 Author Share Posted November 2, 2015 Thanks for pointing me to the Translator. That definitively answers my question. I like the idea of @Can to use _init.php for language strings. I'll try that out. 1 Link to comment Share on other sites More sharing options...
neosin Posted March 19, 2018 Share Posted March 19, 2018 Perhaps there is a better way but I couldn't find anything on how to get the default language slug so i added this to _func.php function userLang($lang){ switch($lang){ case 'default': $lang = 'en'; break; case 'fr': $lang = 'fr'; break; default: $lang = 'en'; } return $lang; } just switch 'en' for whatever your default is and change or add the others like 'fr'. and in my templates I have $lang = userLang($user->language->name); for my variable names I use fieldname_lang, so this way I can do $page->${"fieldname_".$lang} for example if you have image fields such as post_image_en and post_image_fr this way you can show the image that corresponds to the visitors chosen language Link to comment Share on other sites More sharing options...
owzim Posted November 15, 2018 Share Posted November 15, 2018 This is how I've done it, hooks to the rescue. Clean. $this->addHookProperty('Language::code', function(HookEvent $event) { $lang = $event->object; $event->return = $lang->name === 'default' ? 'en' : $lang->name; }); So now you can access it as a property of languages. $user->language->code; 5 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