Juergen Posted February 14, 2022 Share Posted February 14, 2022 Hello, I am struggeling with the following problem: I have created a module that creates 2 additional text fields during the module installation. As documented ,on version ProcessWire 3.0.181 and above you can ship your modules with additional translation files inside the languages folder. This works great. My problem is that I want to add the translations for the 2 additional fields too. It works great for the module itself, but I dont know how to save the translations to the field too after installing the language file to a certain language. The workflow should be the following: After importing and saving fe the German csv file, the translations should also be added and stored on the 2 additional fields (fe the Label in German). Unfortunately there is no useable hookable function inside the ProcessLanguage class (https://github.com/processwire/processwire/blob/master/wire/modules/LanguageSupport/ProcessLanguage.module). Does anyone has an idea how to accomplish this? Thanks in advance for hints. Link to comment Share on other sites More sharing options...
Juergen Posted February 14, 2022 Author Share Posted February 14, 2022 I have found a solution, but you have to hack the core file "ProcessLanguage.php"a little bit. The perfect method to save translations to fields created by a module is to hook after the "processCSV" method of the "ProcessLanguage" class file. Only problem: This method is not hookable by default, so I added 3 underscores to the method name to make it hookable. Goal: I want to add the German labels to the 3 fields created by the module after uploading and saving the translation csv file for this module. The label translations are inside the module translation csv file and I need to save it to the 3 fields. This is the main problem, because this field were created by the module but they are not part of the module, so I have to create/save the translations for this fields in an extra step. They will not be saved by adding the translation file to the module by default. To save the labels of the fields in another language I use the method below: // Define the hook function inside the init method public function init() { $this->addHookAfter('ProcessLanguage::processCSV', $this, 'addTranslationsToFields'); } /** * Method to add translation to the labels of fields created by this module * @param HookEvent */ protected function addTranslationsToFields(HookEvent $event){ // get the language object of the uploaded translation csv file $language = $event->arguments(1); $translation = new LanguageTranslator($language); $fields = ['jk_publish_open', 'jk_publish_from', 'jk_publish_until']; // name of the field to translate the labels foreach($fields as $fieldName){ //grab the field object $field = $this->wire('fields')->get($fieldName); $label = $translation->getTranslation($this, $field->label); $field->set("label{$language->id}", $label); $field->save('label'); } } The 3 fields where I want to add the translations to the labels are called 'jk_publish_open', 'jk_publish_from', 'jk_publish_until'. After uploading the translation csv. file for the module, the translations for the labels will be also added to the extra created fields which are not part of the module. This works like a charme (thanks to the PW LanguageTranslator class). I will try to convince Ryan to make the processCSV method hookable by default. Therefore I have submitted a pull request for making this method hookable by default: Quote https://github.com/processwire/processwire-requests/issues/436 Best regards 1 Link to comment Share on other sites More sharing options...
Juergen Posted February 15, 2022 Author Share Posted February 15, 2022 Ryan will make the processCSV method hookable in the next update ? No problem, I will make sure it is hookable in the next dev branch version 3.0.195. If you'd like feel free to add the 3 underscores to your copy and it'll be present on the next update. 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