|
1
|
<?php |
|
2
|
|
|
3
|
/** |
|
4
|
* I18n Feature - Public API Functions |
|
5
|
* |
|
6
|
* Only commonly-used developer-facing functions in mini\ namespace. |
|
7
|
* Internal framework functions are in mini\I18n\ namespace. |
|
8
|
*/ |
|
9
|
|
|
10
|
namespace mini; |
|
11
|
|
|
12
|
use mini\I18n\Fmt; |
|
13
|
use mini\I18n\Translatable; |
|
14
|
|
|
15
|
/** |
|
16
|
* Translation function - creates a Translatable instance |
|
17
|
* |
|
18
|
* This is the primary function developers use for translations. |
|
19
|
* |
|
20
|
* @param string $text The text to translate |
|
21
|
* @param array $vars Variables for interpolation (e.g., ['name' => 'John']) |
|
22
|
* @return Translatable |
|
23
|
*/ |
|
24
|
function t(string $text, array $vars = []): Translatable { |
|
25
|
return new Translatable($text, $vars); |
|
26
|
} |
|
27
|
|
|
28
|
/** |
|
29
|
* Get a formatter instance for convenience |
|
30
|
* |
|
31
|
* Provides shortcuts for common formatting tasks. |
|
32
|
* Note: Fmt methods are static, so you can also call Fmt::currency() directly |
|
33
|
* |
|
34
|
* @return Fmt Stateless formatter instance (singleton) |
|
35
|
*/ |
|
36
|
function fmt(): Fmt { |
|
37
|
return Mini::$mini->get(Fmt::class); |
|
38
|
} |
|
39
|
|
|
40
|
/** |
|
41
|
* ============================================================================ |
|
42
|
* I18n Service Registration |
|
43
|
* ============================================================================ |
|
44
|
*/ |
|
45
|
|
|
46
|
namespace mini\I18n; |
|
47
|
|
|
48
|
use mini\Mini; |
|
49
|
use mini\Lifetime; |
|
50
|
use mini\Util\PathsRegistry; |
|
51
|
|
|
52
|
// Register translations path registry |
|
53
|
$primaryTranslationsPath = $_ENV['MINI_TRANSLATIONS_ROOT'] ?? (Mini::$mini->root . '/_translations'); |
|
54
|
Mini::$mini->paths->translations = new PathsRegistry($primaryTranslationsPath); |
|
55
|
$frameworkTranslationsPath = \dirname((new \ReflectionClass(Mini::class))->getFileName(), 2) . '/translations'; |
|
56
|
Mini::$mini->paths->translations->addPath($frameworkTranslationsPath); |
|
57
|
|
|
58
|
// Register I18n services |
|
59
|
// Register interface (loads from config, allows custom implementations) |
|
60
|
Mini::$mini->addService(TranslatorInterface::class, Lifetime::Singleton, fn() => Mini::$mini->loadServiceConfig(TranslatorInterface::class)); |
|
61
|
// Register concrete class as alias to interface (for backward compatibility) |
|
62
|
Mini::$mini->addService(Translator::class, Lifetime::Singleton, fn() => Mini::$mini->get(TranslatorInterface::class)); |
|
63
|
Mini::$mini->addService(Fmt::class, Lifetime::Singleton, fn() => Mini::$mini->loadServiceConfig(Fmt::class)); |
|
64
|
|