Jump to content

TextformatterCurrency


netcarver
 Share

Recommended Posts

TextformatterCurrency

A locale-aware currency formatter module for ProcessWire.

The module reads a formatted string, splits it and formats each valid monetary amount according to the locale defined in the module settings. The module is capable of using the visiting browser's headers to determine which locale to use when formatting monetary amounts of various currencies.

Internally, the module uses PHPs Internationalization extension (intl) to do the formatting.

Input money format

The input money format is made up of any number of currency-amount pairs.

Each input money pair is made up of 3-letter ISO 4217 currency code and a numeric amount. These are always separated by at least one space with the code coming first. (eg. "GBP 50.99" or "JPY 100000")

  • No thousands separators are allowed in the input.
  • A full-stop '.' character is the only acceptable decimal separator.
Each money pair is separated by a configurable character string ('|' by default.) You can change the character used to split the incoming string into currency-amount pairs in the module settings page.

Output format

Each currency-amount pair is formatted using the defined locale or that supplied by the browser and thus appears localised in the output of the formatter. If the input text has more than one currency-amount money pair then the formatter will join the localised outputs for each pair using the output glue string defined in the module settings.

Negative amounts are prettified by substituting ascii '-' characters with the more typographically pleasing minus glyph ''.

Examples Using Module Defaults

Input "GBP 50.99 | EUR 100" gives "£50.99 + €100.00" when viewed in browser set to British English but gives "50,99 £GB + 100,00 €" using a browser set to French.

Availability

On github.

In the module repository, here.

post-465-0-38756900-1463317661_thumb.png

  • Like 8
Link to comment
Share on other sites

Just spotted this in a post by Ryan (here);

My understanding is that Google advises against modifying the output of the request based on the accept_language header, and thus could be an seo concern. 

Although my own use-case for this module is for an invoicing system that is not publicly accessible - I can see that this might be the case if this is used on public facing PW pages. Unless anyone can suggest a better way, I'll look at adding a method to allow the locale to be set after the module is loaded (either from a template file or perhaps ready.php.) This should allow a site to control its own URL-to-locale mapping and still use this module as a textformatter if desired.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...