Jump to content

Recommended Posts

Posted

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?

Posted

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 ;)
  • Like 4
Posted

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.

  • Like 2
Posted

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.

  • Like 1
Posted

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.

  • Like 1
  • 2 years later...
Posted

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 

  • 7 months later...
Posted

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;
  • Like 5

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...