Jump to content

Language help...


drilonb
 Share

Recommended Posts

Hello i am making a web with 2 different language and i am using like this code from Ryan,

<?php $languages = array('en', 'al'); 
if(in_array($input->urlSegment1, $languages)) {
   $session->language = $input->urlSegment1; 
}
if($session->language) {
       $body = $page->get("body_" . $session->language);
$headline = $page->get("headline_" . $session->language); 
$sidebar = $page->get("sidebar_" . $session->language);
} else {
       $body = $page->body_en;
$headline = $page->headline_en;
$sidebar = $page->sidebar_en;
}

its possible to make changes and from children pages

<?php 

			// Output navigation for any children below the bodycopy.
			// This navigation cycles through the page's children and prints
			// a link and summary: 

			if($page->numChildren) {

				echo "<ul class='nav'>";

				foreach($page->children as $child) {
					echo "<li><p><a href='{$child->url}'>{$child->title}</a><br /><span class='summary'>{[b]$body[/b]}</span></p></li>"; 
				}

				echo "</ul>";

to change and other language ? when i switch from AL to EN

$body = $page->get("body_" . $session->language);

  this is for page  , if any possibility for child.

.

Link to comment
Share on other sites

<?php
   $lang = $session->language; //or any other way you save your language preferences

			if($page->numChildren) {

				echo "<ul class='nav'>";

				foreach($page->children as $child) {
					echo "<li><p><a href='{$child->url}'>".$child->{'title_'.$language}."</a><br /><span class='summary'>".$page->{'body_'.$language}."</span></p></li>"; 
				}

				echo "</ul>";

Or, you could also do this:

<?php
  foreach(..){
    $t = $child->{'title_'.$language};
    $b = $child->{'body_'.$language};
    echo "...$b...$t";
  }
Link to comment
Share on other sites

This is really amazing perfect lovely now process wire for me is full 1.2.3.4.5 language support,

thanks Adamkiss for this fast support

i make like this and work perfect

<?php 

			// Output navigation for any children below the bodycopy.
			// This navigation cycles through the page's children and prints
			// a link and summary: 
			$languages = $session->language;
			if($page->numChildren)

			{
				echo "<ul class='nav'>";
				foreach($page->children as $child) {
					echo "<li><p><a href='{$child->url}'>".$child->{'headline_'.$session->language}."</a><br /><span class='summary'>".$child->{'body_'.$session->language}."</span></p></li>"; 
				}

				echo "</ul>";
			}

and language settings is

<?php 

 $languages = array('en', 'al'); 
// check if the current page's URL ends with a language
if(in_array($input->urlSegment1, $languages)) {
        $session->language = $input->urlSegment1; 
}
if($session->language) {
        $body = $page->get("body_" . $session->language);
$headline = $page->get("headline_" . $session->language);
$sidebar = $page->get("sidebar_" . $session->language);
} else {
        $body = $page->body_en;
$headline = $page->headline_en;
$sidebar = $page->sidebar_en;
} 
Link to comment
Share on other sites

  • 2 months later...

I think the code could be made simpler by using...

<?php

$languages = array('en', 'al');

if(in_array($input->urlSegment1, $languages)) {
$session->language = $input->urlSegment1;
}
else {
$session->language = 'en';
}

$body = $page->get("body_" . $session->language);
$headline = $page->get("headline_" . $session->language);
$sidebar = $page->get("sidebar_" . $session->language);

...so you don't have to duplicate the lines with required fields for the default language.

Link to comment
Share on other sites

  • 3 weeks later...

Just an idea from the lazy man for future ProcessWire releases. I am just wandering, hoping it will be possible.

In addition to the common scenario used in the current topic.

1) Lets asume we already have set a global variable like $session->language or $config->lang (like me) with the current language stored in a url segment, subdomain or etc.

2) Will be great if $page->get('body') returns the value of field {"body_" . $config->lang} if the field "body" doesnt exist in the current page template. I am not sure if this will lower the site performance but the code in the templates will be cleaner. Upgrading the site from one to more languages will be easier too.

3) Similar.. will be great if $image->description returns {'description_' . $config->lang} if {description} field doesn't exist. I have made a modifications to existing "FieldtypeFile" module with multiple "description" fields for each defined language. If the FieldtypeFile or FieldtypeImage are used in a multilanguage site there is a must to have separate description fields even the fields are used as shared assets or are attached to one page with all fields for all languages.

Probably the first issue that have to be solved is the common scenario for storing the current language and an array with the site languages. So each new multi-language module will use them as system variables otherwise each module will be a custom solution.

Link to comment
Share on other sites

That's a good idea and I don't think there would be any worries about performance or overhead. But since there may be different approaches for multi language support, I'd probably want to delegate it to a module rather than make translation assumptions with field names. So we could add a new hook to the Page class that is called when an unknown property is accessed (a hook called when the return value is going to be null). Then a module could hook into it to see if it can translate it to another field. Using your example, the hook would get called when the unknown field "body" is accessed. The hook would check to see if the page has a "body_$lang" field (where $lang might be "en or "fr", etc), and return it if it does.  Of course, that sort of thing could very easily become and endless recursion loop, so PW would prevent that.

Link to comment
Share on other sites

Thank you, Ryan. It all makes sense but I dont know how it will be implemented. A little more help will be much appreciated but if your really pushed don't bother.

Lets asume I have added a new hook to the Page class and created the necessary code for null check in getFieldValue;

$this->addHookAfter('Page::getValue', $this, 'getFieldValue');

This way in the templates I should change the $page->body and $page->get('body') to $page->getValue('body'), right?

Is there a way to keep the $page->body, $page->get('body') syntax in the templates?

Link to comment
Share on other sites

getFieldValue isn't hookable and won't be. It's used internally by the Page class, and not part of the external API. There is a little overhead with every hooked function, and getFieldValue is one that sees so much activity that making it hookable would potentially reduce performance. What I'll do instead is add a new hook that only gets called when $page->get(fieldName) or $page->fieldName is returning a null. That will enable us to achieve the same thing without adding much, if any overhead.

We'll likely call the hook 'getUnknownFieldValue'. When a module hooks into that function, it'll get the opportunity to supply a value for a $page->fieldName call that would have otherwise returned null.  For instance, if your page didn't have a 'body' field, and you called $page->body, it would return null. But in your case, you want it to potentially translate 'body' to 'body_fr' or 'body_en' or something like that, so that's where this hook would come into play. You could have a call for $page->body return the contents of $page->body_en rather than null.

Also want to note that there isn't currently a hook like this. It's something that I need to add, but it will be easy to do.

Is there a way to keep the $page->body, $page->get('body') syntax in the templates?

Of course– There won't be any change in syntax.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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