Open tomhv opened 9 years ago
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. So I just need to tweak the vendor code with the first code, and then create this file (2nd code) in my own bundle ? I use the JMSI18nRoutingBundle on it's basic configuration.
Don't edit the vendor code.
DefaultPatternGenerationStrategy
from the JMSI18nRoutingBundle to your app bundle (AppBundle\Router\PatternGenerationStrategy
).$i18nPattern = '/'.$locale.$i18nPattern;
$i18nPattern = '/'.$this->transform($locale).$i18nPattern;
AppBundle\Router\PatternGenerationStrategy::transform()
methodjms_i18n_routing.pattern_generation_strategy.class: AppBundle\Router\PatternGenerationStrategy
That should do it.
OK great ! I found it strange to update the vendor code ;)
@tomhv I guess you meant jms_i18n_routing.pattern_generation_strategy.class: AppBundle\Router\DefaultPatternGenerationStrategy
right ?
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 AppBundle\Router\PatternGenerationStrategy
@tomhv this is working nicely.
One warning message/error is raised though, when I "extract" the routes with the command app/console translation:extract fr_FR --config=app --output-format=xliff
I get this
Can only extract the translation id from a scalar string, but got "PHPParser_Node_Expr_Variable". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in ../src/Des/AppBundle\Router\DefaultPatternGenerationStrategy.php on line 47)
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.