From 2b27718d8644c132f7f88c7b37d8a76184aa295b Mon Sep 17 00:00:00 2001 From: fbaligand Date: Sat, 19 Aug 2017 08:45:53 +0200 Subject: [PATCH] add support for number format internationalization --- .../kibana/common/field_formats/types/_numeral.js | 12 +++++++++++- src/core_plugins/kibana/ui_setting_defaults.js | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/core_plugins/kibana/common/field_formats/types/_numeral.js b/src/core_plugins/kibana/common/field_formats/types/_numeral.js index a042c4a1c72af..5bc00eea28f6c 100644 --- a/src/core_plugins/kibana/common/field_formats/types/_numeral.js +++ b/src/core_plugins/kibana/common/field_formats/types/_numeral.js @@ -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; @@ -11,7 +16,6 @@ export function createNumeralFormat(FieldFormat, opts) { constructor(params, getConfig) { super(params); - this.getConfig = getConfig; } @@ -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; diff --git a/src/core_plugins/kibana/ui_setting_defaults.js b/src/core_plugins/kibana/ui_setting_defaults.js index 46cd81f1a234c..783bd0e4696fa 100644 --- a/src/core_plugins/kibana/ui_setting_defaults.js +++ b/src/core_plugins/kibana/ui_setting_defaults.js @@ -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 { @@ -237,6 +241,12 @@ export function getUiSettingDefaults() { value: '($0,0.[00])', description: 'Default numeral format for the "currency" format' }, + 'format:number:defaultLocale': { + value: 'en', + type: 'select', + options: numeralLanguageIds, + description: 'numeral language' + }, 'savedObjects:perPage': { type: 'number', value: 5,