2019-09-26 12:43:29 +02:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
2017-10-30 16:40:50 +01:00
|
|
|
//
|
|
|
|
// 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 { Injectable } from '@angular/core';
|
2018-07-11 13:01:56 +02:00
|
|
|
import { Globalization, GlobalizationOptions } from '@ionic-native/globalization';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreAppProvider } from '@providers/app';
|
2017-10-30 16:40:50 +01:00
|
|
|
|
2018-07-11 13:01:56 +02:00
|
|
|
/**
|
|
|
|
* Mock the Globalization Error.
|
|
|
|
*/
|
|
|
|
export class GlobalizationErrorMock implements GlobalizationError {
|
|
|
|
static UNKNOWN_ERROR = 0;
|
|
|
|
static FORMATTING_ERROR = 1;
|
|
|
|
static PARSING_ERROR = 2;
|
|
|
|
static PATTERN_ERROR = 3;
|
|
|
|
|
|
|
|
constructor(public code: number, public message: string) { }
|
|
|
|
}
|
|
|
|
|
2017-10-30 16:40:50 +01:00
|
|
|
/**
|
|
|
|
* Emulates the Cordova Globalization plugin in desktop apps and in browser.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class GlobalizationMock extends Globalization {
|
|
|
|
|
|
|
|
constructor(private appProvider: CoreAppProvider) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:01:56 +02:00
|
|
|
/**
|
|
|
|
* Converts date to string.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param date Date you wish to convert
|
2018-07-11 13:01:56 +02:00
|
|
|
* @param options Options for the converted date. Length, selector.
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Returns a promise when the date has been converted.
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
dateToString(date: Date, options: GlobalizationOptions): Promise<{ value: string; }> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a pattern string to format and parse currency values according to the client's user preferences and ISO 4217
|
|
|
|
* currency code.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param currencyCode Currency Code.
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
getCurrencyPattern(currencyCode: string): Promise<any> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
2017-10-30 16:40:50 +01:00
|
|
|
/**
|
|
|
|
* Get the current locale.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Locale name.
|
2017-10-30 16:40:50 +01:00
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
private getCurrentlocale(): string {
|
2017-10-30 16:40:50 +01:00
|
|
|
// Get browser language.
|
2018-01-29 10:05:20 +01:00
|
|
|
const navLang = (<any> navigator).userLanguage || navigator.language;
|
2017-10-30 16:40:50 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (this.appProvider.isDesktop()) {
|
2018-01-29 10:05:20 +01:00
|
|
|
return require('electron').remote.app.getLocale() || navLang;
|
2017-10-30 16:40:50 +01:00
|
|
|
} else {
|
|
|
|
return navLang;
|
|
|
|
}
|
2018-01-29 10:05:20 +01:00
|
|
|
} catch (ex) {
|
2017-10-30 16:40:50 +01:00
|
|
|
// Something went wrong, return browser language.
|
|
|
|
return navLang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:01:56 +02:00
|
|
|
/**
|
|
|
|
* Returns an array of the names of the months or days of the week, depending on the client's user preferences and calendar.
|
|
|
|
*
|
|
|
|
* @param options Object with type (narrow or wide) and item (month or days).
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Returns a promise.
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
getDateNames(options: { type: string; item: string; }): Promise<{ value: Array<string>; }> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a pattern string to format and parse dates according to the client's user preferences.
|
|
|
|
*
|
|
|
|
* @param options Object with the format length and selector
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Returns a promise.
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
getDatePattern(options: GlobalizationOptions): Promise<any> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first day of the week according to the client's user preferences and calendar.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return returns a promise with the value
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
getFirstDayOfWeek(): Promise<{ value: string; }> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
2017-10-30 16:40:50 +01:00
|
|
|
/**
|
|
|
|
* Get the current locale name.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Promise resolved with an object with the language string.
|
2017-10-30 16:40:50 +01:00
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
getLocaleName(): Promise<{ value: string }> {
|
|
|
|
const locale = this.getCurrentlocale();
|
2017-10-30 16:40:50 +01:00
|
|
|
if (locale) {
|
2018-01-29 10:05:20 +01:00
|
|
|
return Promise.resolve({ value: locale });
|
2017-10-30 16:40:50 +01:00
|
|
|
} else {
|
2018-07-11 13:01:56 +02:00
|
|
|
const error = new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Cannot get language');
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-10-30 16:40:50 +01:00
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:01:56 +02:00
|
|
|
/**
|
|
|
|
* Returns a pattern string to format and parse numbers according to the client's user preferences.
|
|
|
|
* @param options Can be decimal, percent, or currency.
|
|
|
|
*/
|
|
|
|
getNumberPattern(options: { type: string; }): Promise<any> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
2017-10-30 16:40:50 +01:00
|
|
|
/*
|
|
|
|
* Get the current preferred language.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Promise resolved with an object with the language string.
|
2017-10-30 16:40:50 +01:00
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
getPreferredLanguage(): Promise<{ value: string }> {
|
2017-10-30 16:40:50 +01:00
|
|
|
return this.getLocaleName();
|
|
|
|
}
|
2018-07-11 13:01:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether daylight savings time is in effect for a given date using the client's time zone and calendar.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param date Date to process.
|
|
|
|
* @return reutrns a promise with the value
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
isDayLightSavingsTime(date: Date): Promise<{ dst: string; }> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a number formatted as a string according to the client's user preferences.
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param numberToConvert The number to convert
|
|
|
|
* @param options Object with property `type` that can be set to: decimal, percent, or currency.
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
numberToString(numberToConvert: number, options: { type: string; }): Promise<{ value: string; }> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a date formatted as a string, according to the client's user preferences and calendar using the time zone of the
|
|
|
|
* client, and returns the corresponding date object.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param dateString Date as a string to be converted
|
2018-07-11 13:01:56 +02:00
|
|
|
* @param options Options for the converted date. Length, selector.
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Returns a promise when the date has been converted.
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
stringToDate(dateString: string, options: GlobalizationOptions): Promise<any> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param stringToConvert String you want to conver to a number.
|
2018-07-11 13:01:56 +02:00
|
|
|
*
|
|
|
|
* @param options The type of number you want to return. Can be decimal, percent, or currency.
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Returns a promise with the value.
|
2018-07-11 13:01:56 +02:00
|
|
|
*/
|
|
|
|
stringToNumber(stringToConvert: string, options: { type: string; }): Promise<{ value: number | string; }> {
|
|
|
|
return Promise.reject(new GlobalizationErrorMock(GlobalizationErrorMock.UNKNOWN_ERROR, 'Not supported.'));
|
|
|
|
}
|
2017-10-30 16:40:50 +01:00
|
|
|
}
|