Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php8.1,thinkphp8.0,多应用模式下,多语言不能正常使用 #2953

Open
tuweifu opened this issue Nov 15, 2023 · 15 comments · May be fixed by top-think/think-multi-app#46
Open

php8.1,thinkphp8.0,多应用模式下,多语言不能正常使用 #2953

tuweifu opened this issue Nov 15, 2023 · 15 comments · May be fixed by top-think/think-multi-app#46

Comments

@tuweifu
Copy link

tuweifu commented Nov 15, 2023

PHP环境:PHP Version 8.1.7
框架版本:ThinkPHP 8.0
出现的异常:
1、多应用模式下,在全局配置中开启多语言,在全局中间件文件中添加了语言包的中间件定义。然后在页面传入lang=en测试发现多语言切换无效。检查cookie发现cookie值已写入 en-us,但是使用lang输出的还是中文。检查多次语言包你让和lang.php配置都正常。
2、然后我尝试将中间件中的语言包定义,移到单个应用中的中间件文件中定义。发现又正常了。然后我尝试将全局配置lang.php放到多应用的应用中配置中。发现应用配置的lang.php不会生效。语言包的cookie名称等还是使用全局文件的。
3、后面找到一篇类似问题的反馈:https://blog.csdn.net/qq_24118445/article/details/132461009 ,于是我尝试将
\vendor\topthink\framework\src\think\App.php 中的 $this->loadLangPack(); 注释,再返回测试。发现一切都正常了。多应用中应用配置生效了,语言包也正常切换了。

@tuweifu tuweifu changed the title 在php8.1下,thinkphp8.0多语言不能正常使用 php8.1,thinkphp8.0,多应用模式下,多语言不能正常使用 Nov 15, 2023
@tuweifu
Copy link
Author

tuweifu commented Nov 15, 2023

在多應用模式下,不要在 app/middleware.php 的地方載入 \think\middleware\LoadLangPack::class
改成在 app/[多應用目錄]/middleware.php 的地方載入

但是在 app/[多應用目錄]/middleware.php 下载入后,应用的 lang.php 配置不会生效,只能用全局的 lang.php

@ZoftTy
Copy link

ZoftTy commented Apr 8, 2024

这个问题能修复一下吗?

@hulang
Copy link

hulang commented Apr 21, 2024

我也困扰了很久了。。按照楼主的,成功了,改文件了无法官方无缝升级呢。。这又是问题。

@ZoftTy
Copy link

ZoftTy commented Apr 22, 2024

我也困扰了很久了。。按照楼主的,成功了,改文件了无法官方无缝升级呢。。这又是问题。

可以按照二楼的方法来,不用修改文件,只不过需要在每一个模块中都引入多语言中间件。

@hulang
Copy link

hulang commented Apr 24, 2024

@ZoftTy 一样的,我按照二楼的,还是没有成功。。。

@ahyunzhang
Copy link

我也困扰了很久了。。按照楼主的,成功了,改文件了无法官方无缝升级呢。。这又是问题。

配置文件中的default_lang 设置为空 可以解决

@hulang
Copy link

hulang commented Apr 26, 2024

@ahyunzhang 有具体的示例吗?
还有:xxx?lang=en-us
xxx方法里面应该怎么写的?我什么都没有写啊。

    /**
     * 切换语言
     */
    public function set()
    {
        $json = [];
        $json['code'] = 0;
        //
        $lang = input('get.lang');
        if (!in_array($lang, config('lang.allow_lang_list'))) {
            $lang = 'en-us';
        }
        //
        $json['code'] = 1;
        $json['lang'] = $lang;
        return json($json);
    }

@ZoftTy
Copy link

ZoftTy commented Apr 28, 2024

我也困扰了很久了。。按照楼主的,成功了,改文件了无法官方无缝升级呢。。这又是问题。

配置文件中的default_lang 设置为空 可以解决

可以解决

相同问题: top-think/think-multi-app#32 (comment)

@ZoftTy
Copy link

ZoftTy commented Apr 28, 2024

2021年的 issue 都没有修

@hulang
Copy link

hulang commented Apr 28, 2024

@ZoftTy 哥们,能做一个多应用的切换demo吗?demo应该就几个文件吧。。中间件如何设置啊。。

@ZoftTy
Copy link

ZoftTy commented Apr 28, 2024

@ZoftTy 哥们,能做一个多应用的切换demo吗?demo应该就几个文件吧。。中间件如何设置啊。。

PHP版本: 8.2.16
Thinkphp 版本: 8.0.3
topthink/think-multi-app 版本: 1.0.17

两种方法

  1. 在每个模块中引入多语言中间件

    文件路径: app/模块路径/middleware.php

<?php
// app/模块路径/middleware.php

namespeace app\模块名;

