Skip to content

Commit

Permalink
日期格式化,涉及:
Browse files Browse the repository at this point in the history
  • Loading branch information
cdmikechen committed Dec 16, 2019
1 parent 2096cc6 commit 701c844
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 15 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ werkzeug==0.15.5 # via flask, flask-jwt-extended
wtforms-json==0.3.3
wtforms==2.2.1 # via flask-wtf, wtforms-json
pydruid==0.5.7
requests==2.22.0
requests==2.22.0
flask_oauthlib==0.9.5
95 changes: 95 additions & 0 deletions superset/assets/src/d3TimeLocales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const d3Locale = {
"de-DE": {
"dateTime": "%A, der %e. %B %Y, %X",
"date": "%d.%m.%Y",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
"shortDays": ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
"months": ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
"shortMonths": ["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
},
"en-US": {
"dateTime": "%x, %X",
"date": "%-m/%-d/%Y",
"time": "%-I:%M:%S %p",
"periods": ["AM", "PM"],
"days": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
"shortDays": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
"months": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
"shortMonths": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
},
"fr-FR": {
"dateTime": "%A, le %e %B %Y, %X",
"date": "%d/%m/%Y",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"],
"shortDays": ["dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam."],
"months": ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
"shortMonths": ["janv.", "févr.", "mars", "avr.", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."]
},
"it-IT": {
"dateTime": "%A %e %B %Y, %X",
"date": "%d/%m/%Y",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"],
"shortDays": ["Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"],
"months": ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"],
"shortMonths": ["Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic"]
},
"ko-KR": {
"dateTime": "%Y/%m/%d %a %X",
"date": "%Y/%m/%d",
"time": "%H:%M:%S",
"periods": ["오전", "오후"],
"days": ["일요일", "월요일", "화요일", "수요일", "목요일", "금요일", "토요일"],
"shortDays": ["일", "월", "화", "수", "목", "금", "토"],
"months": ["1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월", "10월", "11월", "12월"],
"shortMonths": ["1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월", "10월", "11월", "12월"]
},
"ru-RU": {
"dateTime": "%A, %e %B %Y г. %X",
"date": "%d.%m.%Y",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["воскресенье", "понедельник", "вторник", "среда", "четверг", "пятница", "суббота"],
"shortDays": ["вс", "пн", "вт", "ср", "чт", "пт", "сб"],
"months": ["января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря"],
"shortMonths": ["янв", "фев", "мар", "апр", "май", "июн", "июл", "авг", "сен", "окт", "ноя", "дек"]
},
"zh-CN": {
"dateTime": "%x %A %X",
"date": "%Y年%-m月%-d日",
"time": "%H:%M:%S",
"periods": ["上午", "下午"],
"days": ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
"shortDays": ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
"months": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
"shortMonths": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
},
"ja-JP": {
"dateTime": "%Y %b %e %a %X",
"date": "%Y/%m/%d",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"],
"shortDays": ["日", "月", "火", "水", "木", "金", "土"],
"months": ["睦月", "如月", "弥生", "卯月", "皐月", "水無月", "文月", "葉月", "長月", "神無月", "霜月", "師走"],
"shortMonths": ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
},
"pt-BR":
{
"dateTime": "%A, %e de %B de %Y. %X",
"date": "%d/%m/%Y",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"],
"shortDays": ["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb"],
"months": ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
"shortMonths": ["Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"]
}
};

export default d3Locale;
1 change: 1 addition & 0 deletions superset/assets/src/explore/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const D3_FORMAT_OPTIONS = [
['.1s', '.1s (12345.432 => 10k)'],
['.3s', '.3s (12345.432 => 12.3k)'],
['.3z', '.3z (12345.432 => 1.23万)'],
['.4z', '.4z (12345.432 => 1.234万)'],
[',.1%', ',.1% (12345.432 => 1,234,543.2%)'],
['.3%', '.3% (12345.432 => 1234543.200%)'],
['.4r', '.4r (12345.432 => 12350)'],
Expand Down
3 changes: 2 additions & 1 deletion superset/assets/src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import moment from 'moment';
import { configure } from '@superset-ui/translation';
import setupClient from './setup/setupClient';
import setupColors from './setup/setupColors';
import setupFormatters from './setup/setupFormatters';
import {default as setupFormatters, setupTimeFormatters} from './setup/setupFormatters';

// Configure translation
if (typeof window !== 'undefined') {
Expand All @@ -31,6 +31,7 @@ if (typeof window !== 'undefined') {
const languagePack = bootstrapData.common.language_pack;
configure({ languagePack });
moment.locale(bootstrapData.common.moment_locale);
setupTimeFormatters(bootstrapData.common.time_locale)
} else {
configure();
}
Expand Down
12 changes: 9 additions & 3 deletions superset/assets/src/setup/setupFormatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* under the License.
*/
import { createDurationFormatter, getNumberFormatter, getNumberFormatterRegistry, NumberFormats } from '@superset-ui/number-format';
import { getTimeFormatterRegistry, smartDateFormatter, smartDateVerboseFormatter } from '@superset-ui/time-format';
import { getTimeFormatterRegistry, getSmartDateFormatter, getSmartDateVerboseFormatter, setLocaleDef } from '@superset-ui/time-format';
import {default as d3Locales} from '../d3TimeLocales';

export default function setupFormatters() {
getNumberFormatterRegistry()
Expand Down Expand Up @@ -57,9 +58,14 @@ export default function setupFormatters() {
.registerValue('$,.f', getNumberFormatter('$,.4f'))
.registerValue('DURATION', createDurationFormatter())
.registerValue('DURATION_SUB', createDurationFormatter({ formatSubMilliseconds: true }));
}

export function setupTimeFormatters(timeLocaleKey) {
// 设置时区
setLocaleDef(d3Locales[timeLocaleKey]);
// 注册日期转化函数
getTimeFormatterRegistry()
.registerValue('smart_date', smartDateFormatter)
.registerValue('smart_date_verbose', smartDateVerboseFormatter)
.registerValue('smart_date', getSmartDateFormatter())
.registerValue('smart_date_verbose', getSmartDateVerboseFormatter())
.setDefaultKey('smart_date');
}
20 changes: 10 additions & 10 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,16 @@ def _try_json_readfile(filepath):
BABEL_DEFAULT_FOLDER = "superset/translations"
# The allowed translation for you app
LANGUAGES = {
"en": {"flag": "us", "name": "English", "moment_locale": "en", "humanize_locale": "en"},
"it": {"flag": "it", "name": "Italian", "moment_locale": "it", "humanize_locale": "en"},
"fr": {"flag": "fr", "name": "French", "moment_locale": "fr", "humanize_locale": "fr_FR"},
"zh": {"flag": "cn", "name": "Chinese", "moment_locale": "zh-cn", "humanize_locale": "zh_CN"},
"ja": {"flag": "jp", "name": "Japanese", "moment_locale": "ja", "humanize_locale": "en"},
"de": {"flag": "de", "name": "German", "moment_locale": "de", "humanize_locale": "de_DE"},
"pt": {"flag": "pt", "name": "Portuguese", "moment_locale": "pt", "humanize_locale": "en"},
"pt_BR": {"flag": "br", "name": "Brazilian Portuguese", "moment_locale": "pt-br", "humanize_locale": "pt_BR"},
"ru": {"flag": "ru", "name": "Russian", "moment_locale": "ru", "humanize_locale": "ru_RU"},
"ko": {"flag": "kr", "name": "Korean", "moment_locale": "ko", "humanize_locale": "ko_KR"},
"en": {"flag": "us", "name": "English", "moment_locale": "en", "humanize_locale": "en", "time_locale": "en-US"},
"it": {"flag": "it", "name": "Italian", "moment_locale": "it", "humanize_locale": "en", "time_locale": "it-IT"},
"fr": {"flag": "fr", "name": "French", "moment_locale": "fr", "humanize_locale": "fr_FR", "time_locale": "fr-FR"},
"zh": {"flag": "cn", "name": "Chinese", "moment_locale": "zh-cn", "humanize_locale": "zh_CN", "time_locale": "zh-CN"},
"ja": {"flag": "jp", "name": "Japanese", "moment_locale": "ja", "humanize_locale": "en", "time_locale": "ja-JP"},
"de": {"flag": "de", "name": "German", "moment_locale": "de", "humanize_locale": "de_DE", "time_locale": "de-DE"},
"pt": {"flag": "pt", "name": "Portuguese", "moment_locale": "pt", "humanize_locale": "en", "time_locale": "en-US"},
"pt_BR": {"flag": "br", "name": "Brazilian Portuguese", "moment_locale": "pt-br", "humanize_locale": "pt_BR", "time_locale": "pt-BR"},
"ru": {"flag": "ru", "name": "Russian", "moment_locale": "ru", "humanize_locale": "ru_RU", "time_locale": "ru-RU"},
"ko": {"flag": "kr", "name": "Korean", "moment_locale": "ko", "humanize_locale": "ko_KR", "time_locale": "ko-KR"},
}

# ---------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def common_bootstrap_payload():
"conf": {k: conf.get(k) for k in FRONTEND_CONF_KEYS},
"locale": locale,
"moment_locale": conf.get("LANGUAGES").get(locale).get("moment_locale"),
"time_locale": conf.get("LANGUAGES").get(locale).get("time_locale"),
"language_pack": get_language_pack(locale),
"feature_flags": get_feature_flags(),
}
Expand Down

0 comments on commit 701c844

Please sign in to comment.