upfix localstorage iframe

This commit is contained in:
_Bastler 2022-03-20 11:21:25 +01:00
parent 578e6e6981
commit 5a9caf69a5
6 changed files with 68 additions and 37 deletions

View File

@ -17,9 +17,11 @@ export class AppComponent {
ngOnInit() { ngOnInit() {
this._adapter.setLocale(this.i18n.getLocale()); this._adapter.setLocale(this.i18n.getLocale());
try {
if (localStorage.getItem("bstly.darkTheme") == "true") { if (localStorage.getItem("bstly.darkTheme") == "true") {
window.document.body.classList.add("dark-theme"); window.document.body.classList.add("dark-theme");
} }
} catch { }
} }
} }

View File

@ -69,7 +69,10 @@ import { JukeboxComponent } from './pages/jukebox/jukebox.compontent';
export function init_app(i18n: I18nService) { export function init_app(i18n: I18nService) {
return () => i18n.fetch().then(response => { }, error => { }); return () => {
console.log("APP_INITIALIZER");
i18n.fetch();
};
} }
@Injectable() @Injectable()

View File

@ -55,18 +55,22 @@ export class AuthenticatedGuard implements CanActivate {
for (let profileField of profileFields) { for (let profileField of profileFields) {
if (profileField.name == "darkTheme") { if (profileField.name == "darkTheme") {
darktheme = profileField.value; darktheme = profileField.value;
} else if (profileField.name == "locale" && this.i18nService.locales.indexOf(profileField.value) != -1 && localStorage.getItem("bstly.locale") != profileField.value) { } else if (profileField.name == "locale" && this.i18nService.locales.indexOf(profileField.value) != -1) {
if (this.i18nService.locale != profileField.value) { try {
if (localStorage.getItem("bstly.locale") != profileField.value || this.i18nService.locale != profileField.value) {
localStorage.setItem("bstly.locale", profileField.value); localStorage.setItem("bstly.locale", profileField.value);
updateLocale = true; updateLocale = true;
} }
} catch { }
} }
} }
try {
if (darktheme != localStorage.getItem("bstly.darkTheme")) { if (darktheme != localStorage.getItem("bstly.darkTheme")) {
localStorage.setItem("bstly.darkTheme", darktheme); localStorage.setItem("bstly.darkTheme", darktheme);
updateTheme = true; updateTheme = true;
} }
} catch { }
if (updateLocale || updateTheme) { if (updateLocale || updateTheme) {
window.location.reload(); window.location.reload();

View File

@ -18,7 +18,10 @@ export class ServicesComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
this.view = localStorage.getItem("bstly.servicesView") || 'grid'; this.view = 'grid';
try {
this.view = localStorage.getItem("bstly.servicesView") || this.view;
} catch { }
this.serviceService.services().subscribe({ this.serviceService.services().subscribe({
next: (data: Array<any>) => { next: (data: Array<any>) => {
@ -39,7 +42,9 @@ export class ServicesComponent implements OnInit {
} }
updateView(): void { updateView(): void {
try {
localStorage.setItem("bstly.servicesView", this.view); localStorage.setItem("bstly.servicesView", this.view);
} catch { }
} }
} }

View File

@ -37,31 +37,43 @@ export class I18nService {
browserLocale = browserLocale.split("-")[ 0 ]; browserLocale = browserLocale.split("-")[ 0 ];
} }
let locale = localStorage.getItem("bstly.locale") || browserLocale || this.locales[ 0 ]; let locale = browserLocale || this.locales[ 0 ];
try {
locale = localStorage.getItem("bstly.locale") || locale;
} catch { }
if (locale == 'de') { if (locale == 'de') {
locale = 'de-informal'; locale = 'de-informal';
} }
try { await this.http.get(environment.apiUrl + "/i18n").subscribe({
await this.http.get(environment.apiUrl + "/i18n").toPromise().then((response: any) => { next: (data: any) => {
this.locales = response; console.log("fetch3", data);
}); this.locales = data;
} catch (e) { }, error: (error) => {
console.debug("fallback to default locales"); console.debug("fallback to default locales");
} }
});
if (this.locales.indexOf(locale) == -1) { if (this.locales.indexOf(locale) == -1) {
locale = this.locales[ 0 ]; locale = this.locales[ 0 ];
} }
this.setLocale(locale); this.setLocale(locale);
try { await this.http.get(environment.apiUrl + "/i18n/" + locale).subscribe({
this.i18n = await this.http.get(environment.apiUrl + "/i18n/" + locale).toPromise(); next: (data: any) => {
} catch (e) { this.i18n = data;
this.i18n = await this.http.get("/assets/i18n/" + locale + ".json").toPromise(); }, error: async (error) => {
console.debug("fallback to default locale"); await this.http.get("/assets/i18n/" + locale + ".json").subscribe({
next: (data: any) => {
this.i18n = data;
}, error: (error) => {
} }
});
}
});
} }
get(key, args: string[]): string { get(key, args: string[]): string {

View File

@ -58,17 +58,20 @@ export class MainComponent {
} else { } else {
this.opened = true; this.opened = true;
} }
try {
if (localStorage.getItem("bstly.darkTheme") == "true") { if (localStorage.getItem("bstly.darkTheme") == "true") {
this.darkTheme = "true"; this.darkTheme = "true";
window.document.body.classList.add("dark-theme"); window.document.body.classList.add("dark-theme");
} }
} catch { }
this.touchEvents(); this.touchEvents();
} }
setLocale(locale) { setLocale(locale) {
try {
localStorage.setItem("bstly.locale", locale); localStorage.setItem("bstly.locale", locale);
} catch { }
if (this.auth && this.auth.authenticated) { if (this.auth && this.auth.authenticated) {
this.profileService.getField("locale").subscribe({ this.profileService.getField("locale").subscribe({
@ -102,7 +105,9 @@ export class MainComponent {
this.darkTheme = "false"; this.darkTheme = "false";
} }
try {
localStorage.setItem("bstly.darkTheme", this.darkTheme); localStorage.setItem("bstly.darkTheme", this.darkTheme);
} catch { }
if (this.auth && this.auth.authenticated) { if (this.auth && this.auth.authenticated) {
this.profileService.getField("darkTheme").subscribe({ this.profileService.getField("darkTheme").subscribe({