delight-im / PHP-I18N

Internationalization and localization for PHP
MIT License
57 stars 13 forks source link

How to make subdomain based language setting work with only 2-letter language code #12

Closed piejanssens closed 4 years ago

piejanssens commented 4 years ago

fr.example.com en.example.com

<?php
$i18n = new \Delight\I18n\I18n([
    \Delight\I18n\Codes::NL_BE,
    \Delight\I18n\Codes::EN_US,
    \Delight\I18n\Codes::FR_BE
]);
$i18n->setLocaleAutomatically();
?>
<h2><?php echo($i18n->getLocale()); ?></h2>

I can see that the language is correctly detected based on the subdomain, however putting the files in locale/fr or locale/fr_BE is not working to produce the translated webpage. Only when testing fr-BE.example.com the FR text shows.

ocram commented 4 years ago

Excellent point, thank you!

This should have been fixed with this commit now.

Can you try? You’d need to update (to the next minor version) via Composer.

By the way, if you define \Delight\I18n\Codes::FR_BE as supported, locale/fr_BE is also where you put the files, not locale/fr. It’s obvious that you had wanted to test this, of course. The change is that an fr subdomain should now be mapped to \Delight\I18n\Codes::FR_BE as well – unless \Delight\I18n\Codes::FR is also explicitly supported.

Further, matching is case-insensitive, so you can safely use fr-be.example.com in your URLs.

piejanssens commented 4 years ago

Works great, thanks! You might want to add this to the readme. You want to leave it open or close?

ocram commented 4 years ago

Thanks for testing this!

Documentation has now been added as well, so we can close this. Thanks again!