10000 [intl] Get all language names with the country language · Issue #31562 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[intl] Get all language names with the country language #31562

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
lsv opened this issue May 21, 2019 · 3 comments
Closed

[intl] Get all language names with the country language #31562

lsv opened this issue May 21, 2019 · 3 comments

Comments

@lsv
Copy link
lsv commented May 21, 2019

Description

Lets say you have a language selector, now you are on the english site, so all the languages are in english fx

  • Danish
  • French
  • Russian

Now you select Russian, now the dropdown will look something like this

  • английский
  • датский
  • французский

Instead it would be lovely to have a dropdown that always look something like this

  • Dansk
  • Français
  • Pусский

Example

$locales = Intl::getLocaleBundle()->getLocaleNamesNotTranslated();
dump($locales);
....
"ru" => "русский",
"da" => "dansk",
"fr" => "français",
....

It would be exactly like doing this

\Locale::setDefault('fr');
$locales = Intl::getLocaleBundle()->getLocaleNames();
$french = $locales['fr'];

\Locale::setDefault('ru');
$locales = Intl::getLocaleBundle()->getLocaleNames();
$russian = $locales['ru'];

\Locale::setDefault('da');
$locales = Intl::getLocaleBundle()->getLocaleNames();
$danish = $locales['da'];

@ro0NL
Copy link
Contributor
ro0NL commented May 25, 2019

I tend to believe the current API allows to built such a list already.

Using forms it's simply as

$form = $this->createForm(LanguageType::class, null, [
    'choice_label' => function ($lng) {
        return Intl::getLanguageBundle()->getLanguageName($lng, null, $lng);
    },
]);

@javiereguiluz
Copy link
Member

I'm not sure this feature is common enough to provide it as a feature. Moreover, when the alternative is only a couple of lines of code. I'm 👎 about the proposal unless someone comes with some compelling reasons.

@javiereguiluz
Copy link
Member

Since I published the above comment I've changed my mind around this. The feature proposed is not that uncommon. In fact, even the Symfony Demo app now displays each language into its own language:

image

So, I've created #32388 to implement this feature.

fabpot added a commit that referenced this issue Dec 3, 2019
…age in LanguageType (javiereguiluz)

This PR was squashed before being merged into the 5.1-dev branch (closes #32388).

Discussion
----------

[Form] Allow to translate each language into its language in LanguageType

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #31562
| License       | MIT
| Doc PR        | I'll do it if this is approved

This would allow to set this option:

```php
->add('language', LanguageType::class, [
    'choice_self_translation' => true,
])
```

To display each language translated into its own language:

![image](https://user-images.githubusercontent.com/73419/60709908-cdf29b80-9f11-11e9-83c8-8ee939c0de3a.png)

@ro0NL if this proposal is approved, could you please tell me why I must `try ... catch` with `\Exception`?

* First, I need the try..catch because of a special language with the code "root" which triggers exceptions (*The resource bundle "symfony-code/src/Symfony/Component/Intl/Resources/data/languages/root.json" does not exist*)
* Second, I must catch \Exception because when I try to catch the exact exception (`Symfony\Component\Intl\Exception\ResourceBundleNotFoundException`) the exception is not caught. Why?

Commits
-------

138200c [Form] Allow to translate each language into its language in LanguageType
@fabpot fabpot closed this as completed Dec 3, 2019
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

4 participants
0