Converts localized files in Laravel 6 and Module from PHP to JSON
1. Now the translation strings are stored in the files in the resources/lang directory. In this directory, every language supported by the application should have a corresponding subdirectory:
/resources
/lang
/en
validation.php
/en-US
validation.php
2. The previous call method: return trans(online_store_theme_graphql::validation.custom.theme_editor_session.theme_editor_session_id_exists_rule);
3. Plan to translate files are stored in the resources/lang directory in JSON format
/resources
/lang
/en
validation.php
/en-US
validation.php
en.json
en-US.json
3. Deliberately distinguish /en/validation.php from en.json to confirm the order of priority. Confirm that .php has priority over .json. as shown in Figure 1
<?php
return [
'custom' => [
'theme_editor_session' => [
'theme_editor_session_id_exists_rule' => 'The selected :attribute is invalid.',
],
],
'attributes' => [],
];
{
"validation.custom.theme_editor_session.theme_editor_session_id_exists_rule": "The selected :attribute is invalid1."
}
4. Delete PHP-related directories and files
/resources
/lang
en.json
en-US.json
5. It is found that the translation did not work normally. as shown in Figure 2
6. Use the GetLocale and IsLocale methods of App Facade to determine the current locale
echo App::getLocale();
exit;
{
"error": {},
"text": "en"
}
7. Refer to How to use JSON translation files using modules? (Translation strings as keys):https://github.com/nWidart/laravel-modules/issues/954. Adjust the registerTranslations() in the service provider, delete the reference to the php file, and use the reference to the json file.
/**
* Register translations.
*
* @return void
*/
public function registerTranslations()
{
$langPath = resource_path('lang/modules/' . $this->moduleNameLower);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
$this->loadJsonTranslationsFrom($langPath);
} else {
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', $this->moduleNameLower);
$this->loadJsonTranslationsFrom(__DIR__ . '/../Resources/lang');
}
}
/**
* Register translations.
*
* @return void
*/
public function registerTranslations()
{
$langPath = resource_path('lang/modules/' . $this->moduleNameLower);
if (is_dir($langPath)) {
$this->loadJsonTranslationsFrom($langPath);
} else {
$this->loadJsonTranslationsFrom(__DIR__ . '/../Resources/lang');
}
}
8. The translation still does not work normally. In the end, it was decided to give up this implementation for the time being.

