1

Does anyone know how to make the dropdown field translatable?

I'm working on a site which has both English and German languages, but whenever I set up a dropdown field it only ever appears in English and I don't have an option for the German version.

EDIT: I'm needing this to work as a dropdown on the German site also and not through templating {{ "word" | t }} as I need the client to have total control of this feature.

Any help would be appreciated — thanks.

Screenshot of the dropdown menu field in CraftCMS

1

It does it per default:

public function getInputHtml($value, ElementInterface $element = null): string
{
    return Craft::$app->getView()->renderTemplate('_includes/forms/select', [
        'name' => $this->handle,
        'value' => $value,
        'options' => $this->translatedOptions(), //<-- here are your translated options
    ]);
}

But you need to insert your translations into your translation files for the site category

protected function translatedOptions(): array
{
    $translatedOptions = [];

    if ($this->options) {
        foreach ($this->options as $option) {
            $translatedOptions[] = [
                'label' => Craft::t('site', $option['label']),
                'value' => $option['value']
            ];
        }
    }

    return $translatedOptions;
}
|improve this answer|||||
  • Ah okay — I have a translation file already set up but wanted the client to have total control over what the German options would be. Guessing the translation file is the only option really? – fbnntt May 1 '18 at 15:08
  • No - if you want to change the translation method, you can use a database table as well craftcms.stackexchange.com/questions/25926/… – Robin Schambach May 1 '18 at 15:09
  • Brilliant — I'll have a look into that. Thanks! – fbnntt May 1 '18 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.