Skip to content

Commit

Permalink
Add english locale with 24h format
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Nov 9, 2020
1 parent 193d076 commit 2f1a4d6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
5 changes: 3 additions & 2 deletions model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const (
ThemeMaterialDark = "MaterialDark"
ThemeMaterialLight = "MaterialLight"

DateLocaleGerman = "German"
DateLocaleEnglish = "English"
DateLocaleGerman = "German"
DateLocaleEnglish = "English"
DateLocaleEnglish24h = "English24h"
)

var daysOfWeek = map[string]time.Weekday{
Expand Down
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ enum Theme {
}

enum DateLocale {
English, German
English, English24h, German
}

input InputReplaceOptions {
Expand Down
2 changes: 1 addition & 1 deletion setting/usersettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func toExternal(internal model.UserSetting) *gqlmodel.UserSettings {

func toInternalDateLocale(locale gqlmodel.DateLocale) string {
switch locale.String() {
case model.DateLocaleEnglish, model.DateLocaleGerman:
case model.DateLocaleEnglish, model.DateLocaleGerman, model.DateLocaleEnglish24h:
return locale.String()
default:
return model.DateLocaleEnglish
Expand Down
31 changes: 22 additions & 9 deletions ui/src/provider/UserSettingsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import * as React from 'react';
import {useSettings} from '../gql/settings';
import {CenteredSpinner} from '../common/CenteredSpinner';
import moment from 'moment';
import moment, {LocaleSpecification} from 'moment';
import {DateLocale, WeekDay} from '../gql/__generated__/globalTypes';
import {expectNever} from '../utils/never';

const setLocale = (locale: DateLocale) => {
const setLocale = (locale: DateLocale, spec: LocaleSpecification) => {
switch (locale) {
case DateLocale.English:
moment.locale('en');
return true;
moment.locale('en', spec);
return;
case DateLocale.English24h:
moment.locale('en', {
...spec,
longDateFormat: {
LTS: 'HH:mm:ss',
LT: 'HH:mm',
L: 'MM/DD/YYYY',
LL: 'MMMM D, YYYY',
LLL: 'MMMM D, YYYY HH:mm',
LLLL: 'dddd, MMMM D, YYYY HH:mm',
},
});
return;
case DateLocale.German:
moment.locale('de');
return true;
moment.locale('de', spec);
return;
default:
return expectNever(locale);
expectNever(locale);
return;
}
};

Expand Down Expand Up @@ -46,8 +60,7 @@ export const BootUserSettings: React.FC = ({children}): React.ReactElement => {
if (!done) {
return;
}
setLocale(dateLocale);
moment.updateLocale(moment.locale(), {
setLocale(dateLocale, {
week: {
dow: weekDayToMoment(firstDayOfTheWeek),
doy: moment.localeData(moment.locale()).firstDayOfYear(),
Expand Down

0 comments on commit 2f1a4d6

Please sign in to comment.