@@ -96,134 +96,6 @@ $collator = new Collator('en_US');
96
96
$result = $collator->compare('apple', 'äpple'); // Proper Unicode comparison
97
97
```
98
98
99
- ## Code Examples
100
-
101
- ### Number Formatting
102
-
103
- ``` php
104
- use NumberFormatter;
105
-
106
- class LocalizedNumbers
107
- {
108
- public function formatCurrency(float $amount, string $locale, string $currency): string
109
- {
110
- if (!extension_loaded('intl')) {
111
- // Fallback for non-ICU builds
112
- return $currency . ' ' . number_format($amount, 2);
113
- }
114
-
115
- $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
116
- return $formatter->formatCurrency($amount, $currency);
117
- }
118
-
119
- public function formatPercent(float $value, string $locale): string
120
- {
121
- if (!extension_loaded('intl')) {
122
- return number_format($value * 100, 1) . '%';
123
- }
124
-
125
- $formatter = new NumberFormatter($locale, NumberFormatter::PERCENT);
126
- return $formatter->format($value);
127
- }
128
- }
129
-
130
- // Usage
131
- $numbers = new LocalizedNumbers();
132
-
133
- echo $numbers->formatCurrency(1234.56, 'en_US', 'USD'); // $1,234.56
134
- echo $numbers->formatCurrency(1234.56, 'de_DE', 'EUR'); // 1.234,56 €
135
- echo $numbers->formatPercent(0.1234, 'en_US'); // 12.3%
136
- ```
137
-
138
- ### Date Formatting
139
-
140
- ``` php
141
- use IntlDateFormatter;
142
-
143
- class LocalizedDates
144
- {
145
- public function formatDate(\DateTime $date, string $locale): string
146
- {
147
- if (!extension_loaded('intl')) {
148
- return $date->format('M j, Y');
149
- }
150
-
151
- $formatter = new IntlDateFormatter(
152
- $locale,
153
- IntlDateFormatter::LONG,
154
- IntlDateFormatter::NONE
155
- );
156
-
157
- return $formatter->format($date);
158
- }
159
- }
160
-
161
- // Usage
162
- $dates = new LocalizedDates();
163
- $date = new DateTime('2024-03-15');
164
-
165
- echo $dates->formatDate($date, 'en_US'); // March 15, 2024
166
- echo $dates->formatDate($date, 'de_DE'); // 15. März 2024
167
- echo $dates->formatDate($date, 'ja_JP'); // 2024年3月15日
168
- ```
169
-
170
- ### String Collation
171
-
172
- ``` php
173
- use Collator;
174
-
175
- class LocalizedSorting
176
- {
177
- public function sortNames(array $names, string $locale): array
178
- {
179
- if (!extension_loaded('intl')) {
180
- // Simple ASCII sort fallback
181
- sort($names);
182
- return $names;
183
- }
184
-
185
- $collator = new Collator($locale);
186
- $collator->sort($names);
187
- return $names;
188
- }
189
- }
190
-
191
- // Usage
192
- $sorter = new LocalizedSorting();
193
- $names = ['Müller', 'Mueller', 'Miller', 'Möller'];
194
-
195
- $sorted = $sorter->sortNames($names, 'de_DE');
196
- // Proper German sorting with umlauts
197
- ```
198
-
199
- ## Framework Integration
200
-
201
- ### Laravel Localization
202
-
203
- ``` php
204
- // config/app.php
205
- return [
206
- 'locale' => 'en',
207
- 'available_locales' => ['en', 'es', 'fr', 'de', 'ja'],
208
- ];
209
-
210
- // With ICU support, you can use advanced formatters
211
- class LocalizedContent
212
- {
213
- public function getLocalizedPrice(float $price): string
214
- {
215
- $locale = app()->getLocale();
216
- $currency = config('app.currency.' . $locale, 'USD');
217
-
218
- if (extension_loaded('intl')) {
219
- $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
220
- return $formatter->formatCurrency($price, $currency);
221
- }
222
-
223
- return $currency . ' ' . number_format($price, 2);
224
- }
225
- }
226
- ```
227
99
228
100
## Performance Considerations
229
101
0 commit comments