Internationalization & Localization Application with CakePHP 3

CakePHP has one of the coolest features to make application Internationalization & Localization. It’s really easy to translate your application or website into multiple languages with CakePHP 3.  Let’s see it.

First of all, create a directory Locale inside PROJECT_ROOT/src

Create a subdirectory of each language (which language you want to translate your application) inside PROJECT_ROOT/src/Locale/. Subdirectory name can be anything (ex. english, french, etc), but prefer ISO code of the language (ex. en_US, fr_FR, bn_BN etc). So follow this tutorial and make your subdirectory name en_USfr_FR, and bn_BN

Create default.po file inside all subdirectories (en_USfr_FR, and bn_BN). And copy the following code in the respective file.

src/Locale/en_US/default.po

msgid "message"
msgstr "Hello World"

 

src/Locale/fr_FR/default.po

msgid "message"
msgstr "Bonjour le monde"

 

src/Locale/bn_BN/default.po

msgid "message"
msgstr "ওহে বিশ্ব"

Here msgid key of which will use inside the template (view files) and msgstr is the values which will gonna translate.

So now you have three different languages (en_USfr_FR, and bn_BN). and one key (message) which can be translated into these three languages.

Now use <?php echo __(‘message’); ?> in your view (any ctp file of your application).

Then open PROJECT_ROOT/config/app.php file and find

 'defaultLocale' => env('APP_DEFAULT_LOCALE', 'fr_FR')

and replace with your language.

But if you want to translate language in runtime then you can use it

use Cake\I18n\I18n;

I18n::locale('fr_FR');

I hope this article will help you to translate your application in multiple language.

Host Static Website in Google App Engine

This article is for hosting your static website into Google App Engine. This article provides an app.yaml file so you can host your static website into Google App Engine. Before do anything make your website’s folder structure like that (see below image). So put all the static files inside assets folder (css, js, images, fonts) and all HTML files in project’s root directory. Inside assets folder css, js, img, and fonts are sperate folders for holding separate files (ex. css file stylesheets, img for images)

Host Static Website in Google App Engine

Then copy-paste this snipped in your YAML file. And then deploy (gcloud app deploy) your application.

You can also make your project structure your own way, then you have to change YAML content based on your changes. So that means need to change YAML information according to your project’s structure.

Create NGINX Virtual Host with PHP 7.0

Here I have shared a script for creating NGINX virtual host with PHP 7.0. NGINX virtual host with  5.x and 7.0 is very similar but only different is php5-fpm.sock location. To create a NGINX virtual host open the configuration files by running this command.

sudo gedit /etc/nginx/sites-available/default

And then copy-pastes this code snippet and modify it by your configuration (information). Then restart your server. And you are ready to go.

See it for creating virtual host with PHP 5.x

CakePHP 3 – DebugKit Toolbar Not Visible

Are you looking for the solution of DebugKit Toolbar not visible in CakePHP 3? These small tips will help you do that. Ensure you have pdo-sqlite installed & enabled on your machine. Cause debugKit by default uses a sqlite db. If not then follow this instruction.

At first install pdo-sqlite in your machine.

sudo apt-get install php7.0-sqlite

Then restart your server (apache server)

sudo service apache2 restart

Now that will help you to visible the DebugKit Toolbar

 

Change an Array Value if Duplicate

I have an array which holds the multiple same values in same categories. And I want to If the data is duplicate and the category is ‘Sales’ I want 1 of them to change to ‘Service’ and vice versa.

From above snippet, you can see ‘Lane Assist’ is duplicate in ‘Sales‘ category and ‘Bandenafdichtset’ is duplicate in ‘Service‘ category and I changed them as vice versa.

Configure Email Transporter in CakePHP Runtime

If you are looking for a solution to configure email transporter from CakePHP controller, components in run-time. This script will help you lot. This script is working for CakePHP 3.1.X – 3.3.9.

Email::configTransport('transporter_name', [
            'className' => 'Smtp',
            'host' => 'SMTP_HOST',
            'port' => 'SMTP_PORT',
            'timeout' => 30,
            'username' => 'SMTP_USERNAME',
            'password' => 'SMTP_PASSWORD',
        ]);
        
        $email = new Email();
        $email->transport('transporter_name');
        
        $email->from('no-replay@example.com')
            ->to('to@example.com')
            ->subject('Dummy Subject')
            ->message('This is a test message')
            ->send();

