Skip to content

Commit

Permalink
feat(TranslateService): adding new getBrowserLang method
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Aug 13, 2016
1 parent 7dceb34 commit e24bfa4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Or use the `TranslatePipe` in any template:
- `set(key: string, value: string, lang?: string)`: Sets the translated value of a key
- `reloadLang(lang: string): Observable<string|Object>`: Calls resetLang and retrieves the translations object for the current loader
- `resetLang(lang: string)`: Removes the current translations for this lang. /!\ You will have to call `use`, `reloadLang` or `getTranslation` again to be able to get translations
- `getBrowserLang(): string | undefined`: Returns the current browser lang if available, or undefined otherwise

#### Write & use your own loader
If you want to write your own loader, you need to create a class that implements `TranslateLoader`.
Expand Down
21 changes: 20 additions & 1 deletion src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface LangChangeEvent {
translations: any;
}

declare interface Window {
navigator: any;
}
declare var window: Window;

export abstract class MissingTranslationHandler {
/**
* A function that handles missing translations.
Expand Down Expand Up @@ -114,7 +119,7 @@ export class TranslateService {

if (typeof pending !== "undefined") {
// on init set the currentLang immediately
if(!this.currentLang) {
if (!this.currentLang) {
this.currentLang = lang;
}
pending.subscribe((res: any) => {
Expand Down Expand Up @@ -343,4 +348,18 @@ export class TranslateService {
public resetLang(lang: string): void {
this.translations[lang] = undefined;
}

public getBrowserLang(): string | undefined {
if (typeof window === 'undefined' || typeof window.navigator === 'undefined') {
return undefined;
}
let browserLang: any;
if (typeof window.navigator['languages'] !== 'undefined' && window.navigator['languages'].length > 0) {
browserLang = window.navigator['languages'][0].indexOf('-') !== -1 || window.navigator['languages'].length < 2 ? window.navigator['languages'][0] : window.navigator['languages'][1];
} else {
browserLang = window.navigator['language'] || window.navigator['browserLanguage'];
}

return browserLang && browserLang.length ? browserLang.split('-')[0] : undefined; // use navigator lang if available
}
}
6 changes: 6 additions & 0 deletions tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ export function main() {

expect(translate.getLangs()).toEqual(['en', 'pl']);
});

it('should be able to get the browserLang', () => {
let browserLang = translate.getBrowserLang();
expect(browserLang).toBeDefined();
expect(typeof browserLang === 'string').toBeTruthy();
});
});

describe('MissingTranslationHandler', () => {
Expand Down

0 comments on commit e24bfa4

Please sign in to comment.