Skip to content

Commit

Permalink
feat(locale): Maintain <html>s lang attribute
Browse files Browse the repository at this point in the history
Angular-localization will now update the `lang` attribute
of the root `html` tag to match the current locale's language

This closes #22 because users can now use the lang
attribute in their css to change the direction of the
document and/or add any other language specific css

Example:

```css
html[lang=ar] {
  direction: rtl;
}
```
  • Loading branch information
tolbahadi committed Aug 18, 2015
1 parent 1ce1284 commit db2c2d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ angular.module('ngLocalize')
.service('locale', function ($injector, $http, $q, $log, $rootScope, $window, localeConf, localeEvents, localeSupported, localeFallbacks) {
var TOKEN_REGEX = new RegExp('^[\\w\\.-]+\\.[\\w\\s\\.-]+\\w(:.*)?$'),

$html = angular.element(document.body).parent(),
currentLocale,
deferrences,
bundles,
Expand Down Expand Up @@ -227,6 +228,12 @@ angular.module('ngLocalize')
return result;
}

function updateHtmlTagLangAttr(lang) {
lang = lang.split('-')[0];

$html.attr('lang', lang);
}

function setLocale(value) {
var lang;

Expand All @@ -249,6 +256,8 @@ angular.module('ngLocalize')
deferrences = {};
currentLocale = lang;

updateHtmlTagLangAttr(lang);

$rootScope.$broadcast(localeEvents.localeChanges, currentLocale);
$rootScope.$broadcast(localeEvents.resourceUpdates);

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/serviceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ describe('service', function () {
it('should validate tokens with whitespace', inject(function (locale) {
expect(locale.isToken('test.hello world')).toBe(true);
}));

it('should update the lang attribute of the html tag', inject(function (locale) {
locale.setLocale('en-US');
expect(angular.element(document.body).parent().attr('lang')).toBe('en');
}));
});
});

0 comments on commit db2c2d2

Please sign in to comment.