|
| 1 | +# ember/template-no-bare-strings |
| 2 | + |
| 3 | +<!-- end auto-generated rule header --> |
| 4 | + |
| 5 | +Disallows bare strings in templates to encourage internationalization. |
| 6 | + |
| 7 | +Bare strings in templates make internationalization (i18n) difficult. This rule encourages using translation helpers or properties to enable easy localization of your application. |
| 8 | + |
| 9 | +## Rule Details |
| 10 | + |
| 11 | +This rule disallows text content in templates that isn't wrapped in a translation helper or passed as a property. |
| 12 | + |
| 13 | +The following are allowed: |
| 14 | + |
| 15 | +- Whitespace-only strings |
| 16 | +- Strings in the default allowlist (punctuation characters like `(`, `)`, `.`, `&`, etc.) |
| 17 | +- Strings in a custom allowlist (configurable) |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +Examples of **incorrect** code for this rule: |
| 22 | + |
| 23 | +```gjs |
| 24 | +<template> |
| 25 | + <div>Hello World</div> |
| 26 | +</template> |
| 27 | +``` |
| 28 | + |
| 29 | +```gjs |
| 30 | +<template> |
| 31 | + <button>Click me</button> |
| 32 | +</template> |
| 33 | +``` |
| 34 | + |
| 35 | +```gjs |
| 36 | +<template> |
| 37 | + <h1>Welcome to our app</h1> |
| 38 | +</template> |
| 39 | +``` |
| 40 | + |
| 41 | +Examples of **correct** code for this rule: |
| 42 | + |
| 43 | +```gjs |
| 44 | +<template> |
| 45 | + <div>{{t "hello.world"}}</div> |
| 46 | +</template> |
| 47 | +``` |
| 48 | + |
| 49 | +```gjs |
| 50 | +<template> |
| 51 | + <button>{{@buttonText}}</button> |
| 52 | +</template> |
| 53 | +``` |
| 54 | + |
| 55 | +```gjs |
| 56 | +<template> |
| 57 | + <div> </div> |
| 58 | +</template> |
| 59 | +``` |
| 60 | + |
| 61 | +## Configuration |
| 62 | + |
| 63 | +### `allowlist` |
| 64 | + |
| 65 | +An array of strings that are allowed to appear as bare strings: |
| 66 | + |
| 67 | +```js |
| 68 | +module.exports = { |
| 69 | + rules: { |
| 70 | + 'ember/template-no-bare-strings': [ |
| 71 | + 'error', |
| 72 | + { |
| 73 | + allowlist: ['Welcome', 'Home', 'About'], |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | +}; |
| 78 | +``` |
| 79 | + |
| 80 | +### `globalAttributes` |
| 81 | + |
| 82 | +An array of attribute names where bare strings will be checked globally on all elements (defaults to `["title", "aria-label", "aria-placeholder", "aria-roledescription", "aria-valuetext"]`): |
| 83 | + |
| 84 | +```js |
| 85 | +module.exports = { |
| 86 | + rules: { |
| 87 | + 'ember/template-no-bare-strings': [ |
| 88 | + 'error', |
| 89 | + { |
| 90 | + globalAttributes: [ |
| 91 | + 'title', |
| 92 | + 'aria-label', |
| 93 | + 'aria-placeholder', |
| 94 | + 'aria-roledescription', |
| 95 | + 'aria-valuetext', |
| 96 | + ], |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | +### `elementAttributes` |
| 104 | + |
| 105 | +An object mapping element names to arrays of attribute names to check for bare strings (defaults to `{ input: ["placeholder"], img: ["alt"] }`). The built-in Ember components `Input` and `Textarea` also check `placeholder` and `@placeholder`: |
| 106 | + |
| 107 | +```js |
| 108 | +module.exports = { |
| 109 | + rules: { |
| 110 | + 'ember/template-no-bare-strings': [ |
| 111 | + 'error', |
| 112 | + { |
| 113 | + elementAttributes: { |
| 114 | + input: ['placeholder'], |
| 115 | + img: ['alt'], |
| 116 | + }, |
| 117 | + }, |
| 118 | + ], |
| 119 | + }, |
| 120 | +}; |
| 121 | +``` |
| 122 | + |
| 123 | +### `ignoredElements` |
| 124 | + |
| 125 | +An array of element names whose text content is ignored (defaults to `["pre", "script", "style", "textarea"]`): |
| 126 | + |
| 127 | +```js |
| 128 | +module.exports = { |
| 129 | + rules: { |
| 130 | + 'ember/template-no-bare-strings': [ |
| 131 | + 'error', |
| 132 | + { |
| 133 | + ignoredElements: ['pre', 'script', 'style', 'textarea'], |
| 134 | + }, |
| 135 | + ], |
| 136 | + }, |
| 137 | +}; |
| 138 | +``` |
| 139 | + |
| 140 | +## References |
| 141 | + |
| 142 | +- [eslint-plugin-ember template-no-bare-strings](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-bare-strings.md) |
| 143 | +- [Ember Intl](https://github.com/ember-intl/ember-intl) |
0 commit comments