2023-05-17 21:19:14 +02:00
|
|
|
import {get_strings} from 'core/str';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the translation of strings from a strings object
|
|
|
|
* @param {Object} strings The map of strings
|
|
|
|
* @returns {Object} The map with strings loaded in
|
|
|
|
*/
|
|
|
|
export function load_strings(strings){
|
|
|
|
for(let idx in strings){
|
|
|
|
let stringkeys = [];
|
2023-06-16 23:11:43 +02:00
|
|
|
for(const handle in strings[idx]){
|
|
|
|
const key = strings[idx][handle];
|
2023-05-17 21:19:14 +02:00
|
|
|
let parts = key.split("$");
|
|
|
|
let identifier = parts[0];
|
|
|
|
let component = (parts.length > 1)?parts[1]:'local_treestudyplan';
|
|
|
|
stringkeys.push({ key: identifier, component: component});
|
|
|
|
}
|
|
|
|
get_strings(stringkeys).then(function(str){
|
|
|
|
let i = 0;
|
|
|
|
for(const key in strings[idx]){
|
|
|
|
strings[idx][key] = str[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the translation of strings from a strings object based on keys
|
|
|
|
* Used for loading values for a drop down menu or the like
|
|
|
|
* @param {Object} string_keys The map of stringkeys
|
|
|
|
* @returns {Object} The map with strings loaded in
|
|
|
|
*/
|
|
|
|
export function load_stringkeys(string_keys){
|
|
|
|
for(let idx in string_keys){
|
|
|
|
// Get text strings for condition settings
|
|
|
|
let stringkeys = [];
|
|
|
|
for(const i in string_keys[idx]){
|
|
|
|
const key = string_keys[idx][i].textkey;
|
|
|
|
let parts = key.split("$");
|
|
|
|
let identifier = parts[0];
|
|
|
|
let component = (parts.length > 1)?parts[1]:'local_treestudyplan';
|
|
|
|
stringkeys.push({ key: identifier, component: component});
|
|
|
|
}
|
|
|
|
get_strings(stringkeys).then(function(strings){
|
|
|
|
for(const i in strings) {
|
|
|
|
const s = strings[i];
|
|
|
|
const l = string_keys[idx][i];
|
|
|
|
l.text = s;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return string_keys;
|
|
|
|
}
|
2023-07-28 23:42:57 +02:00
|
|
|
/**
|
|
|
|
* Format a date according to localized custom
|
|
|
|
* @param {Date|string} d The date to convert
|
|
|
|
* @param {boolean} short Short format (default false)
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function format_date(d,short){
|
|
|
|
if(!(d instanceof Date)){
|
|
|
|
d = new Date(d);
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-07-28 23:42:57 +02:00
|
|
|
let monthformat = "short";
|
|
|
|
if(short){
|
|
|
|
monthformat = "numeric";
|
|
|
|
}
|
|
|
|
return d.toLocaleDateString(document.documentElement.lang,{
|
|
|
|
year: 'numeric', month: monthformat, day: 'numeric'
|
|
|
|
});
|
|
|
|
}
|