Localization the easy way in Laravel (v5.*)

- https://github.com/jenssegers/date
- https://github.com/mcamara/laravel-localization

For Dates

1- install the lang package on the server

$ sudo apt-get install language-pack-xx // xx = ar or any lang you want

2- change ur old Carbon to Date

// if we used "->toFormattedDateString()" we get a short name for the month which will also be trimmed when localized
// so instead use "->format('F d, Y')"
return Date::parse($value)->format('F d, Y');

For Pages

1- routes.php

  'prefix'     => LaravelLocalization::setLocale(),
  'middleware' => ['localeSessionRedirect', 'localizationRedirect'],
  ], function () {

    // other routes


// anything else that should not be localized

2- for RTL locals

* in case you are using a language like 
* arabic , hebrew, indian, etc..
* as the default trim that come with laravel doesnt support those encodings
public function trim($value, $limit = 100, $end = '...')
  $value = strip_tags($value);

  if (App::getLocale() != 'en') {
    return strlen($value) <= $limit
      ? $value
      : mb_substr($value, 0, $limit, 'UTF-8').$end;

  return mb_strwidth($value, 'UTF-8') <= $limit
    ? $value
    : rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;

For Slug

// this use arabic ,so change the regex according to ur language
function slug($string, $separator='-') {
  $url = trim($string);
  $url = strtolower($url);
  $url = preg_replace('/\s+/', ' ', $url);
  $url = preg_replace( '|[^a-z-A-Z\p{Arabic}0-9 _]|iu', '', $url );
  $url = str_replace(' ', $separator, $url);
  return $url;


# v5.5 update

now you can use “but i haven’t tested it yet for RTL languages.”

str_slug($string, $separator, $language)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.