How to maintain an efficient AOT Angular app in Firebase hosting?

I have an angular 4 application hosted by a Firebase hosting service with a custom domain (for example: www.something.com, which redirects the firebase URL). I used i18n internationalization, and my question is: What is the best way to serve the right compiled application depending on the user nation? At this point, there is only the standard --aot en online, and I would like to service this version if someone comes from Italy, for example.

thank

+4
source share
2 answers

, . , . , .

, , :

, . root!

function _window(): any {
  // return the global native browser window object
  return window;
}

@Injectable()
export class WindowRef {
  get nativeWindow(): any {
    return _window();
  }

  public ln = 'en'; //default language


  constructor() {
    try {
      if (!isNullOrUndefined(this.nativeWindow.navigator.language) && this.nativeWindow.navigator.language !== '') {
        let lang = this.nativeWindow.navigator.language;
        if (lang.indexOf('-') >= 0) {
          lang = lang.split('-')[0]; //replace en-us with en.
        }
        this.ln = lang;
      }
    } finally {
    }
  }
}

defaut angular :

@Pipe({name: 'datepipe', pure: true})
export class MyDatePipe extends DatePipe implements PipeTransform {
  constructor(private win: WindowRef) {
    super(win.ln);
  }

  transform(value: any, pattern?: string): string | null {
    return super.transform(value, pattern);
  }
}

:

@Pipe({name: 'translate'})
export class TranslationPipe implements PipeTransform {
  constructor(private translationService: TranslationService) {

  }

  transform(value: string) {
    return this.translationService.getTranslation(value);
  }
}

:

@Injectable()
export class TranslationService {
  lng: string = 'en';

  nl: Array<KV> = [
    {key: 'Hello', value: 'Hallo'},
  ];

  languages = [
    {key: 'nl', value: this.nl}

  ];

  constructor() {
    if (!isNullOrUndefined(navigator.language)) {
      this.lng = navigator.language;
    }
    if (this.lng.indexOf('-') >= 0) {
      this.lng = this.lng.split('-')[0];
    }
  }

  getTranslation(key: string): string {
    return this.findByKey(this.findByKey(this.languages, this.lng), key) || key;
  }

  private findByKey(list: { key, value }[], key: string): any {
    if (!isNullOrUndefined(list)) {
      for (let i = 0; i < list.length; i++) {
        if (list[i].key === key) {
          return list[i].value;
        }
      }
    }
    return undefined;
  }
}

export class KV {
  key: string;
  value: string;
}

,

+2

. , firebase.json

{
  "hosting": {
    "public": "dist",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "redirects": [
      {
        "source" : "/",
        "destination" : "/en",
        "type" : 301
      }
    ],
    "rewrites": [
      {
        "source": "/en/**",
        "destination": "/en/index.html"
      },
      {
        "source": "/es/**",
        "destination": "/es/index.html"
      },
      {
        "source": "/fr/**",
        "destination": "/fr/index.html"
      }
    ]
  }
}

, !

+1

Source: https://habr.com/ru/post/1690643/


All Articles