0
\$\begingroup\$

I have a simple Angular Service that pre-configures translations for the ngx-translate library based on the currently logged in business case.

I'm not sure if my switch case statement within the if/else block can be written more effective / performant. I currently have no other ideas to improve the code.

Can the code be better and more clearly structured here?

import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import 'moment/locale/de';
import { UserAuthService } from 'app/auth/user-auth.service';

@Injectable()
export class ConfigurationService {

languages = {
    en: 'English',
    de: 'German',
    es: 'Spanish',
    ja: 'Japanese',
    pt: 'Portuguese'
};

// Business cases with available translation files
businessCases = {
    case1: '_case1',
    case2: '_case2',
    case3: '_case3',
    case4: '_case4',
    case5: '_case5',
    case6: '_case6'
};

defaultLanguage = 'en';

constructor(
    private translate: TranslateService,
    private authService: UserAuthService
) {

}
// Get business case specific language
// TODO: Check current business case (maybe in bcase-required.guard.ts)
getAutoConfiguredBCLanguage(){
    const browserLang = this.translate.getBrowserLang();
    const activeBusinessCase = this.authService.activeBusinessCase;
    console.log('Browser language: ', browserLang);
    console.log('Active Businesscase: ', activeBusinessCase);
    if (this.languages.hasOwnProperty(browserLang) && activeBusinessCase) {
    switch (activeBusinessCase) {
        case 'CASE1':
        return browserLang.concat(this.businessCases.case1);
        case 'CASE2':
        return browserLang.concat(this.businessCases.case2);
        case 'CASE3':
        return browserLang.concat(this.businessCases.case3);
        case 'CASE4':
        return browserLang.concat(this.businessCases.case4);
        case 'CASE5':
        return browserLang.concat(this.businessCases.case5);
        case 'CASE6':
        return browserLang.concat(this.businessCases.case6);
        default:
        return browserLang;
    }
    } else if (this.languages.hasOwnProperty(browserLang) && !activeBusinessCase) {
    console.log('No currently active business case.');
    return browserLang;
    } else {
    return this.defaultLanguage;
    }
}

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.