Build a localized app with Laravel - Part 4: Frontend with multi-language styling

Introduction

In this final tutorial of the series, we will focus on making the frontend of the application. We will build a simple destination page and customize it for each language using css.

In the previous chapters, we looked at what an international application is and the different things to consider when making one. We started building our tour guide application and made the backend of the application. We also added multilingual support for the basic pages and content.

Prerequisites

You have read all previous chapters.

Here are the part 1, part 2 and part 3 in case you missed it!

Getting started

In this guide, we will look at how to use CSS to adjust our display based on the app language. We will make the pages for the application and generate different language versions of the page where necessary.

Before we proceed, we need to create bookings and a destinations folder inside our views folder.

1$ mkdir resources/views/booking
2    $ mkdir resources/views/destination

Next, the following files in ./resources/views

1$ touch resources/views/booking/index.blade.php
2    $ touch resources/views/booking/create.blade.php
3    $ touch resources/views/booking/userpage.blade.php
4    $ touch resources/views/destination/index.blade.php
5    $ touch resources/views/destination/show.blade.php

Next, we need to make a few adjustments. The register page generated by the Laravel auth scaffolding does not have fields for phone and country. Let us add that to the page before we continue with other pages.

Open ./resources/views/auth/register.blade.php and add the following:

1// resources/views/auth/register.blade.php
2    [...]
3    
4    <form method="POST" action="{{ route('register') }}">
5    @csrf
6    
7    [...]
8    
9    <div class="form-group row">
10        <label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('Phone') }}</label>
11    
12        <div class="col-md-6">
13            <input id="phone" type="text" class="form-control{{ $errors->has('phone') ? ' is-invalid' : '' }}" name="phone" value="{{ old('phone') }}" required autofocus>
14    
15            @if ($errors->has('phone'))
16                <span class="invalid-feedback">
17                    <strong>{{ $errors->first('phone') }}</strong>
18                </span>
19            @endif
20        </div>
21    </div>
22    
23    <div class="form-group row">
24        <label for="country" class="col-md-4 col-form-label text-md-right">{{ __('Country') }}</label>
25    
26        <div class="col-md-6">
27            <input id="country" type="text" class="form-control{{ $errors->has('country') ? ' is-invalid' : '' }}" name="country" value="{{ old('country') }}" required autofocus>
28    
29            @if ($errors->has('country'))
30                <span class="invalid-feedback">
31                    <strong>{{ $errors->first('country') }}</strong>
32                </span>
33            @endif
34        </div>
35    </div>
36    
37    [...]
38    </form>
39    
40    [...]

Then, add the routes for all our pages. Open ./routes/web.php and edit as follows:

1// routes/web.php
2    Route::get('/', function () {
3        return view('welcome');
4    });
5    
6    
7    Route::prefix('{lang}')->group(function () {
8            Route::get('/destinations', "DestinationController@index");
9        Route::get('/destinations/{destination}', "DestinationController@show");
10    });
11    
12    Auth::routes();
13    Route::get('/booking/destination/{destination}', "BookingController@create");
14    Route::post('/booking', "BookingController@store");
15    Route::get('/dashboard', "BookingController@index");
16    Route::get('/home', 'BookingController@userPage')->name('home');

Also, we need to create an admin user that can see all the booking requests. We will seed the user table with the admin user information.

Run the following command to create the database seeder:

    $ php artisan make:seed UserTableSeeder

Then, open the database/seeds/UserTableSeeder.php file and replace with the following:

1// database/seeds/UserTableSeeder.php
2    <?php
3    
4    use Illuminate\Database\Seeder;
5    use App\User;
6    use Illuminate\Support\Facades\Hash;
7    
8    class UserTableSeeder extends Seeder
9    {
10        /**
11         * Run the database seeds.
12         *
13         * @return void
14         */
15        public function run()
16        {
17            $user = new User;
18            $user->name = "Admin";
19            $user->country = "Canada";
20            $user->phone = "12345678";
21            $user->email = "admin@example.com";
22            $user->password = Hash::make("secret");
23            $user->is_admin = true;
24            $user->save();
25        }
26    }

