Jump to content

Replacing default language or just using the Pages Name module?


landitus
 Share

Recommended Posts

I have a very unique user case. I have a multilingual site in spanish and portuguese. I managed navigation with the wonderful "Generate and parse localized URL" module. I need know to completely remove the default language (spanish) and instead leave portuguese as the default. This poses multiple challenges

I've changed the default language for the guest to portuguese. All the info I need is on the portuguese fields, but I don't mind having the default fields in the admin (for now).

I need the urls to be "site/portuguese-title. So no "/pt" or any prefix. I've tried to solve this changing the default language in the URLs plugin but it gets confused somehow... so I've removed the "Generate and parse localized URL" module to try something else. 

I've activated the new "Languages Support - Page Names" module. Maybe I can change all page names and be done with it. I almost worked!! but now I have spanish and portuguese URLs working at the same time. So I only have to disable the default url and presto! But... I've downloaded the latest dev branch and replaced the "wire/modules/LanguageSupport" folder but I still can't see any checkboxes. 

I have 2 questions after all this

  1. I am doing this the right way? The best way would be to make the portuguese fields the default language and have only one language. Otherwise, I would have to re-enter the info on all pages (+200)... I guess that If I disable the default URL (with the checkboxes) this would be solved.
  2. Is there a way to set the "Generate and parse localized URL" module to work with a non-default language (portuguese) and not use any prefix?

Thanks a lot!

post-72-0-50126800-1366501003_thumb.png

Link to comment
Share on other sites

So, now the site will be only in portuguese? You don't need any of the spanish information? I don't know if it's possible to do it as you are thinking, but it wouldn't be too difficult to make a script that runs through all the pages and copy the content from the portuguese fields to the default fields.

foreach($fields as $field){
    $page->setOutputFormatting(false);
    $user->language = $languages->get("portuguese"); 
    $pt_field = $field;
    $user->language = $languages->get("default");
    $field = $pt_field;
    $field->save();
}
This is only to show the concept, and i don't have any idea if it works...
Link to comment
Share on other sites

Diogo, that thought crossed my mind but have no idea how to accomplish it! It would be nice because I only need the portuguese fields.  The site will be only in portuguese :) I'll give it a try!!!

Link to comment
Share on other sites

I've tried this with only one page, to simplify it and it doesn't change anything. I've never used the fields api so maybe I'm doing something wrong...

$page = $pages->get('/contacto');

echo "<ul>";
foreach ($page->fields as $field) {

	echo "<li>";
	$page->setOutputFormatting(false);
	$user->language = $languages->get("pt"); 
	$pt_field = $field;
	echo $pt_field . "<br />";
	$user->language = $languages->get("default");
	$field = $pt_field;
	echo $field;
	$field->save();
	echo "</li>";
}
echo "</ul>";
Link to comment
Share on other sites

Good point Soma. I've done the following, still no luck!

$item = $pages->get('/contacto');
echo "<ul>";
foreach ($item->fields as $field) {

	echo "<li>";

	$item->setOutputFormatting(false);
	$user->language = $languages->get("pt"); 
	$pt_field = $item->$field;
	echo "Pt: ".$pt_field. "<br />";

	$user->language = $languages->get("default");
	echo "Sp: ".$item->$field. "<br />";
	$item->$field = $pt_field;
	
	$item->save();
	echo "Result: ".$item->$field;
	echo "</li>";
}
echo "</ul>";

The result is still the same:

Pt: Contato
Sp: Contacto
Result: Contacto

Link to comment
Share on other sites

I think what's wrong is that you get the field object and not a value. $pt_field = $item->$field; and $item->$field = $pt_field; If you would output the $item->$field; you'll still see the old value I guess.

If you would stringify by either add "$item->$field" or (string) $item->$field it would work.

Also you're cycling each field on page I think isn't good idea. You should restrict to those fields that you want to change.

I would do this instead, with an array you can define what fields, and also use the $field->getLanguageValue(langID) to get values.

// define fields you want to change
$fieldsArr = array('title','body');

$langAlt = $languages->get("de");
$langDefault = $languages->get("default");

$pa = $pages->find('/about/');

foreach($pa as $p){
    $p->of(false);
    foreach($fieldsArr as $field){
        $title = $p->$field->getLanguageValue($langAlt); // get the alternative lang value
        $p->$field->setLanguageValue($langDefault, $title); // set the new value
    }
    $p->save();
}
  • Like 1
Link to comment
Share on other sites

Yep, with Soma's solution , maybe the mot practical would be to do one template at a time. You could go to that template and check what are the fields that you want to change. And then do it for all the pages of that template:

$pa = $pages->find('template=basic-page');

Then, repeat for another template.

Edit: I hope this will help you to get rid of that spanish, and replace it for the so much nicer portuguese ;)

Link to comment
Share on other sites

I was able to build this final working code based on your help. This code checks for language fields only so it's more safe.

Thanks a lot!!!


$langAlt = $languages->get("pt");
$langDefault = $languages->get("default");

$items = $pages->find('template=contacto');

foreach($items as $item){
    $item->of(false);
    $fieldsSelected = $item->fields;
    foreach($fieldsSelected as $field){
    	//echo $field->type;
    	if($field->type == 'FieldtypePageTitleLanguage' OR $field->type == 'FieldtypeTextLanguage' or $field->type == 'FieldtypeTextareaLanguage') {
    		if($item->$field->getLanguageValue($langAlt)) {
    			$title = $item->$field->getLanguageValue($langAlt); // get the alternative lang value
    			$item->$field->setLanguageValue($langDefault, $title); // set the new value
    		}
    		
    	}
    	$item->name = $sanitizer->pageName($item->title);
       
    }
   echo $item->save();

}

Link to comment
Share on other sites

 But... I've downloaded the latest dev branch and replaced the "wire/modules/LanguageSupport" folder but I still can't see any checkboxes. 

LanguageSupport is a core module and has to be upgraded with the rest of the core. So make sure you are replacing your entire /wire/ directory, and not just /wire/modules/LanguagesSupport/. Most likely you didn't see the checkboxes because they are a component of InputfieldPageName. But don't just go upgrade that module–upgrade the entire core. 

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...