There is no problem not worth solving, and no technology not worth learning!

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

特意将 /en/validation.php 与 en.json 进行区分,以确认优先级的顺序。确认 .php 的优先级高于 .json
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

发现翻译未正常工作
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.

PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.