MOBILE-3947 ion-datetime: Use right language and first day of week

main
Dani Palou 2023-12-15 10:42:31 +01:00
parent 50b8fe2e98
commit df0cf232f6
6 changed files with 89 additions and 0 deletions

View File

@ -1792,6 +1792,7 @@
"core.fileuploader.uploadingperc": "local_moodlemobileapp",
"core.fileuploader.video": "local_moodlemobileapp",
"core.filter": "moodle",
"core.firstdayofweek": "langconfig",
"core.folder": "moodle",
"core.forcepasswordchangenotice": "moodle",
"core.fulllistofcourses": "moodle",

View File

@ -153,6 +153,9 @@ export class CoreConstants {
static readonly MOD_ARCHETYPE_ASSIGNMENT = 2; // Assignment module archetype.
static readonly MOD_ARCHETYPE_SYSTEM = 3; // System (not user-addable) module archetype.
// Other constants.
static readonly CALENDAR_DEFAULT_STARTING_WEEKDAY = 1;
// Config & environment constants.
static readonly CONFIG = { ...envJson.config } as unknown as EnvironmentConfig; // Data parsed from config.json files.
static readonly BUILD = envJson.build as unknown as EnvironmentBuild; // Build info.

View File

@ -0,0 +1,62 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Directive, OnInit } from '@angular/core';
import { CoreLang } from '@services/lang';
import { CoreUser } from '@features/user/services/user';
import { IonDatetime } from '@ionic/angular';
/**
* Directive to automatically add language and starting week day to ion-datetime.
*/
@Directive({
selector: 'ion-datetime',
})
export class CoreIonDatetimeDirective implements OnInit {
constructor(protected datetime: IonDatetime) {}
/**
* @inheritdoc
*/
ngOnInit(): void {
this.setLanguage();
this.setStartingWeekDay();
}
/**
* Set language to use.
*/
protected async setLanguage(): Promise<void> {
if (this.datetime.locale) {
return;
}
const language = await CoreLang.getCurrentLanguage();
this.datetime.locale = language;
}
/**
* Set starting week day.
*/
protected async setStartingWeekDay(): Promise<void> {
if (this.datetime.firstDayOfWeek || this.datetime.firstDayOfWeek === 0) {
return;
}
const startingDay = await CoreUser.getStartingWeekDay();
this.datetime.firstDayOfWeek = startingDay;
}
}

View File

@ -34,6 +34,7 @@ import { CoreCollapsibleFooterDirective } from './collapsible-footer';
import { CoreContentDirective } from './content';
import { CoreUpdateNonReactiveAttributesDirective } from './update-non-reactive-attributes';
import { CoreUserTourDirective } from './user-tour';
import { CoreIonDatetimeDirective } from './datetime';
@NgModule({
declarations: [
@ -57,6 +58,7 @@ import { CoreUserTourDirective } from './user-tour';
CoreContentDirective,
CoreUpdateNonReactiveAttributesDirective,
CoreUserTourDirective,
CoreIonDatetimeDirective,
],
exports: [
CoreAutoFocusDirective,
@ -79,6 +81,7 @@ import { CoreUserTourDirective } from './user-tour';
CoreContentDirective,
CoreUpdateNonReactiveAttributesDirective,
CoreUserTourDirective,
CoreIonDatetimeDirective,
],
})
export class CoreDirectivesModule {}

View File

@ -29,6 +29,7 @@ import { USERS_TABLE_NAME, CoreUserDBRecord } from './database/user';
import { CoreUserHelper } from './user-helper';
import { CoreUrlUtils } from '@services/utils/url';
import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site';
import { CoreConstants } from '@/core/constants';
const ROOT_CACHE_KEY = 'mmUser:';
@ -285,6 +286,24 @@ export class CoreUserProvider {
}
}
/**
* Get the starting week day based on the user preference.
*
* @returns Starting week day.
*/
async getStartingWeekDay(): Promise<number> {
const preference = await CoreUtils.ignoreErrors(this.getUserPreference('calendar_startwday'));
if (preference && !isNaN(Number(preference))) {
return Number(preference);
}
const defaultValue = Number(CoreSites.getCurrentSite()?.getStoredConfig('calendar_startwday') ??
Translate.instant('core.firstdayofweek'));
return !isNaN(defaultValue) ? defaultValue % 7 : CoreConstants.CALENDAR_DEFAULT_STARTING_WEEKDAY;
}
/**
* Get cache key for a user WS call.
*

View File

@ -131,6 +131,7 @@
"filenameexist": "File name already exists: {{$a}}",
"filenotfound": "File not found, sorry.",
"filter": "Filter",
"firstdayofweek": "1",
"folder": "Folder",
"forcepasswordchangenotice": "You must change your password to proceed.",
"fulllistofcourses": "All courses",