Skip to content

Commit

Permalink
add support for number format internationalization
Browse files Browse the repository at this point in the history
  • Loading branch information
fbaligand committed Feb 3, 2018
1 parent 3db3dea commit 2b27718
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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 2b27718

Please sign in to comment.