定制 dkesberg/slim-twig-translation-extension 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dkesberg/slim-twig-translation-extension

Composer 安装命令:

composer require dkesberg/slim-twig-translation-extension

包简介

Twig function for Illuminate\Translation\Translator in Slim framework

README 文档

README

This repository provides a twig extension class for the twig view parser. The class adds a translate helper function for the use in twig templates. The translator function tries to call the trans() function of an Illuminate\Translation\Translator object in the slim container.

How to install

Using Composer

Create a composer.json file in your project root:

{
    "require": {
        "dkesberg/slim-twig-translation-extension": "v0.1.0"
    }
}

Then run the following composer command:

$ php composer.phar install

How to use

Slim

Set up your twig views as described in the SlimViews Repository. Add the extension to your parser extensions.

$view->parserExtensions = array(
    new \Dkesberg\Slim\Twig\Extension\TranslationExtension(),
);

Twig template

In your twig template you would write:

  {{ translate('mails.salutation.male') }}

You can also use the shorthand:

  {{ _('mails.salutation.male') }}

Adding Illuminate/Translation/Translator to slim

Simple injection:

use Illuminate\Translation\Translator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;

$translator = new Translator(new FileLoader(new Filesystem(), __DIR__ . '/lang'), 'en');
$translator->setFallback('en');
$app->translator = $translator;

Singleton ressource:

use Illuminate\Translation\Translator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;

$app->container->singleton('translator', function() {
  return new Translator(new FileLoader(new Filesystem(), __DIR__ . '/lang'), 'en');
});
$app->translator->setFallback('en');

Using slim hooks and singleton:

use Illuminate\Translation\Translator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;

// detect language and set translator
$app->hook('slim.before', function() use ($app) {
  $env = $app->environment();
  
  // Extract locale
  $locale = Locale::acceptFromHttp($env['HTTP_ACCEPT_LANGUAGE']);
  $app->locale = substr($locale,0,2);

  // Set translator instance
  $app->container->singleton('translator', function($app) {
    return new Translator(new FileLoader(new Filesystem(), __DIR__ . '/lang'), $app->locale);
  });
  $app->translator->setFallback('en');
});

Language files

I am wrapping the Illuminate\Translation\Translator. The language files for the translator use the same structure used in the Laravel framework. For more information see also: http://laravel.com/docs/4.2/localization

In the lang directory there should be a subdirectory for each language.

/lang
       /en
            mails.php
            validation.php
       /de
            mails.php
            validation.php

The language files simply return an array of keyed strings.

<?php

return array(
  'salutation' => array(
    'male'    => 'Dear Mr.',
    'female'  => 'Dear Mrs.',
    'unknown' => 'Dear Sir or Madam'
  )
)

The first part of the string passed to the translate function is the name of the language file, the second part is the key you want to retrieve. The second part uses the dot notation for multidimensional arrays.

{{ _('mails.salutation.male') }}

统计信息

  • 总下载量: 1.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固