Simply copy paste this snippet and modified with real information and test it.

Getting Query String Values Without a Hash in the URL

$location.search() returns empty? Angular does not remove the query before the hash bang in URL. According to AngularJS documentation,  hash bang URL format is:

http://example.com/#!/bar?baz=23

So if you have no hash bang in your URL (e.g. http://example.com/bar?baz=23), and try to get query string value, then you can use this snippet.

function getQueryParams(queryParam, url) 
{
        var pageUrl = url || window.location.search.substring(1);
        var urlVariables = pageUrl.split(/[&||?]/);
        var response = null;

        for (var i = 0; i < urlVariables.length; i += 1) {
            var param = urlVariables[i];
            var paramName = (param || '').split('=');

            if (paramName[0] === queryParam) {
                response = paramName[1];
            }
        }
        return response;
}
    
console.log(getQueryParams('baz', 'http://example.com/bar?baz=23'));

This code is also with pure JavaScript (withour AngularJS).

Change Bower Default Directory And Folder

Bower is the client-side dependencies manager. The developer can easily manage their client-side dependencies with the Bower. By default when you install any package (e.g jQyery, Bootstrap etc) through Bower, then a bower_components folder will create in the project’s root directory and store all the files inside it. So the default directory of bower installation is ROOT and folder is bower_components
But if you want to change the Bower default directory and folder then you have to follow these following steps.

Remove bower_components (if exist) from your project root directory

 

Create .bowerrc file in your project’s root directory

 

Paste these code in .bowerrc file and modify DIRECTORY_NAME & FOLDER_NAME

{
  "directory": "DIRECTORY_NAME/FOLDER_NAME"
}

Then finally run bower install command.

Hope these will helpful to configure your Bower directory and folder name.

Avoid Too Much Complexity to the Code, Instead of Returning Often and Early.

Code readability is really important part of programming. The readable and cleaner code is easily manageable and customizable. The programmers don’t take it seriously but it’s important.  Actually, there is always an ongoing debate, but returning often, and returning early will make your code much cleaner looking and readable.

public function saveUser($data = [], $sendEmail)
    {
        // Checking data is exiest or not
        if ($data) {
            $isSaved = $this->UserModel->save($data);

            // Checking if the data is successfully stored or not.
            if ($isSaved) {
                //  If stored then doing some other work

                // send the email to the user
                if ($sendEmail) {
                    // send the email
                }

            } else {
                // Otherwise return false
                return false;
            }
        } else {
            // Otherwise return false
            return false;
        }
    }

If you look over the above code snippet, then you can see there is some nested if-else. And code quickly becomes unreadable and complex. You have to have a deep look to understand this sort of code.

public function saveUser($data = [], $sendEmail)
    {
        if(!$data)
        {
            // If data not exiest then return.
            return false;
        }

        $isSaved = $this->UserModel->save($data);
        if (!$isSaved) {
            // If data not saved then return.
            return false;
        }

        if (!$sendEmail) {
            // send the email
        }
        
        // If everything good, then return true.
        return true;
    }

Now see this code snippet, this code is much easier to read and very cleaner look. Both snippets are doing same work but the second one is much more readable and clearer than first one. And this can be easily manageable & customizable. so that avoid too much complexity to the code, instead of returning often and early.

Loading Model Inside Another Model and Controller in CakePHP 3

Sometimes it necessary to load a model inside another model as well as another controller in CakePHP. This article will help you to load a model from another model & controller with CakePHP 3

Load From Model:
$anotherTable = TableRegistry::get('AnotherTable');
$data = $anotherTable->find('list');

CakePHP TableRegistry is a factory of the Table object. It comes with a lot of static function and get() one of them. TableRegistry::get() provides an instance of Table. You can see an example from above snippet.

Load From Controller:
$this->loadModel('AnotherModel');
$data = $this->AnotherModel->find('list');

If you need to load a model from the controller which does not default model of the controller. You can use loadModel() function to get an instance of another model.