-
Notifications
You must be signed in to change notification settings - Fork 6
fix: normalize filename and try to find a match #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e7d467e
e23c0bc
2326e49
8516008
a483903
c38f784
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,7 +118,7 @@ private function setOption(string $skeletonOption, DateTimeFormatOptions $option | |
| '"e..eee" (weekday) patterns are not supported', | ||
| ); | ||
| } | ||
| $options->weekday = ['short', 'long', 'narrow', 'short'][$length - 4]; | ||
| $options->weekday = $this->getWeekdayValue($length - 4); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes a static analysis issue that Psalm raised in CI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay because I don't understand what a narrow weekday is :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These values are the ECMA-402 names that translate to ICU formatting symbols. "Narrow" basically means the shortest possible display. So, while |
||
|
|
||
| break; | ||
| case 'c': | ||
|
|
@@ -127,7 +127,7 @@ private function setOption(string $skeletonOption, DateTimeFormatOptions $option | |
| '"c..ccc" (weekday) patterns are not supported', | ||
| ); | ||
| } | ||
| $options->weekday = ['short', 'long', 'narrow', 'short'][$length - 4]; | ||
| $options->weekday = $this->getWeekdayValue($length - 4); | ||
|
|
||
| break; | ||
| // Period | ||
|
|
@@ -202,4 +202,28 @@ private function setOption(string $skeletonOption, DateTimeFormatOptions $option | |
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @psalm-return "long" | "narrow" | "short" | ||
| */ | ||
| private function getWeekdayValue(int $index): string | ||
| { | ||
| switch ($index) { | ||
| case 1: | ||
| $value = 'long'; | ||
|
|
||
| break; | ||
| case 2: | ||
| $value = 'narrow'; | ||
|
|
||
| break; | ||
| case 0: | ||
| default: | ||
| $value = 'short'; | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| return $value; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,11 +35,16 @@ | |
| use function array_filter; | ||
| use function array_unique; | ||
| use function array_values; | ||
| use function file_exists; | ||
| use function implode; | ||
| use function is_callable; | ||
| use function scandir; | ||
| use function sprintf; | ||
| use function str_replace; | ||
| use function strtolower; | ||
|
|
||
| use const DIRECTORY_SEPARATOR; | ||
| use const SCANDIR_SORT_NONE; | ||
|
|
||
| /** | ||
| * Loads messages for a given locale from the file system or cache | ||
|
|
@@ -48,6 +53,8 @@ | |
| */ | ||
| class MessageLoader | ||
| { | ||
| private const MESSAGE_FILE_EXTENSION = '.json'; | ||
|
|
||
| private ConfigInterface $config; | ||
| private FileSystemHelper $fileSystemHelper; | ||
| private FormatHelper $formatHelper; | ||
|
|
@@ -108,8 +115,9 @@ private function getLocaleMessages(): array | |
|
|
||
| foreach ($this->getFallbackLocales() as $locale) { | ||
| try { | ||
| $messagesFile = $this->messagesDirectory . DIRECTORY_SEPARATOR . $locale . '.json'; | ||
| $messagesContents = $this->fileSystemHelper->getJsonContents($messagesFile); | ||
| $messagesContents = $this->fileSystemHelper->getJsonContents( | ||
| $this->getFilePathForLocale($locale), | ||
| ); | ||
|
|
||
| break; | ||
| } catch (UnableToProcessFileException $exception) { | ||
|
|
@@ -119,8 +127,9 @@ private function getLocaleMessages(): array | |
|
|
||
| if ($messagesContents === false) { | ||
| throw new LocaleNotFoundException(sprintf( | ||
| 'Unable to find a suitable locale for "%s"; please set a default locale', | ||
| 'Unable to find a suitable locale for "%s" in %s; please set a default locale', | ||
| $this->config->getLocale()->toString(), | ||
| $this->messagesDirectory, | ||
| )); | ||
| } | ||
|
|
||
|
|
@@ -171,4 +180,30 @@ private function loadFormatReader($formatReader): callable | |
|
|
||
| return $this->formatHelper->getReader($formatReader); | ||
| } | ||
|
|
||
| private function getFilePathForLocale(string $locale): string | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method fixes the case-sensitivity problem. |
||
| { | ||
| $filePath = $this->messagesDirectory . DIRECTORY_SEPARATOR . $locale . self::MESSAGE_FILE_EXTENSION; | ||
| if (file_exists($filePath)) { | ||
| return $filePath; | ||
| } | ||
|
|
||
| // If the file doesn't exist, check for alternate casings and notations. | ||
| // e.g., en-XB, en_XB, en-xb, en_xb, EN-XB, EN_XB, eN-xB, etc. | ||
| $normalize = fn (string $filename): string => str_replace('_', '-', strtolower($filename)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| $searchFile = $normalize($locale . self::MESSAGE_FILE_EXTENSION); | ||
| $localeFiles = scandir($this->messagesDirectory, SCANDIR_SORT_NONE) ?: []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be worth putting in a short-circuit before the Just thinking of cases where
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call! |
||
|
|
||
| foreach ($localeFiles as $localeFile) { | ||
| if ($normalize($localeFile) === $searchFile) { | ||
| return $this->messagesDirectory . DIRECTORY_SEPARATOR . $localeFile; | ||
| } | ||
| } | ||
|
|
||
| throw new UnableToProcessFileException(sprintf( | ||
| 'Could not find file for locale "%s" in %s', | ||
| $locale, | ||
| $this->messagesDirectory, | ||
| )); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "about.inspire": { | ||
| "defaultMessage": "[!! Ḁṭ Ṡǩíííĺĺśśśḫâŕŕŕè, ẘè èṁṗṗṗŏẘèèèŕ ṁṁṁèṁḃḃḃèŕśśś ṭŏŏŏ ĝèèèṭ íííńśṗṗṗíŕèèèḋ. !!]" | ||
| }, | ||
| "how.many.pets": { | ||
| "defaultMessage": "[!! Ļâśśśṭ ṭṭṭíṁèèè Ḭ ćḫèèèćǩèèèḋ, !!]{gender, select, male{<italicized>[!! ḫè !!]</italicized>[!! ḫâââḋ !!]} female{<italicized>[!! śḫèèè !!]</italicized>[!! ḫâââḋ !!]} other{<italicized>[!! ṭḫèèèẏ !!]</italicized>[!! ḫâââḋ !!]}}[!! !!]{petCount, plural, =0{<bold>[!! ńŏ !!]</bold>[!! ṗèèèṭś !!]} =1{<bold>[!! â !!]</bold>[!! ṗèèèṭ !!]} other{<bold>#</bold>[!! ṗèèèṭś !!]}}[!! . !!]" | ||
| }, | ||
| "start.with.tag": { | ||
| "defaultMessage": "<foo>{argument}</foo>" | ||
| }, | ||
| "start.with.argument": { | ||
| "defaultMessage": "{argument}" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean
(e.g. "en-XB" vs. "en-xb")(both hiphens)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant
en_xb. Even if someone names their locale filesEN_XB.json,EN-XB.json,EN-xb.json, etc., the localeen-XBshould match.