Hiển thị các bài đăng có nhãn Multi Language. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Multi Language. Hiển thị tất cả bài đăng

Thứ Sáu, 28 tháng 9, 2018

Đa ngôn ngữ trong Phalcon

1. Tạo thư mục mesages trong thư mục app


2. Trong Controller Base thêm function

protected function getTranslation(){    // Ask browser what is the best language    $language = $this->request->getBestLanguage();    $messages = [];
    $translationFile = APP_PATH . '/messages/' . $language . '.php';

    // Check if we have a translation file for that lang    if (file_exists($translationFile)) {        require $translationFile;    } else {        // Fallback to some default        require APP_PATH . '/messages/en.php';    }
    // Return a translation object $messages comes from the require    // statement above    return new NativeArray(        [            'content' => $messages,        ]    );}

3. Tạo file ngôn ngữ trong thư mục message, ví dụ en.php cho ngôn ngữ tiếng Anh, vi.php cho ngôn ngữ tiếng Việt

en.php
<?php
$messages = [
    'index' =>'Index',    'create' =>'Crate',    'add' => 'Add',    'edit' => 'Edit',    'delete' => 'Delete',   
];

vi.php

<?php

$messages = [    /************* CRUD **************/    'index' =>'Danh sách',    'create' =>'Tạo Mới',    'add' => 'Thêm Mới',    'edit' => 'Chỉnh Sữa',    'delete' => 'Xóa',
];


Note: biến $messages trong docs của phalcon để là $mesagesContent nếu để theo như vậy sẽ không chạy

4. Trong Controller cần xài:

class CityController extends ControllerBase{

    public function IndexAction(){   
        $this->view->cities = $cities;
        $this->view->t      = $this->getTranslation();

    }
}

4. Chỉnh ngôn ngữ của trình duyệt web trùng với ngôn ngữ muốn hiển thị là được


Note: còn 1 cách multi language theo routing nữa

Bài 4- Volt Filter -phalcon

  1/ Volt Filter Đây cũng là một bộ lọc giống như phần Filter ở bài 3 , tuy nhiên có bổ sung thêm một số phương thức mới để áp dụng vào ứng ...