private function translateForm() { $translations = array('en_GB' => array('default' => array('label' => 'Fallback shop', 'description' => 'Forward to this shop if not found any shop languages matching browser language'), 'assignedShops' => array('label' => 'Related shops', 'description' => 'Forwards to these shops, if the browser language equals the shop language.'), 'fallbackLanguage' => array('label' => 'Fallback language for the modal', 'description' => 'This is the locale for the translation that will be used by default, if no matching translation was found for the user to be displayed in the infobox in the frontend.'))); // In 4.2.2 we introduced a helper function for this, so we can skip the custom logic if ($this->assertMinimumVersion('4.2.2')) { $this->addFormTranslations($translations); return true; } // Translations $form = $this->Form(); $shopRepository = Shopware()->Models()->getRepository('\\Shopware\\Models\\Shop\\Locale'); //iterate the languages foreach ($translations as $locale => $snippets) { $localeModel = $shopRepository->findOneBy(array('locale' => $locale)); //not found? continue with next language if ($localeModel === null) { continue; } if ($snippets['plugin_form']) { // Translation for form description foreach ($form->getTranslations() as $translation) { if ($translation->getLocale()->getLocale() == $locale) { $formTranslation = $translation; } } // If none found create a new one if (!$formTranslation) { $formTranslation = new \Shopware\Models\Config\FormTranslation(); $formTranslation->setLocale($localeModel); //add the translation to the form $form->addTranslation($formTranslation); } if ($snippets['plugin_form']['label']) { $formTranslation->setLabel($snippets['plugin_form']['label']); } if ($snippets['plugin_form']['description']) { $formTranslation->setDescription($snippets['plugin_form']['description']); } unset($snippets['plugin_form']); } //iterate all snippets of the current language foreach ($snippets as $element => $snippet) { $translationModel = null; //get the form element by name $elementModel = $form->getElement($element); //not found? continue with next snippet if ($elementModel === null) { continue; } // Try to load existing translation foreach ($elementModel->getTranslations() as $translation) { if ($translation->getLocale()->getLocale() == $locale) { $translationModel = $translation; break; } } // If none found create a new one if (!$translationModel) { $translationModel = new \Shopware\Models\Config\ElementTranslation(); $translationModel->setLocale($localeModel); //add the translation to the form element $elementModel->addTranslation($translationModel); } if ($snippet['label']) { $translationModel->setLabel($snippet['label']); } if ($snippet['description']) { $translationModel->setDescription($snippet['description']); } } } }
/** * translates the plugin configuration form with the given translations * * @param $translations */ private function translateForm($translations) { // Translations $shopRepository = Shopware()->Models()->getRepository('\\Shopware\\Models\\Shop\\Locale'); //iterate the languages foreach ($translations as $locale => $snippets) { $localeModel = $shopRepository->findOneBy(array('locale' => $locale)); //not found? continue with next language if ($localeModel === null) { continue; } if ($snippets['plugin_form']) { // Translation for form description $formTranslation = null; /* @var \Shopware\Models\Config\FormTranslation $translation */ foreach ($this->form->getTranslations() as $translation) { if ($translation->getLocale()->getLocale() == $locale) { $formTranslation = $translation; } } // If none found create a new one if (!$formTranslation) { $formTranslation = new \Shopware\Models\Config\FormTranslation(); $formTranslation->setLocale($localeModel); //add the translation to the form $this->form->addTranslation($formTranslation); } if ($snippets['plugin_form']['label']) { $formTranslation->setLabel($snippets['plugin_form']['label']); } if ($snippets['plugin_form']['description']) { $formTranslation->setDescription($snippets['plugin_form']['description']); } unset($snippets['plugin_form']); } //iterate all snippets of the current language foreach ($snippets as $element => $snippet) { $translationModel = null; //get the form element by name $elementModel = $this->form->getElement($element); //not found? continue with next snippet if ($elementModel === null) { continue; } // Try to load existing translation foreach ($elementModel->getTranslations() as $translation) { if ($translation->getLocale()->getLocale() == $locale) { $translationModel = $translation; break; } } // If none found create a new one if (!$translationModel) { $translationModel = new \Shopware\Models\Config\ElementTranslation(); $translationModel->setLocale($localeModel); //add the translation to the form element $elementModel->addTranslation($translationModel); } if ($snippet['label']) { $translationModel->setLabel($snippet['label']); } if ($snippet['description']) { $translationModel->setDescription($snippet['description']); } } } }