Skip to content

Commit

Permalink
Fixes for JSON export (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahtinurme authored Feb 5, 2024
1 parent e4c6a5d commit 9b40b57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/TranslationsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ public function getLocales(): array
}

collect($this->filesystem->files(lang_path()))->each(function ($file) use ($locales) {
if ($this->filesystem->extension($file) != 'json') {
if ($this->filesystem->extension($file) !== 'json') {
return;
}
foreach (config('translations.exclude_files') as $excludeFile) {
if (fnmatch($excludeFile, $file)) {
return;
}
if (fnmatch($excludeFile, basename($file))) {
return;
}
}

if (! $locales->contains($file->getFilenameWithoutExtension())) {
$locales->push($file->getFilenameWithoutExtension());
Expand Down Expand Up @@ -108,7 +116,11 @@ public function export($download = false): void

foreach ($phrasesTree as $locale => $groups) {
foreach ($groups as $file => $phrases) {
$langPath = $download ? storage_path("app/translations/$locale/$file") : lang_path("$locale/$file");
if ($file === "$locale.json") {
$langPath = $download ? storage_path("app/translations/$file") : lang_path("$file");
} else {
$langPath = $download ? storage_path("app/translations/$locale/$file") : lang_path("$locale/$file");
}

if (! $this->filesystem->isDirectory(dirname($langPath))) {
$this->filesystem->makeDirectory(dirname($langPath), 0755, true);
Expand All @@ -127,7 +139,7 @@ public function export($download = false): void
}

if ($this->filesystem->extension($langPath) == 'json') {
$this->filesystem->put($langPath, json_encode($phrases, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
$this->filesystem->put($langPath, json_encode($phrases, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ function buildPhrasesTree($phrases, $locale): array

/** @var \Outhebox\TranslationsUI\Models\Phrase $phrase */
foreach ($phrases as $phrase) {
if ($phrase->file->file_name === "$locale.json") {
$tree[$locale][$phrase->file->file_name][$phrase->key] = ! blank($phrase->value) ? $phrase->value : $phrase->source->value;

continue;
}
setArrayValue(
array: $tree[$locale][$phrase->file->file_name],
key: $phrase->key,
Expand Down

0 comments on commit 9b40b57

Please sign in to comment.