-
Notifications
You must be signed in to change notification settings - Fork 178
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
Add localized slugging #210
Labels
Comments
👍 but I would really try to avoid having a class for each locale. Let's try it with a simple config, first. |
This is the custom file so far that works <?php
namespace App\Inflector;
use Cake\Utility\Text;
class LocalizedInflector
{
/**
* We can move this later into config.
*
* @var string[]
*/
protected static $slugMap = [
'de' => 'de-ASCII; Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove',
];
/**
* We can move this later into config.
*
* @var string[]
*/
protected static $normalizeMap = [
'de' => 'de-ASCII; Any-Latin; Latin-ASCII',
];
/**
* Transliterate only (removal of accents, but not replacing of e.g. -).
*
* @param string $string
* @param string $code ISO 2 code lowercase
* @param array $options
*
* @return string
*/
public static function normalize(string $string, string $code, array $options = []): string
{
if (isset(static::$normalizeMap[$code])) {
$options['transliteratorId'] = static::$normalizeMap[$code];
}
return Text::transliterate($string, $options['transliteratorId'] ?? null);
}
/**
* Slug using specific language version where possible.
*
* @param string $string
* @param string $code ISO 2 code lowercase
* @param array $options
*
* @return string
*/
public static function slug(string $string, string $code, array $options = []): string
{
if (isset(static::$slugMap[$code])) {
$options['transliteratorId'] = static::$slugMap[$code];
}
return Text::slug($string, $options);
}
} We might want to make it non static and allow Configure::read() to merge in any custom project ones. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I propose
etc as e.g. normal inflection (default cake core) of
Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove
is invalid for German for example.Refs https://stackoverflow.com/questions/50412085/how-to-make-textslug-convert-german-umlauts-properly
Here one would expect
de-ASCII; Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove
.We cannot hardcode this into bootstrap however for a multi-language site, as this will kill the others, e.g.
Turkish
Söğütlü
wouldn't beSogutlu
anymore, butSoeguetlue
.So for this we should add localized ones per language - and invoke that based on that language then inside the business logic.
Is there a better way to avoid too many classes?
Maybe with some simple config instead?
The text was updated successfully, but these errors were encountered: