-
Notifications
You must be signed in to change notification settings - Fork 159
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
Ability to transform locale prefix #159
Comments
I have the same issue. @tomhv , does your code make this feature available ? |
Hey @picks44, I used the code above and then the following method to transform my locale. Not sure if that's what you were asking, but there you go. Cheers, Tom <?php
// src/AppBundle/Router/PatternGenerationStrategy.php
namespace AppBundle\Router;
use JMS\I18nRoutingBundle\Router\PatternGenerationStrategyInterface;
use Symfony\Component\Routing\RouteCollection,
Symfony\Component\Translation\TranslatorInterface,
Symfony\Component\Routing\Route;
class PatternGenerationStrategy implements PatternGenerationStrategyInterface
{
// ...
/**
* transform _locale into URL prefix
*
* e.g. en_US => /us/en
*
* @param string $locale
*
* @return string
*/
private function transform($locale)
{
switch ($locale) {
case 'en_GB':
return 'uk/en';
default:
return sprintf(
'%s/%s',
strtolower(substr($locale,3,2)),
strtolower(substr($locale,0,2))
);
}
}
} |
Thanks a lot @tomhv , I'll try that. |
Don't edit the vendor code.
That should do it. |
OK great ! I found it strange to update the vendor code ;) |
@tomhv I guess you meant In the parameters of my config.yml ? |
I have this in my params.yml file, but it could go in your config.yml file too. For the value, you should use whatever class you created with your custom code. I used |
@tomhv this is working nicely. One warning message/error is raised though, when I "extract" the routes with the command I get this Not sure it has something to do with this hack, but the targeted file is the one which we use for our matter. |
Hey @picks44, I haven't tried extracting the translations yet, so I don't know if it works. |
Would it be possible to update the DefaultPatternGenerationStrategy to have some kind of hook to allow some kind of transform on the
_locale
before it is added as a prefix?We use country + language for our locale and would prefer to have it prefixed like this:
than this:
Another transform I can see is changing gb to uk to be more consistent with the .co.uk/.uk siffixes.
This is where I have added the call to the transform method in my code: https://github.com/schmittjoh/JMSI18nRoutingBundle/blob/master/Router/DefaultPatternGenerationStrategy.php#L69
I know I can provide my own PatternGenerationStrategy, and I have done, but I'd hate to miss out on any bug fixes to the rest of the class.
The text was updated successfully, but these errors were encountered: