Skip to content

Commit

Permalink
feat(TranslateService): Added support for Language Culture Name
Browse files Browse the repository at this point in the history
Fixes #293
  • Loading branch information
thorsten authored and ocombe committed Nov 2, 2016
1 parent 3a70f02 commit 2156cac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ In the html page:
- `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
- `getBrowserCultureLang(): string | undefined`: Returns the current browser culture language name (e.g. "de-DE" 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: 21 additions & 0 deletions src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ export class TranslateService {
this.translations[lang] = undefined;
}

/**
* Returns the language code name from the browser, e.g. "de"
*
* @returns string
*/
public getBrowserLang(): string {
if(typeof window === 'undefined' || typeof window.navigator === 'undefined') {
return undefined;
Expand All @@ -379,4 +384,20 @@ export class TranslateService {

return browserLang;
}

/**
* Returns the culture language code name from the browser, e.g. "de-DE"
*
* @returns string
*/
public getBrowserCultureLang(): string {
if (typeof window === 'undefined' || typeof window.navigator === 'undefined') {
return undefined;
}

let browserCultureLang: any = window.navigator.languages ? window.navigator.languages[0] : null;
browserCultureLang = browserCultureLang || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage;

return browserCultureLang;
}
}
6 changes: 6 additions & 0 deletions tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ describe('TranslateService', () => {
expect(browserLang).toBeDefined();
expect(typeof browserLang === 'string').toBeTruthy();
});

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

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

0 comments on commit 2156cac

Please sign in to comment.