[Webman 1.3.14] Use custom translation loader

Toky

[Webman 1.3.14] Use custom translation loader

Sorry guys I don't speak Chinese. I want to implement a custom translation loader in my webman app. The goal is to get translations from a remote database.

My webman version

"workerman/webman-framework": "1.3.14",

My translation version

"symfony/translation": "^5.3",

My file structure

./src
├── app
│   ├── constant
│   ├── controller
│   ├── exception
│   ├── extension
│   │   ├── constant
│   │   ├── interfaces
│   │   ├── model
│   │   └── traits
│   ├── middleware
│   ├── model
│   │   ├── constant
│   │   │   └── properties
│   │   └── filter
│   ├── observer
│   ├── queue
│   │   └── redis
│   ├── service
│   ├── tools
│   ├── validation
│   └── view
│       └── index
├── config
├── coverage
│   └── phpunit
│       └── html
│           ├── _css
│           ├── _icons
│           └── _js
├── data
│   └── api-redis-service
│       ├── lib
│       └── redis.conf
├── database
│   ├── migrations
│   └── seeds
│       ├── mails
│       │   ├── en
│       │   └── fr
│       └── mails.prod.10-2023
│           ├── en
│           └── fr
├── docker
│   └── php
├── process
├── public
├── resource
│   ├── files
│   │   ├── absence_demands
│   │   ├── exports
│   │   │   ├── absence_amounts
│   │   │   ├── absence_demands
│   │   │   └── absence_demands_waiting_validation
│   │   ├── imports
│   │   │   └── absence_amounts
│   │   └── ml_data
│   │       ├── doc
│   │       ├── docx
│   │       ├── gif
│   │       ├── jpeg
│   │       ├── jpg
│   │       ├── pdf
│   │       └── png
│   └── translations
│       ├── en
│       └── fr
├── runtime
│   ├── annotation
│   ├── cache
│   ├── logs
│   ├── sessions
│   └── views
├── support
│   ├── bootstrap
│   │   └── db
│   ├── command
│   │   ├── oga
│   │   │   └── translation
│   │   ├── phinx
│   │   └── workerman
│   │       └── process
│   ├── events
│   ├── exception
│   ├── middleware
│   └── view
└── test
    ├── absences
    │   ├── agenda
    │   └── demands
    │       └── validations
    ├── auth
    └── users

92 directories

What I have tried

I already tried to change the src/support/bootstrap/Translation.php file, to load all translation messages form database but it seems not working, It seems to always load the translations from the resources directory.

Here is the content of my src/support/bootstrap/Translation.php now :

<?php

/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace support\bootstrap;

use Webman\Bootstrap;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\PhpFileLoader;

/**
 * Class Translation
 * @package support\bootstrap
 * @method static string trans(?string $id, array $parameters = [], string $domain = null, string $locale = null)
 * @method static void setLocale(string $locale)
 * @method static string getLocale()
 */
class Translation implements Bootstrap
{

    /**
     * @var array
     */
    protected static $_translator = [];

    /**
     * @param \Workerman\Worker $worker
     * @return void
     */
    public static function start($worker)
    {
        if (!class_exists('\Symfony\Component\Translation\Translator')) {
            return;
        }
        $config = config('translation', []);
        if (!$translations_path = realpath($config['path'])) {
            return;
        }

        static::$_translator = $translator = new Translator($config['locale']);
        $translator->setFallbackLocales($config['fallback_locale']);

        $classes = [
            \app\extension\DatabaseTranslationLoader::class => [
                'extension' => '.db',
                'format' => 'custom_db',
                'type' => 'db',
            ],
            /*\Symfony\Component\Translation\Loader\PhpFileLoader::class => [
                'extension' => '.php',
                'format' => 'phpfile',
                'type' => 'file',
            ],
            \Symfony\Component\Translation\Loader\PoFileLoader::class => [
                'extension' => '.po',
                'format' => 'pofile',
                'type' => 'file',
            ],*/
        ];

        foreach ($classes as $class => $opts) {
            $translator->addLoader($opts['format'], new $class);
            if ($opts['type'] == 'db') {
                $dbLocale = \app\extension\DatabaseTranslationLoader::loadLocaleFromDb();
                $dbDomain = \app\extension\DatabaseTranslationLoader::loadDomainFromDb();
                foreach ($dbLocale as $oneLocale) {
                    foreach ($dbDomain as $oneDomain) {
                        $resourceName = $opts['format'] . "_" . $oneLocale . "_" . $oneDomain;
                        $translator->addResource(
                            format: $opts['format'],
                            resource: $resourceName,
                            locale: $oneLocale,
                            domain: $oneDomain
                        );
                    }
                }
            } else {
                foreach (glob($translations_path . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . '*' . $opts['extension']) as $file) {
                    $domain = basename($file, $opts['extension']);
                    $dir_name = pathinfo($file, PATHINFO_DIRNAME);
                    $locale = substr(strrchr($dir_name, DIRECTORY_SEPARATOR), 1);
                    if ($domain && $locale) {
                        $translator->addResource($opts['format'], $file, $locale, $domain);
                    }
                }
            }
        }
    }

    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        return static::$_translator->{$name}(...$arguments);
    }
}
466 0 1
0个回答

年代过于久远,无法发表回答
×
🔝