Skip to content

Instantly share code, notes, and snippets.

@BBGuy
Created December 9, 2013 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BBGuy/7875084 to your computer and use it in GitHub Desktop.
Save BBGuy/7875084 to your computer and use it in GitHub Desktop.
Drupal Commerce Localization - translate the currency symbol
<?php
/**
* Implementation of hook_commerce_currency_info_alter().
*/
function MyModule_commerce_currency_info_alter(&$currencies, $langcode) {
// Add a curreny format callback so we can take over formating.
$currencies['RUB']['format_callback'] = 'MyModule_commerce_currency_format';
}
/**
* Currency format callback
*/
function MyModule_commerce_currency_format($amount, $currency, $object = NULL) {
// Get the language.
global $language;
$lang_name = $language->language ;
if ($lang_name != 'ru') {
// If not Russian we will use the international code for the symbol.
return MyModule_original_commerce_currency_format($amount, $currency['code'], 'RUB', $object, FALSE);
}
else {
// If is Russian we will use the Russian code for the symbol.
return MyModule_original_commerce_currency_format($amount, $currency['code'], 'руб', $object, FALSE);
}
}
/**
* Copy of commerce_currency_format from commerce.module
*
* The only changes are:
* 1) The removal of the format_callback line (commented out).
* 2) Option to set the $symbol instead of $currency['symbol'].
*/
function MyModule_original_commerce_currency_format($amount, $currency_code, $symbol, $object = NULL, $convert = TRUE) {
// First load the currency array.
$currency = commerce_currency_load($currency_code);
// Then convert the price amount to the currency's major unit decimal value.
if ($convert == TRUE) {
$amount = commerce_currency_amount_to_decimal($amount, $currency_code);
}
// Remove the custom format callback so we dont go into an endless loop.
// if (!empty($currency['format_callback'])) {
// return $currency['format_callback']($amount, $currency, $object);
// }
// Format the price as a number.
$price = number_format(commerce_currency_round(abs($amount), $currency), $currency['decimals'], $currency['decimal_separator'], $currency['thousands_separator']);
// Establish the replacement values to format this price for its currency.
$replacements = array(
'@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '',
'@symbol_before' => $currency['symbol_placement'] == 'before' ? $symbol : '',
'@price' => $price,
'@symbol_after' => $currency['symbol_placement'] == 'after' ? $symbol : '',
'@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '',
'@negative' => $amount < 0 ? '-' : '',
'@symbol_spacer' => $currency['symbol_spacer'],
'@code_spacer' => $currency['code_spacer'],
);
return trim(t('@code_before@code_spacer@negative@symbol_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment