Git Product home page Git Product logo

Comments (15)

tuweifu avatar tuweifu commented on May 23, 2024

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

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

from framework.

ZoftTy avatar ZoftTy commented on May 23, 2024

这个问题能修复一下吗?

from framework.

hulang avatar hulang commented on May 23, 2024

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

from framework.

ZoftTy avatar ZoftTy commented on May 23, 2024

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

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

from framework.

hulang avatar hulang commented on May 23, 2024

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

from framework.

ahyunzhang avatar ahyunzhang commented on May 23, 2024

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

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

from framework.

hulang avatar hulang commented on May 23, 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);
    }

from framework.

ZoftTy avatar ZoftTy commented on May 23, 2024

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

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

可以解决

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

from framework.

ZoftTy avatar ZoftTy commented on May 23, 2024

2021年的 issue 都没有修

from framework.

hulang avatar hulang commented on May 23, 2024

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

from framework.

ZoftTy avatar ZoftTy commented on May 23, 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,
    // 其他中间件...
];

from framework.

hulang avatar hulang commented on May 23, 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\下有对应的语言包

from framework.

ZoftTy avatar ZoftTy commented on May 23, 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()

from framework.

hulang avatar hulang commented on May 23, 2024

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

from framework.

ZoftTy avatar ZoftTy commented on May 23, 2024

那就不用添加 extend_list

from framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.