Skip to content

Commit

Permalink
Merge pull request #22 from nextapps-be/feature/remove_empty_translat…
Browse files Browse the repository at this point in the history
…ions_on_download

remove empty translation strings on poeditor download
  • Loading branch information
yinx authored Feb 7, 2024
2 parents f52d188 + 8730824 commit efea95d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Upgrading

## From v1 to v2

- Translation keys with '.' in the key i.e. 'user.name' will result in duplicate translation entries after upload and download from poeditor. To prevent this convert all keys with '.' to arrays.
2 changes: 1 addition & 1 deletion src/Poeditor/Poeditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function download(string $language) : array
'type' => 'key_value_json',
])->json('result.url');

return Http::get($exportUrl)->json();
return collect(Http::get($exportUrl)->json())->dot()->filter()->undot()->toArray();
}

public function upload(string $language, array $translations, bool $overwrite = false) : UploadResponse
Expand Down
36 changes: 35 additions & 1 deletion tests/PoeditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class PoeditorTest extends TestCase
{
/** @test */
public function it_requests_export_url_and_downloads_it_content()
public function it_requests_export_url_and_downloads_its_content()
{
Http::fake([
'https://api.poeditor.com/v2/projects/export' => Http::response([
Expand Down Expand Up @@ -47,6 +47,40 @@ public function it_requests_export_url_and_downloads_it_content()
});
}

/** @test */
public function it_downloads_its_content_and_filters_empty_strings_out()
{
Http::fake([
'https://api.poeditor.com/v2/projects/export' => Http::response([
'response' => [
'status' => 'success',
'code' => 200,
'message' => 'OK',
],
'result' => [
'url' => $exportUrl = $this->faker->url(),
],
]),
$exportUrl => Http::response([
'foo' => 'bar',
'empty' => '',
'nested' => [
'empty' => [
'value' => '',
],
'not_empty' => [
'bar' => 'baz',
'empty' => '',
],
],
]),
]);

$translations = app(Poeditor::class)->download($this->faker->locale());

$this->assertEquals(['foo' => 'bar', 'nested' => ['not_empty' => ['bar' => 'baz']]], $translations);
}

/** @test */
public function it_throws_an_error_if_api_key_is_empty()
{
Expand Down

0 comments on commit efea95d

Please sign in to comment.