CodeIgniter4模块语言文件调用失效问题咨询
Hey there! You totally don’t have to move all your translation files into the app-level Language folder—there are straightforward ways to make your module’s language files work properly.
The issue here is that CodeIgniter (I’m assuming you’re using v4, given your module structure) doesn’t automatically scan module language directories by default when you call lang('home.title'). It only looks in the app’s core Language folder unless you tell it otherwise. Here are your best solutions:
Option 1: Use Module Namespace Prefix in lang() Calls
This is the simplest approach. Since your translations are in the Admin module, just prepend the module’s namespace to your language key:
lang('Admin::home.title');
CodeIgniter will now look directly in Modules/Admin/Language/[current_lang]/home.php for the title key instead of the app-level folder. Just make sure your Admin module is properly registered in app/Config/Modules.php:
public $modules = [ 'Admin' => ROOTPATH . 'Modules/Admin', ];
Option 2: Register the Module’s Language Path Globally
If you don’t want to add the namespace prefix every time, you can register the module’s language path with CodeIgniter’s Language loader. You can do this in your module’s main config file (e.g., Modules/Admin/Config/Admin.php) or in the app’s BaseController:
// In your Admin module's config controller public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) { parent::initController($request, $response, $logger); // Add the module's language path $language = \Config\Services::language(); $language->addPath(ROOTPATH . 'Modules/Admin/Language'); }
Once registered, lang('home.title') will check both the app-level Language folder and your module’s Language folder for the translation key.
Either of these methods will fix the issue where you’re getting the raw home.title string instead of your translated content.
内容的提问来源于stack exchange,提问作者Ahmad Almosallam