Run the seeder:

    $ php artisan db:seed --class=UserTableSeeder

Making the destinations pages

The destinations pages are the primary pages of our application. They hold key information that will enable visitors to decide to use our service. We want to modify the pages slightly for different visitors to improve their experience and enable them to make buying decisions.

We will assume the following:

  • The Germans and French like their websites being a representation of their national flags.
  • The rest of the world will go with our default design.

We will use the colors of the flags of France and Germany to make the style variations on the pages.

Create a file style.css in the ./public/css directory with this command directory

    $ touch public/css/style.css

Paste in the following lines of code into our style.css file:

1:lang(de) body{
2            background: #000000;
3            color:#FFCE00;
4    }
5    :lang(fr) body{
6            background: #0055A4;
7            color:#ffffff;
8    }
9    .text-tiny {
10            font-size: 1rem;
11    }
12    :lang(fr) .text-tiny {
13            background: #EF4135;
14            color:#ffffff;
15            font-weight: 900;
16            padding: 0px 10px;
17    }
18    :lang(fr) .col-md-4 a, :lang(fr) .col-md-12 a {
19            color:#ffffff;
20            text-decoration: underline;
21            font-weight: 900;
22    }
23    :lang(fr) .col-md-4 a:hover, :lang(fr) .col-md-12 a:hover {
24            color:#EF4135;
25    }
26    :lang(de) .text-tiny {
27            background: #DD0000;
28            color:#ffffff;
29            font-weight: 900;
30            padding: 0px 10px;
31    }

In the above style, we used the :lang() selector to apply unique styles to portions of our page based on the language of the page. We made the backgrounds take the first color of the respective flags and the other two colors were used for text and highlighting.

The lang() selector can be used on any tag or class. This way, you can target specific parts of your webpage and style them differently when the page is in a particular language.

<html lang="{{ app()->getLocale() }}"> is a very important part of our application. It sets the language of the current webpage a user is viewing. It is also what the lang() CSS selector uses to detect the language of the page, to style it accordingly. If you omit it, language dependent styles on your web pages will not apply.

The lang attribute can be set on any tag. The tag can also have a different language from the rest of the webpage. While it is possible to have multiple languages on the same page and the browser will know about them, it is a strongly discouraged practice. It impacts the experience of the user and can impact on what the user receives if she translates your webpage into an entirely different language.

Open the destination/index.blade.php file and add the following:

1@extends('layouts.app')
2    @section('styles')
3        <link href="{{ asset('css/style.css') }}" rel="stylesheet">
4    @endsection
5    @section('content')
6    <div class="container">
7            <div class="row">
8                    @foreach($destinations as $destination)
9                    <div class="col-md-4">
10                            <img src="{{$destination->image}}" class="img img-fluid">
11                            <h2>{{$destination->name}} <span class="float-right text-tiny">{{$destination->location}}</span></h2>
12                            <hr/>
13                            <a href="{{url(app()->getLocale().'/destinations/'.$destination->id)}}">{{__('View More')}}</a>
14                    </div>
15                    @endforeach
16            </div>
17    </div>
18    @endsection

We extended the main app container file and added two sections. One for styles and the other for content. The page is simple. It lists out all the destinations we have and puts a link to view more on each destination.

The __() method, as we explained in the last chapter, handles translations. The string maps we created for the French-English texts are used to find replacements for the English word when the language of the page is in French. Similarly, the string map for German-English will be used when the page is in German. The French string map is in fr.json file in the ./resources/lang directory.

Now, open the layouts/app.blade.php file and add the yield for styles and scripts

1<!DOCTYPE html>
2    <html lang="{{ app()->getLocale() }}">
3    <head>
4        [...]
5        <!-- Styles -->
6        <link href="{{ asset('css/app.css') }}" rel="stylesheet">
7        @yield('styles')
8    </head>
9    [...]
10            <main class="py-4">
11                @yield('content')
12            </main>
13        </div>
14        @yield('scripts')
15    </body>
16    </html>

