没有不值得去解决的问题,也没有不值得去学习的技术!

Convert Localization Files From PHP to JSON in Laravel 6 Modules — Abandoned Due to Lack of Support

Found that the translation is not working properly

1. Currently, translation strings are stored in files under the resources/lang directory. In this directory, there should be a corresponding subdirectory for each language supported by the application:

Plaintext
/resources
    /lang
        /en
            validation.php
        /en-US
            validation.php

2. The previous calling method: return trans(‘online_store_theme_graphql::validation.custom.theme_editor_session.theme_editor_session_id_exists_rule’);

3. Plan to store translation files in JSON format in the resources/lang directory

Plaintext
/resources
    /lang
        /en
            validation.php
        /en-US
            validation.php
		en.json
		en-US.json

3. Deliberately differentiate between /en/validation.php and en.json to confirm the priority order. Confirmed that .php has a higher priority than .json. See Figure 1

Deliberately differentiate between /en/validation.php and en.json to confirm the priority order. Confirmed that .php has a higher priority than .json
Figure 1
PHP
<?php
 
return [
    'custom' => [
        'theme_editor_session' => [
            'theme_editor_session_id_exists_rule' => 'The selected :attribute is invalid.',
        ],
    ],
 
    'attributes' => [],
];
JSON
{
  "validation.custom.theme_editor_session.theme_editor_session_id_exists_rule": "The selected :attribute is invalid1."
}

4. Delete the PHP-related directories and files

Plaintext
/resources
    /lang
		en.json
		en-US.json

5. Found that the translation is not working properly. See Figure 2

Found that the translation is not working properly
Figure 2

6. Use the App Facade’s getLocale and isLocale methods to determine the current locale

PHP
        echo App::getLocale();
        exit;

JSON
{
  "error": {},
  "text": "en"
}

7. Referenced How to use json translation files using modules? (Translation Strings As Keys): https://github.com/nWidart/laravel-modules/issues/954 . Adjusted registerTranslations() in the service provider, removed the reference to PHP files, and adopted the reference to JSON files.

PHP
    /**
     * 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');
        }
    }

PHP
    /**
     * 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 properly. Finally decided to abandon this implementation for now.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系