diff --git a/README.md b/README.md index 4053efe5..c35c473e 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,25 @@ In order to make it easier to get started with Peck, we've included a few preset - `laravel` +### Languages + +While by default `peck` verfies the spelling by using GNU Aspell's `en_US` language dictionary, you can optionally specify a different (installed) language to be passed using the "language" key in the configuration: + +```json +{ + "preset": "laravel", + "language": "en_US", + "ignore": { + "words": [ + "config", + "namespace" + ] + } +} +``` + +You can see a full list of available dictionaries using the command `aspell dump dicts`. + ## Command Options The behaviour of `peck` can be modified with the following command options: diff --git a/src/Config.php b/src/Config.php index dabbd366..8861b098 100644 --- a/src/Config.php +++ b/src/Config.php @@ -15,6 +15,11 @@ final class Config */ private const JSON_CONFIGURATION_NAME = 'peck.json'; + /** + * The default language passed to Aspell. + */ + private const DEFAULT_LANGUAGE = 'en_US'; + /** * The instance of the configuration. */ @@ -35,6 +40,7 @@ public function __construct( public array $whitelistedWords = [], public array $whitelistedPaths = [], public ?string $preset = null, + public ?string $language = null, ) { $this->whitelistedWords = array_map(strtolower(...), $whitelistedWords); } @@ -87,6 +93,7 @@ public static function instance(): self /** * @var array{ * preset?: string, + * language?: string, * ignore?: array{ * words?: array, * paths?: array @@ -99,6 +106,7 @@ public static function instance(): self $jsonAsArray['ignore']['words'] ?? [], $jsonAsArray['ignore']['paths'] ?? [], $jsonAsArray['preset'] ?? null, + $jsonAsArray['language'] ?? null, ); } @@ -154,6 +162,14 @@ public function isWordIgnored(string $word): bool ]); } + /** + * Retrieves the configured language or the default + */ + public function getLanguage(): string + { + return $this->language ?? self::DEFAULT_LANGUAGE; + } + /** * Save the configuration to the file. */ @@ -163,6 +179,7 @@ private function persist(): void file_put_contents($filePath, json_encode([ ...$this->preset !== null ? ['preset' => $this->preset] : [], + ...$this->language !== null ? ['language' => $this->language] : [], 'ignore' => [ 'words' => $this->whitelistedWords, 'paths' => $this->whitelistedPaths, diff --git a/src/Services/Spellcheckers/Aspell.php b/src/Services/Spellcheckers/Aspell.php index f517a6d0..7836b667 100644 --- a/src/Services/Spellcheckers/Aspell.php +++ b/src/Services/Spellcheckers/Aspell.php @@ -122,7 +122,7 @@ private function createProcess(): Process 'utf-8', '-a', '--ignore-case', - '--lang=en_US', + '--lang='.$this->config->getLanguage(), ]); $process->setTimeout(0); diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index 780e7217..722d9f3c 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Peck\Config; +use Peck\Support\ProjectPath; it('should have a default configuration', function (): void { $config = Config::instance(); @@ -59,3 +60,37 @@ 'tests', ]); }); + +describe('language', function (): void { + beforeEach(function (): void { + $configPath = '/temp.json'; + $this->jsonPath = ProjectPath::get().$configPath; + Config::flush(); + Config::resolveConfigFilePathUsing(fn (): string => $configPath); + $this->exampleConfig = [ + 'preset' => 'laravel', + 'ignore' => [ + 'words' => [ + 'config', + ], + ], + ]; + }); + + it('should default to `en_US` when the language flag is not set in the json file', function (): void { + file_put_contents($this->jsonPath, json_encode($this->exampleConfig)); + $config = Config::instance(); + expect($config->getLanguage())->toBe('en_US'); + }); + + it('should read the language flag from the json file', function (): void { + $this->exampleConfig['language'] = 'en_GB'; + file_put_contents($this->jsonPath, json_encode($this->exampleConfig)); + $config = Config::instance(); + expect($config->getLanguage())->toBe('en_GB'); + }); + + afterEach(function (): void { + unlink($this->jsonPath); + }); +});