Coder Social home page Coder Social logo

slim-twig-translate's Introduction

Slim Framework 4 Skeleton Application With Twig Template Engine and Illuminate Translation

It's a starter project which is a modified version of Slim-Skeleton. Added slim's official twig helper and translation extension with some modifications.

It can help to build small web applications which needs basic routing system, template engine and multilingual functionality.

Install the Application

Clone this repository.

git clone https://github.com/alperenersoy/slim-twig-translate.git
cd slim-twig-translate

Install dependencies

composer install

Configuration

You can configure twig and translation settings from app/settings.php

'twig' => [
    'templateDir' => '../templates',
    'cache' => false, /*'../var/cache'*/
],
'translator' => [
    'locale' => 'en',
    'fallback' => 'en',
    'folderPath' => '../lang'
]

Serve the Application

To serve this application, run serve command in public folder:

cd public
php -S localhost:8080

Translation Usage

Put your language files to the directory you specified. You can use JSON files such as lang/en.json or php files like lang/en/home.php.

In your twig template files, call these functions:

trans('key')
translate('key')
__('key')
trans_choice('key', $number) //returns singular or plural translation according to the number variable.

You can specify other parameters:

Replaceable variables

__("Our favorite color is :variable.",{'variable':"blue"})

Full usage ($key, array $replace = [], $locale = null, $fallback = true)

__("Our favorite color is :variable.", {'variable':"blue"}, "en", true)

Extra

Other Twig Functions

getLocale() //gets locale e.g. "en".
translator() //returns translator instance itself.
hasTranslation($key, $locale = null, $fallback = true) //checks if translation exists. returns true or false.

PHP CLI Command to Export Translations From Templates

This command helps to export translatable strings from your twig templates.

php command export-translations <templateDirectory> <languageDirectory> <targetLanguage>
#default example
php command export-translations templates lang en

This will create or update lang/en.json with untranslated keys.

slim-twig-translate's People

Contributors

alperenersoy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.