Skip to content

Commit

Permalink
Add a way for the developer to specify the locale
Browse files Browse the repository at this point in the history
  • Loading branch information
saevarom committed Aug 6, 2024
1 parent 6175357 commit f0201d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ window.barchart_labels = function() {
}
```

### Localize number formatting according to a specific locale
Wagtail chart formats numbers according to the browser's default locale by default.
You can override this by setting a global javascript variable called `WAGTAILCHARTS_LOCALE` before calling the `render_charts` template tag:

```django
{% load wagtailcharts_tags %}
<!-- Your content here -->
{% block extra_js %}
<script>WAGTAILCHARTS_LOCALE = 'is-IS';</script>
{% render_charts %}
{% endblock %}
```


## Dependencies
* This project relies on [Jspreadsheet Community Edition](https://bossanova.uk/jspreadsheet/v4/) for data entry and manipulation.
Expand Down
7 changes: 5 additions & 2 deletions wagtailcharts/static/wagtailcharts/js/wagtailcharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ var formatValue = function (val, precision = 1, override) {
return val;
}

// Passing 'undefined' for the locale uses the browser's default.
number = Intl.NumberFormat(undefined, {maximumFractionDigits: precision}).format(val)
// We look for the locale in the window object, which must be set before the `render_charts` template tag is called.
// If it is not present, it will be undefined and the browser's default locale will be used.
let locale = window.WAGTAILCHARTS_LOCALE;

number = Intl.NumberFormat(locale, {maximumFractionDigits: precision}).format(val)

if(override !== undefined && override !== "" && override !== null){
return number + " " + override;
Expand Down

0 comments on commit f0201d8

Please sign in to comment.