Skip to content

Commit

Permalink
add support for number format internationalization (#14019)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbaligand authored and spalger committed Nov 22, 2017
1 parent ef609ae commit 0420cd7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"dependencies": {
"@elastic/datemath": "2.3.0",
"@elastic/filesaver": "1.1.2",
"@elastic/numeral": "2.2.1",
"@elastic/numeral": "2.3.0",
"@elastic/test-subj-selector": "0.2.1",
"@elastic/ui-ace": "0.2.3",
"JSONStream": "1.1.1",
Expand Down
12 changes: 11 additions & 1 deletion src/core_plugins/kibana/common/field_formats/types/_numeral.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import _ from 'lodash';
import numeral from '@elastic/numeral';
import numeralLanguages from '@elastic/numeral/languages';

const numeralInst = numeral();

numeralLanguages.forEach(function (numeralLanguage) {
numeral.language(numeralLanguage.id, numeralLanguage.lang);
});

export function createNumeralFormat(FieldFormat, opts) {
class NumeralFormat extends FieldFormat {
static id = opts.id;
Expand All @@ -11,7 +16,6 @@ export function createNumeralFormat(FieldFormat, opts) {

constructor(params, getConfig) {
super(params);

this.getConfig = getConfig;
}

Expand All @@ -34,8 +38,14 @@ export function createNumeralFormat(FieldFormat, opts) {

if (isNaN(val)) return '';

const previousLocale = numeral.language();
const defaultLocale = this.getConfig && this.getConfig('format:number:defaultLocale') || 'en';
numeral.language(defaultLocale);

const formatted = numeralInst.set(val).format(this.param('pattern'));

numeral.language(previousLocale);

return opts.afterConvert
? opts.afterConvert.call(this, formatted)
: formatted;
Expand Down
10 changes: 10 additions & 0 deletions src/core_plugins/kibana/ui_setting_defaults.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import moment from 'moment-timezone';
import numeralLanguages from '@elastic/numeral/languages';

export function getUiSettingDefaults() {
const weekdays = moment.weekdays().slice();
const [defaultWeekday] = weekdays;
const numeralLanguageIds = numeralLanguages.map(function (numeralLanguage) {
return numeralLanguage.id;
});

// wrapped in provider so that a new instance is given to each app/test
return {
Expand Down Expand Up @@ -237,6 +241,12 @@ export function getUiSettingDefaults() {
value: '($0,0.[00])',
description: 'Default <a href="http://numeraljs.com/" target="_blank" rel="noopener noreferrer">numeral format</a> for the "currency" format'
},
'format:number:defaultLocale': {
value: 'en',
type: 'select',
options: numeralLanguageIds,
description: '<a href="http://numeraljs.com/" target="_blank" rel="noopener">numeral language</a>'
},
'savedObjects:perPage': {
type: 'number',
value: 5,
Expand Down

0 comments on commit 0420cd7

Please sign in to comment.