8
8
use Elegantly \Translator \Collections \Translations ;
9
9
use Elegantly \Translator \Drivers \Driver ;
10
10
use Elegantly \Translator \Exceptions \TranslatorServiceException ;
11
+ use Elegantly \Translator \Services \Exporter \ExporterInterface ;
11
12
use Elegantly \Translator \Services \Proofread \ProofreadServiceInterface ;
12
13
use Elegantly \Translator \Services \SearchCode \SearchCodeServiceInterface ;
13
14
use Elegantly \Translator \Services \Translate \TranslateServiceInterface ;
@@ -19,6 +20,7 @@ final public function __construct(
19
20
public ?TranslateServiceInterface $ translateService = null ,
20
21
public ?ProofreadServiceInterface $ proofreadService = null ,
21
22
public ?SearchCodeServiceInterface $ searchcodeService = null ,
23
+ public ?ExporterInterface $ exporter = null ,
22
24
) {
23
25
//
24
26
}
@@ -29,7 +31,8 @@ public function driver(null|string|Driver $name): static
29
31
driver: $ name instanceof Driver ? $ name : TranslatorServiceProvider::getDriverFromConfig ($ name ),
30
32
translateService: $ this ->translateService ,
31
33
proofreadService: $ this ->proofreadService ,
32
- searchcodeService: $ this ->searchcodeService
34
+ searchcodeService: $ this ->searchcodeService ,
35
+ exporter: $ this ->exporter ,
33
36
);
34
37
}
35
38
@@ -327,6 +330,56 @@ public function saveTranslations(
327
330
328
331
}
329
332
333
+ public function exportTranslations (
334
+ string $ path ,
335
+ ?ExporterInterface $ exporter = null
336
+ ): string {
337
+ $ exporter = $ exporter ?? $ this ->exporter ;
338
+
339
+ if (! $ exporter ) {
340
+ throw TranslatorServiceException::missingExporterService ();
341
+ }
342
+
343
+ $ locales = $ this ->getLocales ();
344
+
345
+ $ translationsByLocale = collect ($ locales )
346
+ ->mapWithKeys (fn ($ locale ) => [$ locale => $ this ->getTranslations ($ locale )])
347
+ ->all ();
348
+
349
+ return $ exporter ->export ($ translationsByLocale , $ path );
350
+
351
+ }
352
+
353
+ /**
354
+ * @return array<string, array<int|string, scalar>>
355
+ */
356
+ public function importTranslations (
357
+ string $ path ,
358
+ ?ExporterInterface $ exporter = null
359
+ ): array {
360
+ $ exporter = $ exporter ?? $ this ->exporter ;
361
+
362
+ if (! $ exporter ) {
363
+ throw TranslatorServiceException::missingExporterService ();
364
+ }
365
+
366
+ $ translationsByLocale = $ exporter ->import ($ path );
367
+
368
+ foreach ($ translationsByLocale as $ locale => $ values ) {
369
+
370
+ $ this ->transformTranslations (
371
+ locale: $ locale ,
372
+ callback: function ($ translations ) use ($ values ) {
373
+ return $ translations ->merge ($ values );
374
+ },
375
+ sort: config ()->boolean ('translator.sort_keys ' ),
376
+ );
377
+
378
+ }
379
+
380
+ return $ translationsByLocale ;
381
+ }
382
+
330
383
public function clearCache (): void
331
384
{
332
385
$ this ->searchcodeService ?->getCache()?->flush();
0 commit comments