return [
    // 多语言加载
    \think\middleware\LoadLangPack::class,
    // 其他中间件...
];
  1. 修改 default_lang 为空字符串

    1. 打开文件 config/lang.php
    2. 修改 default_lang 的值为 ''
    3. app\middleware.php 中引入多语言中间件
    4. 注意请求时需带上多语言参数,如下配置是 lang,因为没有了默认语言
<?php
// config/lang.php
// +----------------------------------------------------------------------
// | 多语言设置
// +----------------------------------------------------------------------

return [
    // 默认语言
    'default_lang' => '',
    // 允许的语言列表
    'allow_lang_list' => [],
    // 多语言自动侦测变量名
    'detect_var' => 'lang',
    // 其他参数...
];
<?php
// app/middleware.php

return [
    // 多语言加载
    \think\middleware\LoadLangPack::class,
    // 其他中间件...
];

@hulang
Copy link

hulang commented Apr 28, 2024

@ZoftTy 按方法1,只能切换:中文、英文,不能切换更多的。。
我的app\index\config\lang.php

<?php
// +----------------------------------------------------------------------
// | 多语言设置
// +----------------------------------------------------------------------

return [
    // 默认语言
    'default_lang'    => 'en-us',
    // 允许的语言列表
    'allow_lang_list' => ['zh-cn', 'en-us', 'fr-fr', 'de-de', 'ja-jp', 'it-it', 'ko-kr', 'tr-tr'],
    // 多语言自动侦测变量名
    'detect_var'      => 'lang',
    // 是否使用Cookie记录
    'use_cookie'      => 1,
    // 多语言cookie变量
    'cookie_var'      => 'think_lang',
    // 扩展语言包
    'extend_list'     => [
        'zh-cn' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'zh-cn', 'index.php']),
        ],
        'en-us' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'en-us', 'index.php']),
        ],
        'fr-fr' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'fr-fr', 'index.php']),
        ],
        'de-de' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'de-de', 'index.php']),
        ],
        'ja-jp' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'ja-jp', 'index.php']),
        ],
        'it-it' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'it-it', 'index.php']),
        ],
        'ko-kr' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'ko-kr', 'index.php']),
        ],
        'tr-tr' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'tr-tr', 'index.php']),
        ],
    ],
    // Accept-Language转义为对应语言包名称
    'accept_language' => [
        'zh-hans-cn' => 'zh-cn',
    ],
    // 是否支持语言分组
    'allow_group'     => 1,
];

文件夹\app\index\lang\下有对应的语言包

@ZoftTy
Copy link

ZoftTy commented Apr 28, 2024

@ZoftTy 按方法1,只能切换:中文、英文,不能切换更多的。。 我的app\index\config\lang.php

<?php
// +----------------------------------------------------------------------
// | 多语言设置
// +----------------------------------------------------------------------

return [
    // 默认语言
    'default_lang'    => 'en-us',
    // 允许的语言列表
    'allow_lang_list' => ['zh-cn', 'en-us', 'fr-fr', 'de-de', 'ja-jp', 'it-it', 'ko-kr', 'tr-tr'],
    // 多语言自动侦测变量名
    'detect_var'      => 'lang',
    // 是否使用Cookie记录
    'use_cookie'      => 1,
    // 多语言cookie变量
    'cookie_var'      => 'think_lang',
    // 扩展语言包
    'extend_list'     => [
        'zh-cn' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'zh-cn', 'index.php']),
        ],
        'en-us' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'en-us', 'index.php']),
        ],
        'fr-fr' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'fr-fr', 'index.php']),
        ],
        'de-de' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'de-de', 'index.php']),
        ],
        'ja-jp' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'ja-jp', 'index.php']),
        ],
        'it-it' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'it-it', 'index.php']),
        ],
        'ko-kr' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'ko-kr', 'index.php']),
        ],
        'tr-tr' => [
            app()->getAppPath() . join(DIRECTORY_SEPARATOR, ['lang', 'tr-tr', 'index.php']),
        ],
    ],
    // Accept-Language转义为对应语言包名称
    'accept_language' => [
        'zh-hans-cn' => 'zh-cn',
    ],
    // 是否支持语言分组
    'allow_group'     => 1,
];

文件夹\app\index\lang\下有对应的语言包

app()->getAppPath() 此时返回的路径为 /app/index/

extend_list 语言文件的完整路径 /app/index/lang/fr-fr.php

然鹅 app/index/lang 中的文件不需要再次添加到 extend_list

但是这也解释不了你为啥用不了其他语言。

如果你的 extend_list 想添加的是 app/lang 下面的语言包,请使用 app()->getBasePath()

@hulang
Copy link

hulang commented Apr 28, 2024

@ZoftTy 只是针对的:index应用。。。

@ZoftTy
Copy link

ZoftTy commented Apr 28, 2024

那就不用添加 extend_list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants