Skip to content

Commit

Permalink
Make export statement in the translation file optional
Browse files Browse the repository at this point in the history
  • Loading branch information
GENL committed Mar 30, 2024
1 parent 11be346 commit a9e8696
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":1635:{a:2:{s:7:"defects";a:9:{s:102:"Genl\Matice\Tests\Unit\ManageTranslationsGeneratorCommandTest::fileIsCreatedWhenMaticeGenerateIsCalled";i:4;s:62:"Genl\Matice\Tests\Unit\ManageTranslationTest::loadTranslations";i:4;s:67:"Genl\Matice\Tests\Unit\ManageTranslationTest::generateTranslationJs";i:3;s:7:"Warning";i:6;s:81:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_folder_namespace_can_be_hidden";i:4;s:83:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_folder_namespace_can_be_excepted";i:3;s:77:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_namespaces_can_be_excepted";i:3;s:90:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_only_certain_namespaces_can_be_exported";i:3;s:100:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_only_namespaces_can_be_both_exported_and_excepted";i:3;}s:5:"times";a:9:{s:62:"Genl\Matice\Tests\Unit\ManageTranslationTest::loadTranslations";d:13.973;s:67:"Genl\Matice\Tests\Unit\ManageTranslationTest::generateTranslationJs";d:3.48;s:102:"Genl\Matice\Tests\Unit\ManageTranslationsGeneratorCommandTest::fileIsCreatedWhenMaticeGenerateIsCalled";d:1.63;s:7:"Warning";d:0.031;s:81:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_folder_namespace_can_be_hidden";d:0.326;s:83:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_folder_namespace_can_be_excepted";d:0.637;s:77:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_namespaces_can_be_excepted";d:0.038;s:90:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_only_certain_namespaces_can_be_exported";d:0.029;s:100:"Genl\Matice\Tests\Unit\ManageTranslationTest::test_only_namespaces_can_be_both_exported_and_excepted";d:0.032;}}}
{"version":1,"defects":[],"times":{"Genl\\Matice\\Tests\\Unit\\ManageTranslationTest::test_load_translations":0.047,"Genl\\Matice\\Tests\\Unit\\ManageTranslationTest::test_generate_translation_js":0.004,"Genl\\Matice\\Tests\\Unit\\ManageTranslationTest::test_namespaces_can_be_excepted":0.003,"Genl\\Matice\\Tests\\Unit\\ManageTranslationTest::test_only_certain_namespaces_can_be_exported":0.003,"Genl\\Matice\\Tests\\Unit\\ManageTranslationTest::test_only_namespaces_can_be_both_exported_and_excepted":0.003}}
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Latest Version on Packagist](https://img.shields.io/packagist/v/genl/matice.svg?style=flat-square)](https://packagist.org/packages/genl/matice)
[![Latest Version on NPM](https://img.shields.io/npm/v/matice.svg?style=flat)](https://npmjs.com/package/matice)
[![GitHub Actions Status](https://img.shields.io/github/actions/workflow/status/genl/matice/.github/workflows/tests.yml?branch=master&label=tests&style=flat)](https://github.com/genl/matice/actions/workflows/.github/workflows/tests.yml?branch=master)
[![Tests](https://github.com/GENL/matice/actions/workflows/tests.yml/badge.svg)](https://github.com/GENL/matice/actions/workflows/tests.yml)
[![Total Downloads on packagist](https://img.shields.io/packagist/dt/genl/matice.svg?style=flat-square)](https://packagist.org/packages/genl/matice/stats)
[![Downloads on NPM](https://img.shields.io/npm/dt/matice.svg?style=flat)](https://www.npmjs.com/package/matice)

Expand Down Expand Up @@ -284,11 +284,11 @@ The base directory is the lang_directory defined in the config file: `config('ma
## Use with SPA
Matice registers an Artisan console command to generate a `matice_translations.js` translations file, which can be used (or not) as part of an asset pipeline such as [Laravel Mix](https://laravel.com/docs/mix).

You can run `php artisan matice:generate` in your project to generate a static translations file in `resources/assets/js/matice_translations.js`.
You can run `php artisan matice:generate --no-export` in your project to generate a static translations file without the export statement in `resources/assets/js/matice_translations.js`.
You can customize the generation path in the `config/matice.php` file.

```sh
php artisan matice:generate
php artisan matice:generate --no-export
```

An example of `matice_translations.js`, where auth translations exist in `resources/lang/en/auth.php`:
Expand Down Expand Up @@ -318,12 +318,11 @@ const Matice = {
}
};

export { Matice };
```

At this point you can use in javascript this translations file like usual, paste in your html as well.

This is useful if your laravel and js app is separated like with SPA or PWA. So you can
This is useful if your laravel and js app are separated like with SPA or PWA. So you can
link the generated translations file with your JS App. If you're not in the case of SPA, WPA...
you might never have to generate the translations manually because `@translations` directive already does
it for you when the app environment is 'production' to improve performance.
Expand Down
22 changes: 14 additions & 8 deletions src/BladeTranslationsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ class BladeTranslationsGenerator
* All translation are loaded if locale is null or empty. Default to null
* @param bool $wrapInHtml
* @param bool $useCache - Whether to use the cached generated script or to generate a new one.
* @param bool $hasExport
* @return string
* @throws LocaleTranslationsFileOrDirNotFoundException
*/
public function generate($locales = null, bool $wrapInHtml = true, bool $useCache = false): string
public function generate(
$locales = null,
bool $wrapInHtml = true,
bool $useCache = false,
bool $hasExport = true
): string
{
$locales = is_array($locales ?? []) ?
$locales ?? [] :
Expand All @@ -36,7 +43,7 @@ public function generate($locales = null, bool $wrapInHtml = true, bool $useCach
null,
PREG_SPLIT_NO_EMPTY
);
// Given a list of locales are set, we want to make sure the fallback local
// Given a list of locales, we want to make sure the fallback local
// is always there
$locales = $locales ?
collect(
Expand Down Expand Up @@ -72,36 +79,35 @@ public function generate($locales = null, bool $wrapInHtml = true, bool $useCach
"Matice Laravel Translations generated"
);
} else {
return $this->makeMaticeJSObject($locales);
return $this->makeMaticeJSObject($locales, $hasExport);
}
}

/**
* @param string[] $locales
* @param bool $hasExport
* @return string
* @throws LocaleTranslationsFileOrDirNotFoundException
*/
private function makeMaticeJSObject(array $locales): string
private function makeMaticeJSObject(array $locales, bool $hasExport=true): string
{
$translations = json_encode($this->translations($locales));
$appLocale = $locale ?? app()->getLocale();
$fallbackLocale = config('app.fallback_locale');

$exportStatement = $hasExport ? "\n$this->maticeExportStatement" : '';
return <<<EOT
const Matice = {
locale: '$appLocale',
fallbackLocale: '$fallbackLocale',
translations: $translations
}
$this->maticeExportStatement
$exportStatement
EOT;
}


/**
* @param string $maticeJSObject
* @param bool $shouldRemoveExportStatement
* @param string ...$comments
* @return string
*/
Expand Down
8 changes: 6 additions & 2 deletions src/Commands/TranslationsGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

class TranslationsGeneratorCommand extends Command
{
protected $signature = 'matice:generate'; // {path=./resources/assets/js/matice_translations.js}
protected $signature = 'matice:generate
{--no-export : Omit the export statement}';

protected $description = 'Generate js file for including in build process';


/**
* Create a new command instance.
*
Expand All @@ -30,7 +32,9 @@ public function __construct()
public function handle()
{
$path = config('matice.generate_translations_path'); // $this->argument('path');
$generatedTranslations = Matice::generate(null, false, false);
$generatedTranslations = Matice::generate(
null, false, false, !$this->option('no-export')
);
$generatedTranslations = ('/*
|--------------------------------------------------------------------------
| Generated Laravel translations
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/Matice.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* @method static array translations(?string $locale = null) - Load all the translations array.
* @method static string generate(?string $locale = null, bool $wrapInHtml = true, bool $useCache = false) - Load the translations array and the generate a the html code to paste to the page.
* @method static string generate(?string|string[] $locale = null, bool $wrapInHtml = true, bool $useCache = false, bool $hasExport = true) - Load the translations array and the generate a the html code to paste to the page.
*
* @see \Genl\Matice\BladeTranslationsGenerator
*/
Expand Down

0 comments on commit a9e8696

Please sign in to comment.