Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select an Option string in dropdown is not translatable #1704

Open
2 tasks done
pschiffe opened this issue Mar 7, 2024 · 3 comments
Open
2 tasks done

Select an Option string in dropdown is not translatable #1704

pschiffe opened this issue Mar 7, 2024 · 3 comments

Comments

@pschiffe
Copy link
Contributor

pschiffe commented Mar 7, 2024

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

  • Check that there isn't already an issue that reports the same bug
  • Double check that your reproduction steps work in a fresh installation of silverstripe/installer (with any code examples you've provided)
@GuySartorelli
Copy link
Member

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
Copy link
Contributor Author

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 😅

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants