Jump to content

Change Language Page Url


Dave Damage
 Share

Recommended Posts

Hello folks,

I am currently working at a project where I have to import a Wordpress-Site to Processwire. I have a bunch of multilingual Pages and would like to import them to Processwire. Everything works fine but as soon as I try to set the Page-Url, Processwire throws the error: "Error: Call to a member function setLanguageValue() on a non-object".

$page = new Page();
        if($category['parent'])
        {
            $parent = $this->sanitizer->pageName($category['parent'], true);
            $page->parent =  wire('pages')->get('/articles/'.$parent.'/');
        }
        else
        {
            $page->parent = wire('pages')->get('/articles/');
        }
        $page->template = 'category';
        $deName = $this->sanitizeUmlaut($category['de']);
        $en = $this->languages->get("default");
        $de = $this->languages->get("german");
        $page->name = $this->sanitizer->pageName($category['name'], true);
        $page->title->setLanguageValue($en, $category['name']);
        $page->title->setLanguageValue($de, $deName, true);

        $page->save();
        $page->url->setLanguageValue($en, $category['name']);
        $page->url->setLanguageValue($de, $deName, true);

        $page->wp_id = $category['id'];

        $page->save();
        $this->message('Successfully imported '.$category['name']);

Has anyone an idea on how to solve this problem?

Link to comment
Share on other sites

Hey Soma,

thank you for your quick response. Somehow turning off outputformatting did not work: The title is set whether it is turned on or off, but everytime I try to set the localName oer localURL (http://processwire.com/api/multi-language-support/multi-language-urls/) of the page, it throws the same error. 

$page->localName->setLanguageValue($en, $category['name']);

Furthermore, I seem unable to find how to set the "active"-Status of the URL in the API...

[Edit:] Nevermind, I found the solution here: http://processwire.com/api/multi-language-support/multi-language-urls/

Link to comment
Share on other sites

Hi Dave,

I'm having the same problem - trying to set the page name in another language, and setting it to "active". I looked at the page you pointed to in your [Edit], above, but I'm only seeing a way to read those values... Would you mind sharing what worked for you?

Thanks!

Link to comment
Share on other sites

Hi Dave,

I'm having the same problem - trying to set the page name in another language, and setting it to "active". I looked at the page you pointed to in your [Edit], above, but I'm only seeing a way to read those values... Would you mind sharing what worked for you?

Thanks!

$page->set("status$de",1);

Sets the status for the $language (in my case $de).

$page->set("name$de", $this->sanitizer->pageName($pageName_de, [true]));

With "name$language" (in my case again $de) you set the Pagename in the Language.

I don't know if its best practice to set the default language (in my case $en) with:

$page->name = $this->sanitizer->pageName($pageName, [true]); 

or

$page->set("name$en", $this->sanitizer->pageName($pageName_en, [true])); 

I went with the first option, and did not try the second one. Depending on the language, I highly recommend setting up a small script, that converts language-related mutations ( you english people really call Umlaute 'mutations'?) to more machine-readable characters ("oe" for "ö").

PS: I really like the way processwire handles languages compared to other systems (I'm looking at you, WP!). 

[Edit]: Somehow I posted the wrong link above and I am unable to retrieve the working one.

Edited by Dave Damage
Link to comment
Share on other sites

You would use the beautifier translate option (Sanitizer::translate) to have sanitizer use the special char conversion

$name = $this->sanitizer->pageName($pageName_en, Sanitizer::translate);

The chars are defined in the PageName module config screen, where you can set conversion for each char like "ö => oe", default is "ö => o"

  • Like 2
Link to comment
Share on other sites

  • 9 years later...

Hi ,

this topic is quite tricky, using the API to insert multilangual content, thanks for the above input. This works for me, just if someone needs it. Code is not cleaned:

// Array with parent ID, object IDs of external database
$externalDataFromJson["pwParentID"] = 12345;
$externalDataFromJson["DE"] = '{"12": "Äpfel", "13": "Orangen" }';
$externalDataFromJson["EN"] = '{"13": "Oranges","12": "Apples"}';

createIt($externalDataFromJson);

function createIt($data) {
    $dataDE = json_decode($data["DE"], true);
    $dataEN = json_decode($data["EN"], true);

    $DE = \ProcessWire\wire("languages")->get("name=default"); // Get the language object for the default language
    $EN = \ProcessWire\wire("languages")->get("name=en"); // Get the language object for the second language

    foreach ($dataDE as $id => $term_DE) {
        $findItBefore = \ProcessWire\wire("pages")->findOne('template=_vocabulary,external_id=' . $id);

        if ($findItBefore instanceof NullPage) {
            echo "<br>Do not Exists: " . $id . ' - ' . $term_DE . ' create: external_id ';
            $p = new \ProcessWire\Page();
            $p->setOutputFormatting(false);
            $p->parent = \ProcessWire\wire("pages")->get( $data["pwParentID"] );
            $p->template = '_vocabulary';
        } else {
            echo "<br>Exists: " . $id . ' ' . $term_DE;

            //var_dump($findItBefore);

            $p = $findItBefore;
            $p->setOutputFormatting(false);
            //$p->of(false); // outputFormatting must be OFF
        }
        if (isset($dataEN[$id])) {
            $term_EN = $dataEN[$id];
        } else {
            $term_EN = "Eng not found";
        }

        $p->name = \ProcessWire\wire('sanitizer')->pageName($term_DE, true);

        $p->title->setLanguageValue($DE, $term_DE, true);
        $p->title->setLanguageValue($EN, $term_EN);
        $p->external_id = $id;
        $p->save();

        //$p->set("status$de",1);
        $p->set("status$EN",1);

        //$p->set("name$de", \ProcessWire\wire('sanitizer')->pageName($term_DE, [true]));
        $p->set("name$EN", \ProcessWire\wire('sanitizer')->pageName($term_EN, [true]));

        $p->save();


        $url = $p->localUrl("de");
        echo "<hr>";
        var_dump($url);
        $url = $p->localUrl("en");
        echo "<hr>";
        var_dump($url);

        echo "<hr>";

    }
}

Now in settings/ the second URL (in my case "en") is "active" and translated :)

multilangual_url_pw.png

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