Dcat Admin 如何自定义翻译文件路径?

当前的翻译文件路径在 resources/lang 中,我的项目中将每个需求模块化了,项目中包含了很多admin文件夹,请问如何自定义翻译文件路径,使每个模块的翻译文件在不同的位置?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

我将各个需求的Admin放到了 nwidart/laravel-modules 扩展的各个模块中,语言文件也是用的各个模块自己的 Resources 目录下的。

他在 xxxServiceProvider 中加载了语言文件

/**
     * Register translations.
     *
     * @return void
     */
    public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/' . $this->moduleNameLower);

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
        }
    }

所以在admin控制器中只需要定义 $translation 就行了

protected $translation = 'workbench::work-bench-project';
2年前 评论
讨论数量: 6

file

这个符合要求吗?

2年前 评论
木偶 (楼主) 2年前

不知道你的具体使用情况,刚才试了下指定翻译文件目录的方法:

$translator = app("translator");
$translator->addNamespace("customNamespace", resource_path("customPath"));
$translated = $translator->get("customNamespace::password.reset");

加载方法的大致调用情况:

Illuminate\Translation\Translator@get
Illuminate\Translation\Translator@getLine
Illuminate\Translation\Translator@load

interface Illuminate\Contracts\Translation\Loader@load
Illuminate\Translation\FileLoader@load //从这个方法以及 loadNamespaced 可以看到是可以指定翻译文件目录的
Illuminate\Translation\FileLoader@loadNamespaced
2年前 评论
木偶 (楼主) 2年前
木偶 (楼主) 2年前

我将各个需求的Admin放到了 nwidart/laravel-modules 扩展的各个模块中,语言文件也是用的各个模块自己的 Resources 目录下的。

他在 xxxServiceProvider 中加载了语言文件

/**
     * Register translations.
     *
     * @return void
     */
    public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/' . $this->moduleNameLower);

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
        }
    }

所以在admin控制器中只需要定义 $translation 就行了

protected $translation = 'workbench::work-bench-project';
2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!