muroi/phalcon-translate-adapter-nestednativearray

1.0.0 2016-09-21 06:56 UTC

This package is not auto-updated.

Last update: 2025-06-21 23:38:39 UTC


README

翻訳の変換リストを多次元配列で定義出来るように対応したアダプタ

Install

{
    "require": {
        "muroi/phalcon-translate-adapter-nestednativearray": "*"
    }
}

or

php composer.phar require muroi/phalcon-translate-adapter-nestednativearray

Usage

翻訳リストファイルを作成

app/messages/ja.php
app/messages/en.php
  • ファイルの中身
<?php
// app/messages/ja.php
$messages = [
    'test' => [
        'hello' => 'こんにちは'
    ],
    'test1' => [
        'test2' => [
            'test3' => 'テスト3'
        ]
    ]
];

コントローラで呼び出す

<?php

use Phalcon\Mvc\Controller;
use Muroi\Phalcon\Translate\Adapter\NestedNativeArray;

class UserController extends Controller
{
    protected function getTranslation()
    {
        // Ask browser what is the best language
        $language = $this->request->getBestLanguage();

        $translationFile = "app/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/messages/en.php";
        }

        // Return a translation object
        return new NestedNativeArray(
            [
                "content" => $messages,
            ]
        );
    }

    public function indexAction()
    {
        $this->view->t = $this->getTranslation();
    }
}

View

<!-- こんにちは -->
<p>{{ t._("test.hello") }}</p>

See

Phalcon Translate