Finally, open the destination/show.blade.php file and add the following:

1@extends('layouts.app')
2    @section('styles')
3        <link href="{{ asset('css/style.css') }}" rel="stylesheet">
4    @endsection
5    @section('content')
6    <div class="container">
7            <div class="row">
8                    <div class="col-md-12">
9                            <img src="{{$destination->image}}" class="img img-fluid">
10                            <h2>{{$destination->name}} <span class="float-right text-tiny">{{$destination->location}}</span></h2>
11                            <hr/>
12                            <p>{{$destination->translated_description}}</p>
13                            <a href="{{url('booking/destination/'.$destination->id)}}">{{__('Book Now')}}</a>
14                    </div>
15            </div>
16    </div>
17    @endsection

Here, we have displayed the content of each destination and added a link to book it. That’s it for the destination pages.

Making the tour booking page

The tour booking page is going to be simple. It should show the destination information a user clicked on and a little form for the user to provide additional information for the booking. We will translate the page based on language as well.

Open the booking/create.blade.php file and add the following:

1@extends('layouts.app')
2    @section('styles')
3    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.min.css">
4    @endsection
5    @section('content')
6    <div class="container">
7        <div class="row justify-content-center">
8            <div class="col-md-8">
9                <div class="card">
10                    <div class="card-header">{{__('Make a booking')}}</div>
11    
12                    <div class="card-body">
13                        <div class="row">
14                            <div class="col-md-6">
15                                <img src="{{$destination->image}}" class="img img-fluid">
16                                <h2>{{$destination->name}}</h2>
17                                <small>{{$destination->location}}</small>
18                            </div>
19                            <div class="col-md-6">
20                                <form method="post" action="{{url('booking')}}">
21                                    @csrf
22                                    <input type="hidden" name="destination_id" value="{{$destination->id}}">
23                                    <div class="form-group row">
24                                        <div class="col-md-12">
25                                            <label for="number_of_tourists">
26                                                {{__('How many people are coming?')}}
27                                            </label>
28                                            <input id="number_of_tourists" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="number_of_tourists" value="{{ old('number_of_tourists') }}" required autofocus placeholder="{{__('e.g')}} 4">
29    
30                                            @if ($errors->has('name'))
31                                                <span class="invalid-feedback">
32                                                    <strong>{{ $errors->first('number_of_tourists') }}</strong>
33                                                </span>
34                                            @endif
35                                        </div>
36                                    </div>
37    
38                                    <div class="form-group row">
39                                        <div class="col-md-12">
40                                            <label for="visit_date">
41                                                {{__('When would you like to visit?')}}
42                                            </label>
43                                            <div class="input-group date" data-provide="datepicker">
44                                                <input type="text" class="form-control datepicker" required autofocus placeholder="{{__('e.g')}} 01/26/2019" name="visit_date">
45                                                <div class="input-group-addon">
46                                                    <span class="glyphicon glyphicon-th"></span>
47                                                </div>
48                                            </div>
49    
50                                            @if ($errors->has('name'))
51                                                <span class="invalid-feedback">
52                                                    <strong>{{ $errors->first('number_of_tourists') }}</strong>
53                                                </span>
54                                            @endif
55                                        </div>
56                                    </div>
57                                    <div class="form-group row">
58                                        <div class="col-md-12">
59                                            <button type="submit" class="btn btn-primary">
60                                                {{ __('Book Now') }}
61                                            </button>
62                                        </div>
63                                    </div>
64                                </form>
65                            </div>
66                        </div>
67                    </div>
68                </div>
69            </div>
70        </div>
71    </div>
72    @endsection
73    @section('scripts')
74    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js" defer>
75        $(document).ready(function() {
76            $('.datepicker').datepicker();
77        });
78    </script>
79    @endsection

On the page, we used bootstrap-datepicker package to make it easy for our visitors to select the date of their booking in a way our application can easily interpret.

Making the user page for viewing bookings

This page is like the mission control for a user. It helps the user see all the tours they have previously booked. It does not do so much, but it is informative enough for the user.

Open the booking/userpage.blade.php file and add the following:

