Jump to content

How to store translateable value in multilanguage textfield via API


Juergen
 Share

Recommended Posts

Hello @ all,

I am struggeling by storing a translateable string into multilanguage textfield via the API.

This is the translateable string:

__("all day long")

I want to store this into the multilanguage textfield, but it doesnt work as expected. This is what I have so far:

                foreach ($this->languages as $lang) {
                $langtype = $lang->get('name');
                   $page->eventpagetree->setLanguageValue($langtype, __("all day long")); 
                }

But this stores always the same value in both languages.

The name of the multilanguage textfield is "eventpagetree" in this case.

Can someone give me a hint or a working solution?

Link to comment
Share on other sites

I don't think that's something to be solved in a completely straight-forward way, as PW's i18n functions weren't built for such a use case. I can see two possible approaches:

  1. Save the user's current language, then in the loop set it to the current language before assigning the value and restore the saved language afterwards - likely not advisable though, since all kinds of things happening in between (like messages or hooks) would also see the changed language. Or:
  2. Write a function to determine the appropriate text domain string like the original __() function in core/LanguageFunctions.php does, then use $lang->translator()->getTranslation($textdomain, "all day long", '') - though also not what I call trivial.

Perhaps someone could come up with an easier solution to the underlying requirement if you outline why you want to fill fields this way, though.

  • Thanks 1
Link to comment
Share on other sites

Thanks @BitPoet,

the reason for this is because I want to show additional event info inside the page tree next to each event.

screenshot-www.juergen-kern.at-2017-11-03-16-41-26.png.18b2e1313c8b60d730760b021adf0513.png

So if the event has a specific start and end time, the times will be shown next to the date. If the event is all day long, the text "all day long" (in German ganztägig) will be displayed.

Therefore I have created a simple textfield (name: eventpagetree) in which the corresponding value will be added via a Hook.

On the template side I create the pagetree values like this:

screenshot-www.juergen-kern.at-2017-11-03-16-54-22.png.e59534bd3f161ffaaa747e01b510ef28.png

That was the idea behind this.

Link to comment
Share on other sites

20 minutes ago, LostKobrakai said:

I would try to hook the code, where the label is generated and not bloat all the pages with additional fields just to show a string in the pagetree.

Yes, that too. In combination the code could be as straight forward as this (example for site/ready.php):

wire()->addHookAfter("Page::loaded", null, "conjureField");

function conjureField(HookEvent $event) {
	$page = $event->object;
	
	if($page->template->name != "event") return;
	
	$settings = $pages->get('/admin/settings/');
	
	$page->set("magicalfield", $settings->wholedaytext);
}

Then "{magicalfield}" works in the event template's page list settings, you don't have to store anything (so if you want to change the text, there's no need to update existing pages), and you don't have to loop over languages since the built-in multi language support works its usual magic.

  • Like 1
Link to comment
Share on other sites

My final solution is to use a Hook as @LostKobrakai recommended:

Here is an example for others to hook into the page tree label. Code has to be placed inside ready.php

$wire->addHookAfter('ProcessPageListRender::getPageLabel', function($event) {
       $label = $event->object;
       $page = $event->arguments(0);
       if($page->template->name == "single-date"){
        $event->return = $page->title.'-test';
       }
});

For testing purposes I only use it on one template type called "single-date" and I change the label from "title" to "title + test".

Here is what it looks like:

screenshot-www.juergen-kern.at-2017-11-06-12-00-48.png.bb02d52b7dad7de130a83bbe2be8b979.png

As you can see the text "test" will be added on that template type next to the page title. This is only for others to see how it can be done. You have the idea.:)

Only to mention: You will only need this Hook if you want to add text or values which are not stored in the DB. Otherwise you can use the field in the template settings.

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