i18nlint 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +206 -0
- data/exe/i18nlint +5 -0
- data/lib/i18nlint/cli.rb +131 -0
- data/lib/i18nlint/cloneable_struct.rb +14 -0
- data/lib/i18nlint/configuration.rb +120 -0
- data/lib/i18nlint/enumerator.rb +106 -0
- data/lib/i18nlint/error.rb +25 -0
- data/lib/i18nlint/highlighters/below_line.rb +71 -0
- data/lib/i18nlint/highlighters/colour.rb +38 -0
- data/lib/i18nlint/highlighters.rb +17 -0
- data/lib/i18nlint/linter.rb +158 -0
- data/lib/i18nlint/railtie.rb +19 -0
- data/lib/i18nlint/registry.rb +53 -0
- data/lib/i18nlint/rspec/expect_offence.rb +141 -0
- data/lib/i18nlint/rule.rb +128 -0
- data/lib/i18nlint/rule_helper.rb +97 -0
- data/lib/i18nlint/rules/built_in/duplicates.rb +49 -0
- data/lib/i18nlint/rules/built_in/interpolations.rb +59 -0
- data/lib/i18nlint/rules/built_in/match.rb +134 -0
- data/lib/i18nlint/version.rb +5 -0
- data/lib/i18nlint/yaml_with_lines.rb +254 -0
- data/lib/i18nlint.rb +40 -0
- metadata +102 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9a03e19f03f55ba5879300aca6350eefd4853c115f2147ba8e9ddf75fb2f2a21
|
|
4
|
+
data.tar.gz: a3f96e3e9edb03bc7ef0ae5b5dbeee5d1b2e2bef2f491312a15e1f58e509675f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d2ea838d73172ba085ad48459ded3c50c6cf61a7f5dcd11d845faadac5b0241038d43e64d9ff594723fe3574a003326a33fcc4a56afd2c8b4ca3a8d5d662bbfd
|
|
7
|
+
data.tar.gz: 2651d1a65b6fedf1ce26946dee33c991b610d206bdd997160ae3517d84e3b8345a67b5718cce0bafde003f41994350a9a4b3df3d95ec72ff1e361f7dfadb5a55
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Henry Blyth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# I18nLint
|
|
2
|
+
|
|
3
|
+
Lint your i18n for common problems, so you can be sure your copy doesn't get broken.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bundle add i18nlint --git='https://github.com/henrahmagix/i18nlint' --ref='0.1.0'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
git clone https://github.com/henrahmagix/i18nlint
|
|
17
|
+
cd i18nlint
|
|
18
|
+
gem build i18nlint.gemspec
|
|
19
|
+
gem install i18nlint i18nlint-0.1.0.gem
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Rails
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
bin/rails i18nlint
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
This task depends on the Rails task `:environment`, so your application will be loaded, including your I18n configuration, to detect your `I18n.default_locale`.
|
|
31
|
+
|
|
32
|
+
If there's a `.i18nlint.yml` config file in the root folder, it'll get loaded automatically.
|
|
33
|
+
|
|
34
|
+
### CLI
|
|
35
|
+
As a command-line tool with arguments:
|
|
36
|
+
```bash
|
|
37
|
+
i18nlint --source=en --config=lint.yml config/locales/*.yml
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Ruby
|
|
41
|
+
As a Ruby script:
|
|
42
|
+
```rb
|
|
43
|
+
require "i18nlint"
|
|
44
|
+
|
|
45
|
+
class MyRule < I18nLint::Rule
|
|
46
|
+
# Define at least one method of :on_file, :on_segment, or :on_segment_comparison.
|
|
47
|
+
end
|
|
48
|
+
I18nLint.register_rule(MyRule)
|
|
49
|
+
|
|
50
|
+
offences = I18nLint.lint("path/to/locales/*.{yml,rb,json}", source_locale: "en") # array of offences
|
|
51
|
+
|
|
52
|
+
# Each offence has the `rule` that added it, the `filepath`, `lineno`, and `text` of the segment or file, and an optional `message` string.
|
|
53
|
+
# Segment offences also have `locale` and their full `key`.
|
|
54
|
+
# It may also contain a `highlight` array of match offsets pointing to the exact location of the offence in `text`.
|
|
55
|
+
# Also if `source_offence` is not nil, it holds the same attributes but about the segment of the same key defined under the source locale.
|
|
56
|
+
offences.each do |o|
|
|
57
|
+
puts "#{o.rule} offence at #{o.filepath}:#{o.lineno}"
|
|
58
|
+
puts "#{o.key} in locale: #{o.locale}" unless o.locale && o.key
|
|
59
|
+
puts "compared to #{o.source_offence.key} in source #{o.source_offence.locale}" if o.source_offence
|
|
60
|
+
puts o.text, o.message
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Configuration
|
|
65
|
+
|
|
66
|
+
A YAML file named `.i18nlint.yml` is looked for automatically in the current directory and your home directory.
|
|
67
|
+
|
|
68
|
+
It can be used to add your own rules and configure them.
|
|
69
|
+
```yml
|
|
70
|
+
require:
|
|
71
|
+
- ./path/to/my/rule
|
|
72
|
+
- ./path/to/other/rule
|
|
73
|
+
|
|
74
|
+
BuiltIn/Interpolations:
|
|
75
|
+
Enabled: false # disable a built-in that always runs; all rules are enabled by default
|
|
76
|
+
|
|
77
|
+
MyRules/Amazing: # this matches a class named MyRules::Amazing, defined in any of the `require:` paths above
|
|
78
|
+
Enabled: true
|
|
79
|
+
Exclude: # every rule config can have a list of Pathname#fnmatch to exclude
|
|
80
|
+
- '*.exclude.yml'
|
|
81
|
+
|
|
82
|
+
match-segment:
|
|
83
|
+
# A list of `Pattern:` configurations to search for in each individual segment, where each match is an offence.
|
|
84
|
+
- Pattern: 'https://.*\.example.org'
|
|
85
|
+
- Pattern: 'https://.*\.bad.url'
|
|
86
|
+
Exclude:
|
|
87
|
+
- urls-*.yml
|
|
88
|
+
- Pattern: 'Word'
|
|
89
|
+
CaseSensitive: false # makes the Regexp made from Pattern case-insensitive with Regexp::IGNORECASE
|
|
90
|
+
|
|
91
|
+
match-file:
|
|
92
|
+
# Same as match-segment but passes in a whole file
|
|
93
|
+
- Pattern: something
|
|
94
|
+
|
|
95
|
+
mismatch-to-source:
|
|
96
|
+
# Same as match-segment but it matches on both translated segments and their
|
|
97
|
+
# source, and then compares the matches: if there are any that are in the
|
|
98
|
+
# source locale but not the translation, or vice versa, an offence is marked.
|
|
99
|
+
- Pattern: '\[[A-Z_]+\]' # some custom tags used in your renderer like `[NAME]`, `[EMAIL_ADDRESS]`, etc.
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Custom rules
|
|
103
|
+
|
|
104
|
+
You can define your own custom rules by defining classes extending `I18nLint::Rule`, and requiring them before I18nLint runs if you're using it in ruby, or linking them under `require:` in the configuration file if you're calling the binary. I18nLint will automatically register all of the subclasses of `I18nLint::Rule` that exist at runtime.
|
|
105
|
+
|
|
106
|
+
You must define at least one of `on_file`, `on_segment`, or `on_segment_comparison` instance methods. Your rule will get a noop definition for any that is missing.
|
|
107
|
+
- `on_file` receives a `I18nLint::File` that has:
|
|
108
|
+
- `filepath` string
|
|
109
|
+
- `parsed` Ruby value of the contents as loaded by I18n
|
|
110
|
+
- `raw` string contents of the file.
|
|
111
|
+
- `on_segment` receives a `I18nLint::Segment` that has:
|
|
112
|
+
- `file` (the `I18nLint::File` it's in)
|
|
113
|
+
- `filepath` delegated to `file.filepath`
|
|
114
|
+
- `lineno` integer of the start of the segment definition within the file
|
|
115
|
+
- `key` string of the full segment that you would use in `I18n.t()`
|
|
116
|
+
- `text` string content of the segment
|
|
117
|
+
- if `text` is nil, `value` will be the Ruby value of the segment (it could be an array... if you're putting arrays in your I18n 🤷)
|
|
118
|
+
- `locale` symbol of the segment (or string if i18n gem is < 1.9.1)
|
|
119
|
+
- `source_locale` string of the project source locale
|
|
120
|
+
- `source?` true if the locale is the project source
|
|
121
|
+
- `on_segment_comparison` receives two `I18nLint::Segment`s: the first is a translation segment, the second is the same segment in the source locale.
|
|
122
|
+
|
|
123
|
+
To register an offence, use:
|
|
124
|
+
- for `on_file` call `add_file_offence(file, msg = nil, lineno: nil, source: nil, highlight: nil)` where:
|
|
125
|
+
- `file` is the input argument to `on_file`
|
|
126
|
+
- `msg` will be displayed alongside the offence
|
|
127
|
+
- `lineno` is helpful to pass if the issue is isolated to or starts at a certain line in the file
|
|
128
|
+
- `source` should be the string on which the offence occurred, if any
|
|
129
|
+
- `highlight` can be passed as an array of tuples to indicate a range of the file content; if you pass this, you probably don't need to pass the `source`
|
|
130
|
+
- for `on_segment` call `add_segment_offence(segment, msg = nil, highlight: nil)` where:
|
|
131
|
+
- `segment` is the input argument to `on_segment`
|
|
132
|
+
- `msg` and `highlight` are the same as above
|
|
133
|
+
- for `on_segment_comparison` call `add_segment_compare_offence(segment, source_segment, msg = nil, src_msg = nil, highlight: nil, source_highlight: nil)` where:
|
|
134
|
+
- `segment` and `source_segment` are the input arguments to `on_segment_comparison`
|
|
135
|
+
- `msg` and `highlight` are the same as above
|
|
136
|
+
- `src_msg` and `source_highlight` are the same but attached to the source segment
|
|
137
|
+
|
|
138
|
+
If you're using Regexp in your rule, more often than not you can pass `highlight: Regexp.last_match.offset(0)` to the add-offence method.
|
|
139
|
+
|
|
140
|
+
There is a singleton method `on_init` available to add a block that gets evaluated on the rule instance after initialisation, so you can do some one-time setup of configuration values, available as `config`.
|
|
141
|
+
|
|
142
|
+
A rule can define a `description` instance or class method to describe the rule. Without it, the class name will be used with `::` replaced by `/`.
|
|
143
|
+
|
|
144
|
+
A message can be passed in per offence, or it will be taken automatically from a `Message` property on the rule configuration.
|
|
145
|
+
|
|
146
|
+
### Example
|
|
147
|
+
|
|
148
|
+
```rb
|
|
149
|
+
# lib/linters/i18n.rb
|
|
150
|
+
module MyI18nRules # the module namespace doesn't matter and is entirely optional
|
|
151
|
+
class DontAllowHelloAndGoodbyeInSameSentence < I18nLint::Rule
|
|
152
|
+
def on_segment(segment)
|
|
153
|
+
segment.text.downcase.scan(/[^.?!]*?[.?!]\s*/) do |sentence|
|
|
154
|
+
if sentence.include?('hello') && sentence.include?('goodbye')
|
|
155
|
+
add_segment_offence(segment, 'Thou Shalt Not greet and leave in the same sentence', highlight: Regexp.last_match.offset(0))
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
```
|
|
162
|
+
```yml
|
|
163
|
+
# content.yml
|
|
164
|
+
en:
|
|
165
|
+
welcome: "One two three. Hello and goodbye! One more finally."
|
|
166
|
+
fr:
|
|
167
|
+
welcome: "Un deux trois. Bonjour et au revoir! Encore."
|
|
168
|
+
```
|
|
169
|
+
```yml
|
|
170
|
+
# .i18nlint.yml
|
|
171
|
+
require:
|
|
172
|
+
- ./lib/linters/i18n.rb
|
|
173
|
+
```
|
|
174
|
+
```bash
|
|
175
|
+
$ i18nlint --source=en content.yml
|
|
176
|
+
Inspecting 1 files
|
|
177
|
+
F.
|
|
178
|
+
Comparing segments to source EN
|
|
179
|
+
.
|
|
180
|
+
|
|
181
|
+
Offences:
|
|
182
|
+
|
|
183
|
+
content.yml:2 in en.welcome: MyI18nRules/DontAllowHelloAndGoodbyeInSameSentence: Thou Shalt Not greet and leave in the same sentence
|
|
184
|
+
One two three. Hello and goodbye! One more finally.
|
|
185
|
+
^^^^^^^^^^^^^^^^^^^
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Development
|
|
189
|
+
|
|
190
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec appraisal rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
191
|
+
|
|
192
|
+
`bundle exec rake` also runs an example of the CLI tool, and RuboCop.
|
|
193
|
+
|
|
194
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
195
|
+
|
|
196
|
+
## Contributing
|
|
197
|
+
|
|
198
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/henrahmagix/i18nlint. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/henrahmagix/i18nlint/blob/main/CODE_OF_CONDUCT.md).
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
203
|
+
|
|
204
|
+
## Code of Conduct
|
|
205
|
+
|
|
206
|
+
Everyone interacting in the I18nLint project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/henrahmagix/i18nlint/blob/main/CODE_OF_CONDUCT.md).
|
data/exe/i18nlint
ADDED
data/lib/i18nlint/cli.rb
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "optparse"
|
|
4
|
+
require "yaml"
|
|
5
|
+
|
|
6
|
+
require "i18nlint"
|
|
7
|
+
|
|
8
|
+
module I18nLint
|
|
9
|
+
# Run the linter in your terminal.
|
|
10
|
+
class CLI
|
|
11
|
+
def self.run(...) = new.run(...)
|
|
12
|
+
|
|
13
|
+
def run(args = []) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity
|
|
14
|
+
conf = Configuration.new(args)
|
|
15
|
+
conf.on_problems { |e| warn e.message }
|
|
16
|
+
|
|
17
|
+
conf.load_from_argv_and_file!
|
|
18
|
+
|
|
19
|
+
if conf.source_locale.to_s.empty?
|
|
20
|
+
warn Configuration.help
|
|
21
|
+
exit 1
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if conf.config_file_does_not_exist
|
|
25
|
+
warn "Config file does not exist: #{conf.config_file_does_not_exist}"
|
|
26
|
+
warn Configuration.help
|
|
27
|
+
exit 1
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
conf.register_rules!
|
|
31
|
+
conf.remaining_rule_options.each_key do |key|
|
|
32
|
+
warn "Unused configuration #{key.inspect} expects class #{key.gsub("/", "::")} to subclass I18nLint::Rule. " \
|
|
33
|
+
"If this is a rule you're expecting to be used, that means it hasn't been loaded in the `require:` " \
|
|
34
|
+
"list, or it doesn't subclass I18nLint::Rule."
|
|
35
|
+
warn
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
linter = Linter.new(filepaths: conf.filepaths, source_locale: conf.source_locale)
|
|
39
|
+
|
|
40
|
+
if linter.num_files.zero?
|
|
41
|
+
warn "No files given"
|
|
42
|
+
warn Configuration.help
|
|
43
|
+
exit 0
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
if Registry.rules.empty?
|
|
47
|
+
warn "No rules configured"
|
|
48
|
+
warn Configuration.help
|
|
49
|
+
exit 0
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
tick = ->(num_offences) { print num_offences.zero? ? "." : "F" }
|
|
53
|
+
linter.tick_each_file(&tick)
|
|
54
|
+
linter.tick_each_comparison(&tick)
|
|
55
|
+
|
|
56
|
+
puts "Inspecting #{linter.num_files} files"
|
|
57
|
+
linter.run
|
|
58
|
+
puts "\nComparing segments to source #{conf.source_locale}"
|
|
59
|
+
linter.run_comparison
|
|
60
|
+
puts "\n\n"
|
|
61
|
+
|
|
62
|
+
if linter.offences.empty?
|
|
63
|
+
puts "No offences detected"
|
|
64
|
+
exit 0
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
puts "Offences:\n\n"
|
|
68
|
+
puts linter.offences.map { format_offence(_1) }.join("\n\n")
|
|
69
|
+
puts "\n#{linter.offences.size} offences detected"
|
|
70
|
+
exit 1
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
FILE_OFFENCE_DISPLAY = <<~MSG
|
|
74
|
+
%<filepath>s:%<lineno>s: %<rule>s: %<message>s
|
|
75
|
+
%<text_indented>s
|
|
76
|
+
MSG
|
|
77
|
+
|
|
78
|
+
SEGMENT_OFFENCE_DISPLAY = <<~MSG
|
|
79
|
+
%<filepath>s:%<lineno>s in %<locale>s.%<key>s: %<rule>s: %<message>s
|
|
80
|
+
%<text_indented>s
|
|
81
|
+
MSG
|
|
82
|
+
|
|
83
|
+
COMPARE_SEGMENT_OFFENCE_DISPLAY = <<~MSG # 🏳️⚧️🏳️🌈🫶
|
|
84
|
+
Comparison: %<rule>s
|
|
85
|
+
%<trans_filepath>s:%<trans_lineno>s in %<trans_locale>s.%<trans_key>s: %<trans_message>s
|
|
86
|
+
%<trans_text_indented>s
|
|
87
|
+
%<source_filepath>s:%<source_lineno>s in %<source_locale>s.%<source_key>s: %<source_message>s
|
|
88
|
+
%<source_text_indented>s
|
|
89
|
+
MSG
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
def format_offence(offence)
|
|
94
|
+
o = offence_hash_for_formatting(offence)
|
|
95
|
+
|
|
96
|
+
case offence
|
|
97
|
+
when FileOffence then FILE_OFFENCE_DISPLAY % o
|
|
98
|
+
when SegmentOffence then SEGMENT_OFFENCE_DISPLAY % o
|
|
99
|
+
when CompareSegmentOffence
|
|
100
|
+
source = offence_hash_for_formatting(offence.source_offence)
|
|
101
|
+
|
|
102
|
+
COMPARE_SEGMENT_OFFENCE_DISPLAY % rename_keys(o, 'trans_\0').merge(rename_keys(source, 'source_\0'))
|
|
103
|
+
end.gsub(/:? +$/, "").strip
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def offence_hash_for_formatting(offence)
|
|
107
|
+
o = offence.to_h
|
|
108
|
+
o[:text] = highlight(o[:text], offence.highlight)
|
|
109
|
+
indent(o)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def indent(hash)
|
|
113
|
+
hash.keys.each do |k|
|
|
114
|
+
hash[:"#{k}_indented"] = " #{hash[k].to_s.chomp.gsub("\n", "\n ")}"
|
|
115
|
+
end
|
|
116
|
+
hash
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def rename_keys(hash, gsub)
|
|
120
|
+
hash.transform_keys! { _1 == :rule ? _1 : _1.to_s.gsub(/^(.*)$/, gsub).to_sym }
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def highlight(text, range)
|
|
124
|
+
return text if range.nil? || range.to_a.empty?
|
|
125
|
+
|
|
126
|
+
range = [range] unless range[0].is_a?(Array)
|
|
127
|
+
|
|
128
|
+
Highlighters.indicate(text, *range)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module I18nLint
|
|
4
|
+
# Ensure cloning an instance also clones its members.
|
|
5
|
+
class CloneableStruct < Struct
|
|
6
|
+
def clone
|
|
7
|
+
super.tap do |new_obj|
|
|
8
|
+
self.class.members.each do |m|
|
|
9
|
+
new_obj.send("#{m}=", send(m).clone)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module I18nLint
|
|
4
|
+
# Central store of which locale is the source (only useful for comparative rules) and loading + registering rules.
|
|
5
|
+
class Configuration
|
|
6
|
+
def initialize(args, source_locale: "", rule_options: {}, requires: [])
|
|
7
|
+
@args = args
|
|
8
|
+
@source_locale = source_locale.upcase
|
|
9
|
+
@rule_options = rule_options
|
|
10
|
+
@requires = requires
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def load_from_argv_and_file!
|
|
14
|
+
opts = {}
|
|
15
|
+
self.class.parser.parse!(@args, into: opts)
|
|
16
|
+
|
|
17
|
+
@source_locale = opts[:source]&.upcase
|
|
18
|
+
@config_filepath = opts[:config]
|
|
19
|
+
|
|
20
|
+
@filepaths = @args # remaining after being mutated by `parser.parse!`
|
|
21
|
+
@rule_options.merge!(load_rule_options_from_file)
|
|
22
|
+
|
|
23
|
+
@remaining_rule_options = rule_options.dup
|
|
24
|
+
@requires += @remaining_rule_options.delete("require") || []
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def on_problems(&block)
|
|
28
|
+
@on_problems = block
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.help = parser.help
|
|
32
|
+
|
|
33
|
+
attr_reader :filepaths, :source_locale, :requires, :rule_options,
|
|
34
|
+
:config_file_does_not_exist, :remaining_rule_options
|
|
35
|
+
|
|
36
|
+
def register_rules!
|
|
37
|
+
require_relative_from = ::File.dirname(config_filepath || Dir.pwd)
|
|
38
|
+
requires.each do |path|
|
|
39
|
+
require ::File.expand_path(path, require_relative_from)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
register_built_ins
|
|
43
|
+
|
|
44
|
+
problems = register_rule_subclasses
|
|
45
|
+
problems.each { @on_problems.call(_1) } if @on_problems
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.parser
|
|
49
|
+
@parser ||= OptionParser.new do |parser|
|
|
50
|
+
parser.banner = "Usage: i18nlint files... --source=LOCALE --config=path/to/.i18nlint.yml"
|
|
51
|
+
|
|
52
|
+
parser.on("--source=LOCALE", "The locale to configure segment comparisons against source.")
|
|
53
|
+
parser.on("--config=CONFIG", "The configuration of rules.")
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def config_filepath
|
|
60
|
+
@config_filepath ||= [
|
|
61
|
+
::File.join(Dir.pwd, ".i18nlint.yml"),
|
|
62
|
+
::File.join(Dir.home, ".i18nlint.yml")
|
|
63
|
+
].find { |f| ::File.exist?(f) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def load_rule_options_from_file
|
|
67
|
+
return {} if config_filepath.nil?
|
|
68
|
+
|
|
69
|
+
::File.open(config_filepath, "r:bom|utf-8") do |f|
|
|
70
|
+
load_yaml(f, config_filepath) || {}
|
|
71
|
+
end
|
|
72
|
+
rescue ::Errno::ENOENT
|
|
73
|
+
@config_file_does_not_exist = config_filepath
|
|
74
|
+
{}
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
if Gem.loaded_specs["psych"].version < Gem::Version.create("3.1.0")
|
|
78
|
+
def load_yaml(stream, filepath)
|
|
79
|
+
# Args: whitelist_classes = [], whitelist_symbols = [], aliases = false, filename = nil
|
|
80
|
+
::YAML.safe_load(stream.read, [], [], false, filepath)
|
|
81
|
+
end
|
|
82
|
+
else
|
|
83
|
+
def load_yaml(stream, filepath)
|
|
84
|
+
::YAML.safe_load(stream.read, filename: filepath)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def find_built_in = proc { _1.name&.start_with?(Rules::BuiltIn.name) }
|
|
89
|
+
|
|
90
|
+
def register_built_ins
|
|
91
|
+
Rule.rule_classes.select(&find_built_in).each do |klass|
|
|
92
|
+
config = @remaining_rule_options.delete(klass.rule_key) || {}
|
|
93
|
+
config = [config] unless config.is_a? Array
|
|
94
|
+
config.each do |conf|
|
|
95
|
+
register_from_options(conf, klass)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def register_from_options(conf, klass)
|
|
101
|
+
enabled = conf.delete("Enabled")
|
|
102
|
+
enabled = klass.enabled_by_default? if enabled.nil?
|
|
103
|
+
Registry.register_rule(klass, conf) if enabled
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def register_rule_subclasses
|
|
107
|
+
Rule.rule_classes.reject(&find_built_in).filter_map do |klass|
|
|
108
|
+
conf = @remaining_rule_options.delete(klass.rule_key) if klass.rule_key
|
|
109
|
+
conf ||= {}
|
|
110
|
+
|
|
111
|
+
register_from_options(conf, klass)
|
|
112
|
+
nil
|
|
113
|
+
rescue Registry::WillNeverRun => e
|
|
114
|
+
e unless klass.rule_key.to_s.empty?
|
|
115
|
+
rescue Error => e
|
|
116
|
+
e
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|