Home / Archive / Change Currency Format By Locale
Duplicate Snippet

Embed Snippet on Your Site

Change Currency Format By Locale

This snippet below shows how to change the currency format for each language.

Code Preview
php
<?php
/**
 * By default, Charitable allows you to specify a single currency format
 * for the whole site. However, if you are running a multi-lingual
 * fundraising website, you may want to use a different format for each
 * language.
 *
 * The snippet below shows how to change the currency format for each
 * language.
 *
 * @param  string $formatted The amount formatted in the default currency format.
 * @param  float  $amount    The amount to be formatted.
 * @return string
 */
add_filter(
	'charitable_monetary_amount',
	function( $formatted, $amount ) {
		switch( get_locale() ) {
			case 'nl_NL':
			case 'fr_FR':
			case 'it_IT':
				$format = 'right'; // Other options include 'left', 'left-with-space' and 'right-with-space'.
				break;
			default:
				return $formatted;
		}
		$currency_helper = charitable_get_currency_helper();
		return sprintf(
			$currency_helper->get_currency_format( $format ),
			$currency_helper->get_currency_symbol(),
			$amount
		);
	},
	10,
	2
);

Comments

Add a Comment