Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Localization


Documentation



Localization

Cygnite provides a convenient way to localize your application. Localization library retrieves strings in various languages from the language files and allow your application to support multiple languages.

Language message strings are stored in files inside src/Apps/Resources/Languages/ directory. There should be sub-directory for each language supported by your application. And each language directory will have a language file which simply return an array of keyed strings. For example:



  /Apps/Resources
      /Languages
        /en
            messages.php
        /es
            messages.php   

  
 return [
    "accepted"    => "The :attribute must be accepted.",
    'custom'      => [
        'welcome' => 'Welcome to our Application'
    ]
 ];

Configuring Translator

The default language settings for your application is done in the src/Apps/Configs/application.php file. You can modify the settings as you need. By default language feature is not activate, you can activate at runtime by calling setLocale method on the application instance in the src/Apps/Routing/Routes.php file.

  
 $app->setLocale();//will set default locale configured in your application.php file

You may override locale for your particular request.

  
 $app->setLocale($language); // override default language
 
 or
  
 $app->router->get('/welcome/{:name}', function ($router, $lang) {

    app()->getTranslator()->locale($lang); 
    show(trans('message:url', ['{attribute}' => 'user'])); // Will use requested language from url
    //


 });
 

You may also configure a "fallback language" in src/Apps/Configs/application.php file for your application. Fallback language will be used when configured language doesn't contain a language file.

  
 'fallback.locale' => 'en',


You may also get the current locale used for your application by calling locale() method.

  
  app()->getTranslator()->locale(); // will return the lang 


Usage

You may retrieve strings from the languages files using trans helper function or Translator::get() method. The trans function accepts the file name followed by key of the language string as first argument. For example:

  
  echo trans('message:accepted'); // The {attribute} must be accepted.


In the above example, it will display the language line accepted in the src/Apps/Resources/Languages/en/message.php language file.

Replacing Parameters In Language Strings

Sometime you may wish to define placeholder in your language strings and those placeholder can be dynamically replaced. All strings inside {} are considered as placeholder and will be replaced by the given value. For example, you may wish to replace a name in the greet message.

  
 'greet' => 'Hello! {name}',


To replace the place-holders when retrieving a language string, you have to pass second parameter as array to the trans function.

  
 echo trans('message:greet',  ['{user}' => 'Jhon']); // Hello! Jhon


Follow Us On Facebook Twitter Google+ Linkedin
Released Under The MIT Public License. Copyrights @2012-2017. Powered by- Sanjoy Dey Productions.