tflori / angular-translator

translation module for angular
https://tflori.github.io/angular-translator/
MIT License
21 stars 6 forks source link

Extending TranslationLoaderJson #52

Closed saidrkc closed 7 years ago

saidrkc commented 7 years ago

Hi, I want to extend TranslationLoaderJson and use http service. But is private and can't use it.


import { TranslationLoader } from "../TranslationLoader";
import { Http } from "@angular/http";
export declare class TranslationLoaderJson extends TranslationLoader {
    private http;
    constructor(http: Http);
    load({language, module, path}: {
        language?: string;
        module?: string;
        path?: string;
    }): Promise<object>;
    private flattenTranslations(translations, data, prefix?);
}

Thanks

tflori commented 7 years ago

The flattenTranslations is a protected since 2.2.1 (yesterday). You should not require the Http to be protected:

export class MyTranslationLoader extends TranslationLoaderJson {
  constructor(private http: Http) {
    super(http);
  }
  public load(): Promise<object> {
    this.http....
  }
}

Or did I miss something?

saidrkc commented 7 years ago
@Injectable()
export class MyTranslationLoader extends TranslationLoaderJson {
    private translations;

   constructor(private http: Http) {
        super(http);
    }

    public load({language}: any): Promise<object> {
        const headers = new Headers();
        headers.append('responseType', 'arraybuffer');
        const file =  this.http.get( this.cdn + 'messages.es.json', headers)
            .map((response: Response) =>
                response.json())
            .catch((error: any) => Observable.throw(error.json()));

        file.subscribe((response: any) => {
            this.translations['es'] = response;
        });

        console.log(this.translations);
        if (!this.translations[language]) {
            Promise.reject('Language unknown');
        }
        return Promise.resolve(this.translations[language]);
    }
}

Error. Types have separate declarations of a private property 'http'

tflori commented 7 years ago

ok, that's bad, sorry. But you may want to extend TranslationLoader instead. You are not using any TranslationLoaderJson specific stuff.

https://github.com/tflori/angular-translator/blob/master/src/TranslationLoader/Json.ts

As you can see there is only a load method and you overwrite it.

tflori commented 7 years ago

as there are no more comments I assume this is solved.

feel free to reopen if there is another problem with the private http property.