Feat: getLanguages(), getTranslations() methods#12
Conversation
WalkthroughWidened PHPStan dev constraint to 1.*. Added Locale::getLanguages() and Locale::getTranslations() methods. Expanded Locale tests to verify language loading counts, translation maps, placeholder substitutions, and exception behavior for missing translations. Changes
Sequence Diagram(s)sequenceDiagram
actor Client
participant Locale as Locale (class)
Client->>Locale: loadLanguage(code, translations)
Client->>Locale: setDefault(code)
Client->>Locale: getLanguages()
Locale-->>Client: [code1, code2, ...]
Client->>Locale: new Locale()/getInstance
Client->>Locale: getTranslations()
Locale-->>Client: {key: value, ...}
Client->>Locale: getText(key, placeholders?)
alt key exists
Locale-->>Client: interpolated text
else key missing and exceptions=true
Locale-->>Client: throws Exception
else key missing and exceptions=false
Locale-->>Client: key or fallback
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
tests/Locale/LocaleTest.php (1)
84-97: Restore Locale::$exceptions after the assertion; simplify with finally/expectException.Current test toggles a global flag without restoring it and relies on early return. Use finally to restore state and expectException to simplify.
- Locale::$exceptions = true; - - try { - $locale->getText('world'); - } catch (\Throwable $exception) { - $this->assertInstanceOf(Exception::class, $exception); - - return; - } - - $this->fail('No exception was thrown'); + $prev = Locale::$exceptions; + Locale::$exceptions = true; + try { + $this->expectException(Exception::class); + $locale->getText('world'); + } finally { + Locale::$exceptions = $prev; + }
🧹 Nitpick comments (3)
composer.json (1)
18-18: Pin PHPStan to a minor version to avoid CI flakiness."1.*" can pull in newer 1.x releases that may introduce stricter rules and cause nondeterministic CI failures. Prefer pinning to a known-good minor.
- "phpstan/phpstan": "1.*" + "phpstan/phpstan": "^1.11"src/Locale/Locale.php (2)
28-36: Tighten PHPDoc to list for accuracy with array_keys.array_keys returns a numerically-indexed list. Using list improves static analysis precision.
- /** - * Get list of configured languages - * - * @return array<string> - */ + /** + * Get list of configured languages + * + * @return list<string> + */ public static function getLanguages(): array { return \array_keys(self::$language); }
123-131: Fix doc typo and add defensive guard in getTranslations().
- Typo: "transltions" → "translations".
- If the default locale disappears (e.g., languages cleared/reloaded), this will trigger an undefined index. Consider a guard that mirrors constructor/setDefault behavior.
- /** - * Get list of configured transltions in specific language - * - * @return array<string, string> - */ + /** + * Get list of configured translations for the current default language + * + * @return array<string, string> + */ public function getTranslations(): array { - return self::$language[$this->default]; + if (!\array_key_exists($this->default, self::$language)) { + if (self::$exceptions) { + throw new Exception('Locale not found'); + } + return []; + } + return self::$language[$this->default]; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
composer.json(1 hunks)src/Locale/Locale.php(2 hunks)tests/Locale/LocaleTest.php(3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/Locale/LocaleTest.php (1)
src/Locale/Locale.php (7)
Locale(7-132)getLanguages(33-36)setLanguageFromArray(44-47)setLanguageFromJSON(55-64)getTranslations(128-131)setDefault(82-91)getText(102-121)
🔇 Additional comments (2)
tests/Locale/LocaleTest.php (2)
46-49: Good coverage of translation content.Validating the entire map ensures the new API returns expected keys and values. No issues.
55-56: LGTM: Per-locale counts exercise getTranslations across switches.These assertions confirm the map is language-specific and updates correctly after setDefault.
Also applies to: 62-63
These methods will be useful in Appwrite to write CI/CD tests to check if any locale translation is missing.
Summary by CodeRabbit
New Features
Tests
Chores