silverstripe / silverstripe-admin

Silverstripe Admin Component
BSD 3-Clause "New" or "Revised" License
25 stars 91 forks source link

`Select an Option` string in dropdown is not translatable #1704

Open pschiffe opened 3 months ago

pschiffe commented 3 months ago

Module version(s) affected

2.1.17

Description

The $has_one relation of DataObject renders in the admin as a dropdown. And if this relation doesn't have some default configured, it shows text Select an Option, for example, when creating a new instance of the DataObject class.

The text probably comes from some 3rd party, it doesn't seem to react to the browser language settings. The classes around the dropdown are: chosen-container chosen-container-single chosen-container-single-nosearch and chosen-single chosen-default.

Could you help me to identify the said 3rd party library and maybe find a way how to translate it? In the Silverstripe v3 there was no text visible.

307545016-159c6126-ff19-42bb-90df-f37cb1acae9c

How to reproduce

class A extends DataObject {

    private static $has_one = array(
        'B'    => 'B',
    );
...

Possible Solution

No response

Additional Context

No response

Validations

GuySartorelli commented 3 months ago

You've given the css classes, but those are a lot less useful than what the actual underlying PHP FormField implementation is.

If it's being autoscaffolded, that will be a DropdownField. I've played around a bit with the code that scaffolds the form field. It turns out that ' ' is a bit magic. If that was an empty string, or a string containing some text, the empty string or text would be displayed. But for some reason chosen looks at the ' ' differently.

"Select an Option" is the default placeholder text for chosen dropdowns.

There are a couple of ways to tackle this:

  1. Make the text localisable only for DBForeignKey autoscaffolding This would be adding something like ->setAttribute('data-placeholder', _t(__CLASS__ . '.SelectOption', 'Select an Option')) to the field
  2. Make the text localisable for any DropdownField which has this setup This would be more complicated, since it seems like it's being done as a fluke (that someone noticed and intentionally used in the autoscaffolding scenario). This requires finding out what the exact conditions are which cause the default text to be used (e.g. is it a single space specifically? Or any amount of whitespace? etc) and setting a data-placeholder only in those scenarios.

As an aside, the autoscaffolded FormField for has_one relations is changing in Silverstripe CMS 5.2.

pschiffe commented 3 months ago

Hi @GuySartorelli, great job looking into this, thank you. Yes, it's autoscaffolded, I didn't know quickly what else to show you except the CSS :sweat_smile:

Looking at the code you linked, I don't think there's an easy workaround, right?

Reg. the proposed solutions, I don't know what would be the best, nb. 1 seems reasonable to me.

GuySartorelli commented 3 months ago

Option 1 is definitely the least intrusive - but it could result in unexpected behaviour in the (unlikely) scenario someone creates their own DropdownField with whitespace as the "empty string".

Given the extra complexity of option 2 though, I'd be okay with a PR that just implements the first option.