Skip to content

Commit 3b9e01f

Browse files
committed
Merge with style fixes
2 parents e29b888 + a34fd5f commit 3b9e01f

5 files changed

+13
-9
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Laravel Translation Json Cache
22

3-
When using the `.json` option for translations, the json-file is read and parsed for every request. This package provides an artisan command to cache the parsed data as a php-file, pretty much the same was as routes and configs can be cached. This gives a performance boost since no JSON-parsing has to be done and the opcache can be used. The actual performance boost depends on the size of the translation file and the filesystem, see below.
3+
When using the `.json` option for translations, the json-file is read and parsed for every request. This package provides an artisan command to cache the parsed data as a php-file, pretty much the same was as routes and configs can be cached. This gives a performance boost since no JSON-parsing has to be done and the opcache can be used. The actual performance boost depends on the size of the translation file and the disk speed, see below.
44

55
## Installation
66
```bash
@@ -28,10 +28,13 @@ Run `translation-json:cache` during deployment in the same way you run `route:ca
2828
The cached files are stored in the `bootstrap/cache` directory and are named `translation-{$locale}.php`.
2929

3030
## Performance boost
31-
On my Macbook Pro 2018 (2.6 GHz i7), Laravel Valet, and using a JSON translation file of 1500 strings (real strings of varying length), the first call to `__("A")` during a request takes about `1.2 ms`. Enabling this package and the same call takes `0.08 ms`, i.e. the actual performance boost is in the order of `1 ms`, which is substantial considering that a simple full request could be as fast as `10 ms`.
31+
On a Macbook Pro 2018 (2.6 GHz i7), using Laravel Valet, and having a JSON translation file of 1500 strings (real strings of varying length), the first call to `__("A")` during a request takes about `1.2 ms`. Enabling this package and the same call takes `0.08 ms`, i.e. the actual performance boost is in the order of `1 ms`, which is substantial considering that a simple full request could be as fast as `10 ms`.
3232

33-
I have been using this package in production (1500 strings for each language) and I have to say the performance boost is not as noticable there. This may be because our server setup uses docker and an all in-memory disk. I'm open to additional measurements, please feel free to contribute!
33+
I have been using this solution in production for a while (1500 strings for each language) and I have to say the performance boost is not really noticable there (comparing CPU graphs, which of course depends on a lot of other changes as well). The difference may be smaller since our server setup uses docker and an all in-memory disk. Please feel free to contribute with additional measurements under other circumstances or with other sizes of the JSON files.
3434

3535
## License
3636

3737
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
38+
39+
## Future work
40+
I realize the JSON-option may not be the preferred one, and that most people already use php-files for storing their translation data hierachically. In that case, a similar approach could still possibly save time on parsing and traversal, but this is outside the scope of this package. Feel free to investigate this idéa further in a new package!

src/Console/Commands/ClearTranslationJsonCache.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class ClearTranslationJsonCache extends Command
1010
{
1111
protected $signature = 'translation-json:clear';
1212

13-
protected $description = 'Deletes cached translation JSON-files, letting the application fallback to the JSON files.';
13+
protected $description = 'Deletes cached translation JSON-files,
14+
letting the application fallback to the JSON files.';
1415

1516
public function handle()
1617
{

src/Console/Commands/MakeTranslationJsonCache.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function handle()
2828
$this->info("Translation JSON cached successfully!");
2929
}
3030

31-
private function getTranslationJsonFiles () {
31+
private function getTranslationJsonFiles() {
3232
return collect(File::files(base_path().'/resources/lang'))->map(function ($file) {
3333
return $file->getFilename();
3434
})->filter(function ($filename) {

tests/Feature/MakeJsonCacheCommandTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function one_cache_file_is_created_for_each_language()
3939
}
4040

4141
/** @test */
42-
function the_data_in_the_cached_file_corresponds_to_the_json_file ()
42+
function the_data_in_the_cached_file_corresponds_to_the_json_file()
4343
{
4444
file_put_contents(base_path().'/resources/lang/en.json', '{"Test":"Testing123"}');
4545

tests/Feature/TranslationJsonCacheTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
class TranslationJsonCacheTest extends TestCase
66
{
77
/** @test */
8-
function the_cache_file_is_used_when_it_exists ()
8+
function the_cache_file_is_used_when_it_exists()
99
{
1010
file_put_contents(base_path().'/bootstrap/cache/translation-en.php', '<?php return ["Test" => "Testing"];');
1111

1212
$this->assertEquals("Testing", __("Test"));
1313
}
1414

1515
/** @test */
16-
function the_cache_file_is_not_used_when_it_does_not_exist ()
16+
function the_cache_file_is_not_used_when_it_does_not_exist()
1717
{
1818
$this->assertEquals("Test", __("Test"));
1919
}
2020

2121
/** @test */
22-
function the_cached_file_takes_presendece_if_it_differs_from_the_json_file ()
22+
function the_cached_file_takes_presendece_if_it_differs_from_the_json_file()
2323
{
2424
file_put_contents(base_path().'/bootstrap/cache/translation-en.php', '<?php return ["Test" => "Testing123"];');
2525
file_put_contents(base_path().'/resources/lang/en.json', '{"Test":"Testing"}');

0 commit comments

Comments
 (0)