8000 [Form] Added locale option for intl form types. by Koc · Pull Request #23629 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Added locale option for intl form types. #23629

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

Closed
wants to merge 2 commits into from

Conversation

Koc
Copy link
Contributor
@Koc Koc commented Jul 22, 2017
Q A
Branch? 3.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

If this sollution is acceptable, I will update other form types and validators.

@Koc Koc force-pushed the form-types-locale branch from 3e213ba to 7d0476b Compare July 22, 2017 23:58
@Koc Koc changed the title Added locale option for intl form types. [Form] Added locale option for intl form types. Jul 23, 2017
@Koc
Copy link
Contributor Author
Koc commented Jul 23, 2017

//cc @ogizanagi

@ogizanagi
Copy link
Contributor
ogizanagi commented Jul 23, 2017

Looks sensible to me.

At first, I was just wondering how much this could relate to #23611. I.e, should we create this new locale option for Intl types, or could we reuse the suggested (choice_)translation_language from this issue. But I think it'll look weird reusing these translator specific options, and locale identifiers might not match.

@ro0NL
Copy link
Contributor
ro0NL commented Jul 23, 2017

I like the idea of (choice_)translation_locale. And in fact i think we can use exactly that for built-in intl translations, using it as $inLocale. I believe \Locale canonicalizes anyway; there's no real difference here in terms of having 2 types of "locale" or so.

Ideally it would check the SF translator first for any overrides :) and then bypasses translating in the view.

@Koc
Copy link
Contributor Author
Koc commented Jul 23, 2017

It is possible to rework PR and add (choice_)translation_locale option, but can you choose before - what is better - (choice_)translation_locale for all form types or just locale option for intl-specific form types?

@ogizanagi
Copy link
Contributor
ogizanagi commented Jul 24, 2017

My concerns about using (choice_)translation_locale:

Also, I think use-cases for this are very narrow actually; I don't think it's a blocker as the implementation should be straightforward and easily covered, but still, could you elaborate by describing your own use-case? That would probably help for the adoption of both this PR and #23611.

cc @HeahDude

@ro0NL
Copy link
Contributor
ro0NL commented Jul 24, 2017

it hijacks an option originally tied to the usage of forms with the translator component. I fear it'll bring confusion

The turnaround is we have locale and possibly choice_translation_locale options here. I understand the technical difference, but im not sure the user cares/knows; hence it could be confusing just as well. Eventually this is about translating.

By using the same option, we're basically saying we accept any of the format supported by the translator, which is wrong.

Not sure, only SF format is supported which is accepted by intl. So we can leverage it as a translator.

Otherwise i'd call this option intl_locale

@Hanmac
Copy link
Contributor
Hanmac commented Jul 24, 2017

might be for another PR but should the Intl Form Types have use the (X)Html attribute "lang" for that?

other thing: should the intl_locale be a calleable function?
because i got the idea LanguageType and LocaleType to have an option to be selftranslateable.
(means for "de" it shows "Deutsch" while for "en" it shows "english")
if it would be a callable thing that would be able to done.

PS: i know that would make the choiceList more complicated

@ogizanagi
Copy link
Contributor

@Hanmac : I think it's out of the scope indeed, but at least for the LocaleType and LanguageType, it's easy to solve in userland by using your own type extending it and the choice_label option:

class SelfTranslatedLocaleType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault('choice_label', function ($value) {
            return Intl::getLocaleBundle()->getLocaleName($value, $value);
        });
    }

    public function getParent()
    {
        return LocaleType::class;
    }
}

For the CountryType, you'll need a country <-> primary language/locale mapping, which I don't think the intl and icu libraries provide. Anyway, it's probably not perfect, but the point is you don't need the locale option to be a callable for this use-case to me.

@Hanmac
Copy link
Contributor
Hanmac commented Jul 24, 2017

@ogizanagi: that's why I said only language and Locale. Because the mapping from country to language isn't always clear. But if you can overwrite the choice_label then you might not even need your own type extension

@ghost
Copy link
ghost commented Jul 25, 2017

Can this be done for Assert Constraints or the form_errors() can force the language as well?

@@ -44,9 +46,12 @@ public function configureOptions(OptionsResolver $resolver)
return null;
}

$this->locale = $options['locale'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the use case looks weird, this may be overridden for each use of the type in the same request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does form types are shared services?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they are shared services re-used to build each of your form instances. So usage of class properties should be prohibited in most cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably deprecate the ChoiceLoaderInterface implementation in those types as done in #23648.

Copy link
Contributor Author
@Koc Koc Jul 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. It confused me before: it was so hard to find place where should I save locale from options 🤣

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's either this, either decorating the CountryType's ChoiceLoaderInterface implementation when using the locale option, so we keep a cache of base choice list (and will always be used as before when not using the option). But this cache is not worth it to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Koc: Do you have some time to finish this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will try do it during this last week before stabilization phase.

@nicolas-grekas
Copy link
Member

Moving to 4.1. Rebase on master might be needed, where PHP 7.1 features can be used btw.

@fabpot
Copy link
Member
fabpot commented Apr 20, 2018

closing in favor of #26825

@fabpot fabpot closed this Apr 20, 2018
symfony-splitter pushed a commit to symfony/form that referenced this pull request Apr 22, 2018
…hoice types (yceruto, fabpot)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Form] Add choice_translation_locale option for Intl choice types

| Q             | A
| ------------- | ---
| Branch?       | master (4.2)
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #26737
| License       | MIT
| Doc PR        | symfony/symfony-docs#...

This PR adds possibility to show the list of elements for a custom locale different to the current one and proposes also deprecate the `ChoiceLoaderInterface` implementation from all of them, moving it to a separate class.

See related issue #26737 for full description and use-case.

After:
```php
$formBuilder->add('country', CountryType::class, [
    'choice_translation_locale' => 'es',
]);
```

Alternative of symfony/symfony#23629

TODO:

- [x] Update `UPGRADE-*.md` and `src/**/CHANGELOG.md` files.
- [x] Add some tests.

Commits
-------

9592fa64cf moved feature to 4.1
e250dfa702 Add choice_translation_locale option for Intl choice types
fabpot added a commit that referenced this pull request Apr 22, 2018
…hoice types (yceruto, fabpot)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Form] Add choice_translation_locale option for Intl choice types

| Q             | A
| ------------- | ---
| Branch?       | master (4.2)
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #26737
| License       | MIT
| Doc PR        | symfony/symfony-docs#...

This PR adds possibility to show the list of elements for a custom locale different to the current one and proposes also deprecate the `ChoiceLoaderInterface` implementation from all of them, moving it to a separate class.

See related issue #26737 for full description and use-case.

After:
```php
$formBuilder->add('country', CountryType::class, [
    'choice_translation_locale' => 'es',
]);
```

Alternative of #23629

TODO:

- [x] Update `UPGRADE-*.md` and `src/**/CHANGELOG.md` files.
- [x] Add some tests.

Commits
-------

9592fa6 moved feature to 4.1
e250dfa Add choice_translation_locale option for Intl choice types
@Koc Koc deleted the form-types-locale branch July 22, 2019 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0