Using Non-Latin Characters for Product and Category URL Keys in Magento
Magento by default converts non-Latin characters in the URL keys of products and categories to Latin characters. If you want to use non-Latin characters like Hebrew letters (×-ת) directly in your URLs, you’ll need to override some core files and modify the behavior.
Understanding the Default Behavior
The default conversion process happens in two key places:
-
Format URL Key Method: This method is located in
Mage/Catalog/Model/Product/Url.php. It uses a regular expression to replace any characters that are not numbers (0-9) or letters (a-z) with a hyphen (-). The lowercase and trim operations further modify the resulting string. -
URL Helper: Magento uses a helper function located in
Mage/Catalog/Helper/Product/Url.phpfor URL conversion. This helper contains a translation table ($_convertTable) that maps Latin characters to their corresponding non-Latin equivalents.
Overriding the Format URL Key Method
To allow non-Latin characters, you need to override the formatUrlKey method in your custom module.
First, create an observer for the catalog_product_save_after event:
public function catalogProductSaveAfter($observer)
{
$product = $observer->getEvent()->getProduct();
$currentUrlKey = $product->getUrlKey();
// Your custom URL key logic here...
if ($newUrlKey !== $currentUrlKey) {
$product->setUrlKey($newUrlKey);
$product->save();
}
}
Then, modify the formatUrlKey method to include Hebrew letters:
public function formatUrlKey($str)
{
$urlKey = preg_replace('#[^0-9a-z×-ת]+#i', '-', Mage::helper('catalog/product_url')->format($str));
$urlKey = strtolower($urlKey);
$urlKey = trim($urlKey, '-');
return $urlKey;
}
Handling Multiple Calls to formatUrlKey
When you use urlencode, it doesn’t work because Magento calls the formatUrlKey method multiple times during URL processing. This is why you need to override the entire method instead of just using a single line like urlencode($str).
Conclusion: Successfully Using Non-Latin Characters in URLs
By following these steps, you should now be able to save and display product and category URLs with non-Latin characters correctly in both the admin area and on your live site.
Remember that working with multi-byte strings requires careful handling. Ensure that you properly encode and decode URLs when generating them and parsing them from requests.
For more advanced customization and troubleshooting tips, I recommend exploring the official Magento developer documentation or reaching out to professional Magento developers for further assistance at Laravel Company.