Juergen Posted November 3, 2017 Share Posted November 3, 2017 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 More sharing options...
BitPoet Posted November 3, 2017 Share Posted November 3, 2017 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: 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: 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. 1 Link to comment Share on other sites More sharing options...
Juergen Posted November 3, 2017 Author Share Posted November 3, 2017 Thanks @BitPoet, the reason for this is because I want to show additional event info inside the page tree next to each event. 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: That was the idea behind this. Link to comment Share on other sites More sharing options...
BitPoet Posted November 3, 2017 Share Posted November 3, 2017 How about a generic settings template + page where you can enter the translations in a regular text field? Then just get that settings page and use getLanguageValue() for that field. Link to comment Share on other sites More sharing options...
LostKobrakai Posted November 3, 2017 Share Posted November 3, 2017 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. 1 Link to comment Share on other sites More sharing options...
BitPoet Posted November 3, 2017 Share Posted November 3, 2017 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. 1 Link to comment Share on other sites More sharing options...
Juergen Posted November 3, 2017 Author Share Posted November 3, 2017 Thanks for your tipps. I will try it via the Hook method. Link to comment Share on other sites More sharing options...
Juergen Posted November 6, 2017 Author Share Posted November 6, 2017 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: 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now