乐趣区

Luthier-CI-例子-Examples

例子 Examples

例子 # 1: 多语言网站 Multi-language website

这是一个示例,显示了由 URL 管理的多语言网站。中间件用于加载当前语言文件。

<?php
# application/routes/web.php

Route::get('/', function(){

    // Route "by default". This is a good place to request a cookie, session variable
    // or something that allows us to restore the last language of the user, or show a
    // language selection screen if no information is provided.

    redirect(route('homepage', ['_locale' => 'en']));
});

Route::group('{((es|en|it|br|ge)):_locale}', ['middleware' => ['Lang_middleware']], function(){Route::get('home', function(){var_dump( ci()->lang->line('test') );
    })->name('homepage');

    Route::get('about', function(){var_dump( ci()->lang->line('test') );
    })->name('about');

});
<?php
# application/middleware/Lang_middleware.php

class Lang_middleware
{public function run()
    {
        // Obtaining the value of the "_locale" sticky parameter
        $locale = ci()->route->param('_locale');

        $langs = [
            'es' => 'spanish',
            'en' => 'english',
            'it' => 'italian',
            'br' => 'portuguese-brazilian',
            'ge' => 'german',
        ];

        ci()->lang->load('test', $langs[$locale]);
    }
}
退出移动版