Skip to content

Commit e60f2c8

Browse files
authored
Merge pull request #145 from castusdesign/main
[Config]: Adding "language" as a setting
2 parents a554e52 + 1796b91 commit e60f2c8

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,25 @@ In order to make it easier to get started with Peck, we've included a few preset
107107

108108
- `laravel`
109109

110+
### Languages
111+
112+
While by default `peck` verfies the spelling by using GNU Aspell's `en_US` language dictionary, you can optionally specify a different language to be passed using the "language" key in the configuration;
113+
114+
```json
115+
{
116+
"preset": "laravel",
117+
"language": "en_GB-ise",
118+
"ignore": {
119+
"words": [
120+
"config",
121+
"namespace"
122+
]
123+
}
124+
}
125+
```
126+
127+
You can see a full list of available dictionaries using the command `aspell dump dicts`.
128+
110129
## Command Options
111130

112131
The behaviour of `peck` can be modified with the following command options:

src/Config.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ final class Config
1515
*/
1616
private const JSON_CONFIGURATION_NAME = 'peck.json';
1717

18+
/**
19+
* The default language passed to Aspell.
20+
*/
21+
private const DEFAULT_LANGUAGE = 'en_US';
22+
1823
/**
1924
* The instance of the configuration.
2025
*/
@@ -35,6 +40,7 @@ public function __construct(
3540
public array $whitelistedWords = [],
3641
public array $whitelistedPaths = [],
3742
public ?string $preset = null,
43+
public ?string $language = null,
3844
) {
3945
$this->whitelistedWords = array_map(strtolower(...), $whitelistedWords);
4046
}
@@ -87,6 +93,7 @@ public static function instance(): self
8793
/**
8894
* @var array{
8995
* preset?: string,
96+
* language?: string,
9097
* ignore?: array{
9198
* words?: array<int, string>,
9299
* paths?: array<int, string>
@@ -99,6 +106,7 @@ public static function instance(): self
99106
$jsonAsArray['ignore']['words'] ?? [],
100107
$jsonAsArray['ignore']['paths'] ?? [],
101108
$jsonAsArray['preset'] ?? null,
109+
$jsonAsArray['language'] ?? null,
102110
);
103111
}
104112

@@ -154,6 +162,14 @@ public function isWordIgnored(string $word): bool
154162
]);
155163
}
156164

165+
/**
166+
* Retrieves the configured language or the default
167+
*/
168+
public function getLanguage(): string
169+
{
170+
return $this->language ?? self::DEFAULT_LANGUAGE;
171+
}
172+
157173
/**
158174
* Save the configuration to the file.
159175
*/
@@ -163,6 +179,7 @@ private function persist(): void
163179

164180
file_put_contents($filePath, json_encode([
165181
...$this->preset !== null ? ['preset' => $this->preset] : [],
182+
...$this->language !== null ? ['language' => $this->language] : [],
166183
'ignore' => [
167184
'words' => $this->whitelistedWords,
168185
'paths' => $this->whitelistedPaths,

src/Services/Spellcheckers/Aspell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private function createProcess(): Process
122122
'utf-8',
123123
'-a',
124124
'--ignore-case',
125-
'--lang=en_US',
125+
'--lang='.$this->config->getLanguage(),
126126
]);
127127

128128
$process->setTimeout(0);

tests/Unit/ConfigTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Peck\Config;
6+
use Peck\Support\ProjectPath;
67

78
it('should have a default configuration', function (): void {
89
$config = Config::instance();
@@ -59,3 +60,34 @@
5960
'tests',
6061
]);
6162
});
63+
64+
describe('language', function (): void {
65+
beforeEach(function (): void{
66+
$configPath = "/temp.json";
67+
$this->jsonPath = ProjectPath::get().$configPath;
68+
Config::flush();
69+
Config::resolveConfigFilePathUsing(fn() => $configPath);
70+
$this->exampleConfig = [
71+
'preset' => 'laravel',
72+
'ignore' => [
73+
'words' => [
74+
'config',
75+
],
76+
]
77+
];
78+
});
79+
it('should default to `en_US` when the language flag is not set in the json file', function (): void {
80+
file_put_contents($this->jsonPath, json_encode($this->exampleConfig));
81+
$config = Config::instance();
82+
expect($config->getLanguage())->toBe('en_US');
83+
});
84+
it('should read the language flag from the json file', function (): void {
85+
$this->exampleConfig['language'] = 'en_GB';
86+
file_put_contents($this->jsonPath, json_encode($this->exampleConfig));
87+
$config = Config::instance();
88+
expect($config->getLanguage())->toBe('en_GB');
89+
});
90+
afterEach(function (): void {
91+
unlink($this->jsonPath);
92+
});
93+
});

0 commit comments

Comments
 (0)