Appearance
Application Level Translations
Module Manager can find and manage translations not only for modules but also for your main Laravel application. This feature scans PHP and Blade files for translation strings and optionally translates them using AI.
Finding Translation Strings
Command Line
bash
# Find translation strings in the main application
php artisan mm:find-translations --app
This command will:
- Scan all PHP and Blade files in your application (excluding modules)
- Find all strings wrapped in translation functions (
__()
,trans()
,@lang()
) - Add them to your application's language files
- Optionally translate them to other languages if configured
Programmatic Usage
You can also scan for translations programmatically:
php
use ModuleManager\ModuleManager\Internals\Services\TranslationStringFinder;
use ModuleManager\ModuleManager\Internals\Services\TranslationFileManager;
// For the main application
$translations = [];
$finder = app(TranslationStringFinder::class);
$fileManager = app(TranslationFileManager::class);
// Scan application files, excluding modules
$excludePaths = ['Modules', 'vendor', 'node_modules', 'storage'];
$newStringsCount = $finder->scanApplicationFiles(base_path(), $translations, $excludePaths);
// Save the translations if new strings were found
if ($newStringsCount > 0) {
$fileManager->saveTranslationFile(lang_path() . '/en.json', $translations);
}
Configuring Translation
To enable AI-powered translations, configure the translation settings in your config/module-manager.php
file:
php
'translation' => [
'from_language' => 'en',
'to_language' => ['pt_BR', 'es', 'fr'],
'TRANSLATION_API_KEY' => env('OPENAI_API_KEY'),
'TRANSLATION_MODEL' => 'gpt-4o',
'TRANSLATION_MODEL_PROVIDER' => Prism\Prism\Enums\Provider::OpenAI,
],
Make sure the PrismPHP library is installed and properly configured for translations to work.
Translation Process
When you run the mm:find-translations --app
command with AI translation enabled:
- The command scans your application files for translation strings
- New strings are added to your
lang/en.json
file - If you choose to translate, the system will use the configured AI to translate strings
- Translated strings are saved to the appropriate language files (e.g.,
lang/es.json
,lang/fr.json
)
This process provides an efficient way to manage translations across your entire application, ensuring consistency between modules and the main application.
Translatable Properties
In addition to translation functions, Module Manager can also detect and translate property values that are marked as translatable using either attributes or doc comments.
Using the Trans Attribute
You can mark class properties as translatable using the #[Trans]
attribute:
php
use ModuleManager\ModuleManager\Attributes\Trans;
class MyClass
{
#[Trans]
public string $title = 'Welcome to our application';
#[Trans]
protected string $description = 'This is a sample description';
}
Using Doc Comments
Alternatively, you can use the @trans
doc comment to mark properties as translatable:
php
class MyClass
{
/** @trans */
public string $errorMessage = 'An error occurred';
public string $confirmationMessage = 'Operation completed successfully' /** @trans */;
}
Both methods will cause the property values to be detected by the translation scanner and added to your language files when you run the mm:find-translations
command.
Translatable Variables
In addition to class properties, Module Manager can also detect and translate variable values that are marked as translatable using doc comments.
Using Doc Comments with Variables
You can use the @trans
doc comment to mark variables as translatable in two ways:
php
function example()
{
// Annotation after the string
$errorMessage = 'An error occurred' /** @trans */;
// Annotation before the string
$welcomeMessage = /** @trans */ 'Welcome to our application';
}
Both methods will cause the variable values to be detected by the translation scanner and added to your language files when you run the mm:find-translations
command.