Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ Publicly exposed events may be found as part of `localeEvents` situated within t

- `localeEvents.resourceUpdates`: when a new language file is pulled into memory
- `localeEvents.localeChanges`: when the user chooses a different locale
- `localeEvents.textUpdates`: when an element's text is set to its translation

__NOTE:__ The `localeEvents.textUpdates` is only sent for substitutions triggered by the `i18n` and `i18nAttr` directives.

```js
angular.module('myApp', [
Expand All @@ -185,6 +188,9 @@ angular.module('myApp', [
$scope.$on(localeEvents.localeChanges, function (event, data) {
console.log('new locale chosen: ' + data);
});
$scope.$on(localeEvents.textUpdates, function (event, element) {
console.log('translation set for: ' + element);
});
}
]);
```
Expand Down
5 changes: 3 additions & 2 deletions src/localization.events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
angular.module('ngLocalize.Events', [])
.constant('localeEvents', {
resourceUpdates: 'ngLocalizeResourcesUpdated',
localeChanges: 'ngLocalizeLocaleChanged'
});
localeChanges: 'ngLocalizeLocaleChanged',
textUpdates: 'ngLocalizeTextUpdated'
});
14 changes: 9 additions & 5 deletions src/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,14 @@ angular.module('ngLocalize', ['ngSanitize', 'ngLocalize.Config', 'ngLocalize.Eve
};
}
])
.directive('i18n', ['$sce', 'locale', 'localeEvents', 'localeConf',
function ($sce, locale, localeEvents, localeConf) {
.directive('i18n', ['$sce', '$rootScope', 'locale', 'localeEvents', 'localeConf',
function ($sce, $rootScope, locale, localeEvents, localeConf) {
function setText(elm, tag) {
if (tag !== elm.html()) {
elm.html($sce.getTrustedHtml(tag));
}

$rootScope.$broadcast(localeEvents.textUpdates, elm);
}

function update(elm, string, optArgs) {
Expand Down Expand Up @@ -315,8 +317,8 @@ angular.module('ngLocalize', ['ngSanitize', 'ngLocalize.Config', 'ngLocalize.Eve
};
}
])
.directive('i18nAttr', ['locale', 'localeEvents',
function (locale, localeEvents) {
.directive('i18nAttr', ['$rootScope', 'locale', 'localeEvents',
function ($rootScope, locale, localeEvents) {
return function (scope, elem, attrs) {
var lastValues = {};

Expand All @@ -342,12 +344,14 @@ angular.module('ngLocalize', ['ngSanitize', 'ngLocalize.Config', 'ngLocalize.Eve
attrs.$set(key, lastValues[key] = value);
}
}

$rootScope.$broadcast(localeEvents.textUpdates, target);
});
}

attrs.$observe('i18nAttr', function (newVal, oldVal) {
if (newVal && newVal != oldVal) {
updateText(elem, newVal);
updateText(elem, newVal);
}
});

Expand Down