1@extends('layouts.app')
2    
3    @section('content')
4    <div class="container">
5        <div class="row justify-content-center">
6            <div class="col-md-8">
7                <div class="card">
8                    <div class="card-header">Dashboard</div>
9    
10                    <div class="card-body">
11                        @if(empty($bookings))
12                        <h3>{{__('You do not have any reservations')}}</h3>
13                        @else
14                        @foreach($bookings as $booking)
15                        <div class="col-md-12">
16                            <div class="row">
17                                <div class="col-md-6">
18                                    <img src="{{$booking->destination->image}}" class="img img-fluid">
19                                    <h2>{{$booking->destination->name}}</h2>
20                                    <small>{{$booking->destination->location}}</small>
21                                </div>
22                                <div class="col-md-6">
23                                    <h5>{{__('Number of tourists')}}: <br/><strong>{{$booking->number_of_tourists}}</strong></h5>
24                                    <h5>{{__('Tour date')}}: <br/><strong>{{$booking->visit_date->toDateString()}}</strong></h5>
25                                </div>
26                            </div>
27                        </div>
28                        @endforeach
29                        @endif
30                    </div>
31                </div>
32            </div>
33        </div>
34    </div>
35    @endsection

Making the admin page to view bookings

This page is like mission control for the admin user. It helps the admin see all the tours users have booked and information on the users who booked these tours. We will not offer translations for this page. We assume the administrator speaks English.

Open the booking/index.blade.php file and add the following:

1@extends('layouts.app')
2    
3    @section('content')
4    <div class="container">
5        <div class="row justify-content-center">
6            <div class="col-md-8">
7                <div class="card">
8                    <div class="card-header">Admin Dashboard</div>
9    
10                    <div class="card-body">
11                        @if(empty($bookings))
12                        <h3>{{__('You do not have any reservations')}}</h3>
13                        @else
14                            @foreach($bookings as $booking)
15                        <div class="col-md-12">
16                            <div class="row">
17                                <div class="col-md-6">
18                                    <img src="{{$booking->destination->image}}" class="img img-fluid">
19                                    <h2>{{$booking->destination->name}}</h2>
20                                    <small>{{$booking->destination->location}}</small>
21                                </div>
22                                <div class="col-md-6">
23                                    <h5>Number of tourists: <br/><strong>{{$booking->number_of_tourists}}</strong></h5>
24                                    <h5>Tour date: <br/><strong>{{$booking->visit_date->toDateString()}}</strong></h5>
25                                    <hr />
26                                    <h5><strong>User's name:</strong> <br/>{{$booking->user->name}}</h5>
27                                    <h5><strong>Contact information:</strong><br/>
28                                        Phone: {{$booking->user->phone}}<br/>
29                                        Email: {{$booking->user->email}}<br/>
30                                        Country: {{$booking->user->country}}
31                                    </h5>
32                                </div>
33                            </div>
34                        </div>
35                            @if(!$loop->last)
36                        <hr/>
37                            @endif
38                            @endforeach
39                        @endif
40                    </div>
41                </div>
42            </div>
43        </div>
44    </div>
45    @endsection

How our application looks now

Let us see what the different versions look like. Run the following command to start the page:

    $ php artisan serve

Visit 127.0.0.1:8000 to view our international application.

To see the pages in different language versions, change the language settings on your browser language settings. If you are on a chrome browser, read about how to change your language here.

French homepage

Notice that when you set the browser language to French, the text on the homepage shows in French. Then when you have it in English, the text on the page is in English.

German destination page
French single destination page
English single destination page
Tour booking page
Admin dashboard

Conclusion

We have looked at what it takes to build an international application. We built an international application with support for multiple languages. We did string translations for French and German. We saw how to style different sections of our page based on language using the CSS lang() selector.

At the end of the day, we have a simple international application. There is a lot that can be added to it like dynamically generated links for alternative language versions. I hope this guide helps you fully understand what to look out for when building an international application. Checkout out these quick tips from W3 on making international applications.

The source code to the application in this article is available on GitHub.