|
1 | 1 | angular.module("umbraco.directives")
|
2 | 2 |
|
3 |
| - /** |
4 |
| - * @ngdoc directive |
5 |
| - * @name umbraco.directives.directive:localize |
6 |
| - * @restrict EA |
7 |
| - * @function |
8 |
| - * @description |
9 |
| - * <div> |
10 |
| - * <strong>Component</strong><br /> |
11 |
| - * Localize a specific token to put into the HTML as an item |
12 |
| - * </div> |
13 |
| - * <div> |
14 |
| - * <strong>Attribute</strong><br /> |
15 |
| - * Add a HTML attribute to an element containing the HTML attribute name you wish to localise |
16 |
| - * Using the format of '@section_key' or 'section_key' |
17 |
| - * </div> |
18 |
| - * ##Usage |
19 |
| - * <pre> |
20 |
| - * <!-- Component --> |
21 |
| - * <localize key="general_close">Close</localize> |
22 |
| - * <localize key="section_key">Fallback value</localize> |
23 |
| - * |
24 |
| - * <!-- Attribute --> |
25 |
| - * <input type="text" localize="placeholder" placeholder="@placeholders_entername" /> |
26 |
| - * <input type="text" localize="placeholder,title" title="@section_key" placeholder="@placeholders_entername" /> |
27 |
| - * <div localize="title" title="@section_key"></div> |
28 |
| - * </pre> |
29 |
| - **/ |
30 |
| - .directive('localize', function ($log, localizationService) { |
31 |
| - return { |
32 |
| - restrict: 'E', |
33 |
| - scope: { |
34 |
| - key: '@', |
35 |
| - tokens: '=', |
36 |
| - watchTokens: '@' |
37 |
| - }, |
38 |
| - replace: true, |
| 3 | +/** |
| 4 | +* @ngdoc directive |
| 5 | +* @name umbraco.directives.directive:localize |
| 6 | +* @restrict EA |
| 7 | +* @function |
| 8 | +* @description |
| 9 | +* <div> |
| 10 | +* Used to localize text in HTMl-elements or attributes using translation keys. Translations are stored in umbraco/config/lang/ or the /lang-folder of a package i App_Plugins. |
| 11 | +* </div> |
| 12 | +* <div> |
| 13 | +* <strong>Component/Element</strong><br /> |
| 14 | +* Localize using a component to put the localized text into the HTML. |
| 15 | +* </div> |
| 16 | +* <div> |
| 17 | +* <strong>Attribute</strong><br /> |
| 18 | +* Add a HTML attribute to an element containing the HTML attribute name you wish to localise |
| 19 | +* Using the format of '@section_key' or 'section_key' |
| 20 | +* </div> |
| 21 | +* ##Basic Usage |
| 22 | +* <pre> |
| 23 | +* <!-- Component --> |
| 24 | +* <localize key="general_close">Close</localize> |
| 25 | +* <localize key="section_key">Fallback value</localize> |
| 26 | +* |
| 27 | +* <!-- Attribute --> |
| 28 | +* <input type="text" localize="placeholder" placeholder="@placeholders_entername" /> |
| 29 | +* <input type="text" localize="placeholder,title" title="@section_key" placeholder="@placeholders_entername" /> |
| 30 | +* <div localize="title" title="@section_key"></div> |
| 31 | +* </pre> |
| 32 | +* ##Use with tokens |
| 33 | +* Also supports tokens inside the translation key, example of a translation |
| 34 | +* <pre> |
| 35 | +* <key alias="characters_left">You have %0% characters left of %1%</key> |
| 36 | +* </pre> |
| 37 | +* Can be used like this: |
| 38 | +* <pre> |
| 39 | +* <localize key="general_characters_left" tokens="[5,100]" watch-tokens="true">You have %0% characters left of %1%</localize> |
| 40 | +* <!-- Renders: You have 5 characters left of 100 --> |
| 41 | +* </pre> |
| 42 | +* Where the "tokens"-attribute is an array of tokens for the translations, "watch-tokens" will make the component watch the expression passed. |
| 43 | +**/ |
| 44 | +.directive('localize', function ($log, localizationService) { |
| 45 | + return { |
| 46 | + restrict: 'E', |
| 47 | + scope: { |
| 48 | + key: '@', |
| 49 | + tokens: '=', |
| 50 | + watchTokens: '@' |
| 51 | + }, |
| 52 | + replace: true, |
39 | 53 |
|
40 |
| - link: function (scope, element, attrs) { |
41 |
| - var key = scope.key; |
42 |
| - scope.text = ""; |
| 54 | + link: function (scope, element, attrs) { |
| 55 | + var key = scope.key; |
| 56 | + scope.text = ""; |
43 | 57 |
|
44 |
| - // A render function to be able to update tokens as values update. |
45 |
| - function render() { |
46 |
| - element.html(localizationService.tokenReplace(scope.text, scope.tokens || null)); |
47 |
| - } |
| 58 | + // A render function to be able to update tokens as values update. |
| 59 | + function render() { |
| 60 | + element.html(localizationService.tokenReplace(scope.text, scope.tokens || null)); |
| 61 | + } |
48 | 62 |
|
49 |
| - localizationService.localize(key).then(function (value) { |
50 |
| - scope.text = value; |
51 |
| - render(); |
52 |
| - }); |
53 |
| - if (scope.watchTokens === 'true') { |
54 |
| - scope.$watch("tokens", render, true); |
55 |
| - } |
| 63 | + localizationService.localize(key).then(function (value) { |
| 64 | + scope.text = value; |
| 65 | + render(); |
| 66 | + }); |
| 67 | + if (scope.watchTokens === 'true') { |
| 68 | + scope.$watch("tokens", render, true); |
56 | 69 | }
|
57 |
| - }; |
58 |
| - }) |
| 70 | + } |
| 71 | + }; |
| 72 | +}) |
59 | 73 |
|
60 |
| - .directive('localize', function ($log, localizationService) { |
61 |
| - return { |
62 |
| - restrict: 'A', |
63 |
| - link: function (scope, element, attrs) { |
64 |
| - //Support one or more attribute properties to update |
65 |
| - var keys = attrs.localize.split(','); |
| 74 | +.directive('localize', function ($log, localizationService) { |
| 75 | + return { |
| 76 | + restrict: 'A', |
| 77 | + link: function (scope, element, attrs) { |
| 78 | + //Support one or more attribute properties to update |
| 79 | + var keys = attrs.localize.split(','); |
66 | 80 |
|
67 |
| - Utilities.forEach(keys, (value, key) => { |
68 |
| - var attr = element.attr(value); |
| 81 | + Utilities.forEach(keys, (value, key) => { |
| 82 | + var attr = element.attr(value); |
69 | 83 |
|
70 |
| - if (attr) { |
71 |
| - if (attr[0] === '@') { |
72 |
| - //If the translation key starts with @ then remove it |
73 |
| - attr = attr.substring(1); |
74 |
| - } |
| 84 | + if (attr) { |
| 85 | + if (attr[0] === '@') { |
| 86 | + //If the translation key starts with @ then remove it |
| 87 | + attr = attr.substring(1); |
| 88 | + } |
75 | 89 |
|
76 |
| - var t = localizationService.tokenize(attr, scope); |
| 90 | + var t = localizationService.tokenize(attr, scope); |
77 | 91 |
|
78 |
| - localizationService.localize(t.key, t.tokens).then(function (val) { |
79 |
| - element.attr(value, val); |
80 |
| - }); |
81 |
| - } |
82 |
| - }); |
83 |
| - } |
84 |
| - }; |
| 92 | + localizationService.localize(t.key, t.tokens).then(function (val) { |
| 93 | + element.attr(value, val); |
| 94 | + }); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + }; |
85 | 99 |
|
86 |
| - }); |
| 100 | +}); |
0 commit comments