Skip to content

Commit c27cb0d

Browse files
sarotnemMarc Cámara
authored andcommitted
Added support for custom order (#406)
* Added support for custom order * Update Readme for custom order helper function
1 parent 8e01575 commit c27cb0d

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,23 @@ It returns a route, localized to the desired locale using the locale passed. If
254254

255255
This function will return all supported locales and their properties as an array.
256256

257+
### Get Supported Locales Custom Order
258+
259+
```php
260+
/**
261+
* Return an array of all supported Locales but in the order the user
262+
* has specified in the config file. Useful for the language selector.
263+
*
264+
* @return array
265+
*/
266+
public function getLocalesOrder()
267+
268+
//Should be called like this:
269+
{{ LaravelLocalization::getLocalesOrder() }}
270+
```
271+
272+
This function will return all supported locales but in the order specified in the configuration file. You can use this function to print locales in the language selector.
273+
257274
### Get Supported Locales Keys
258275

259276
```php

src/Mcamara/LaravelLocalization/LaravelLocalization.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,27 @@ public function getSupportedLocales()
385385
return $locales;
386386
}
387387

388+
/**
389+
* Return an array of all supported Locales but in the order the user
390+
* has specified in the config file. Useful for the language selector.
391+
*
392+
* @return array
393+
*/
394+
public function getLocalesOrder()
395+
{
396+
$locales = $this->getSupportedLocales();
397+
398+
$order = $this->configRepository->get('laravellocalization.localesOrder');
399+
400+
uksort($locales, function ($a, $b) use ($order) {
401+
$pos_a = array_search($a, $order);
402+
$pos_b = array_search($b, $order);
403+
return $pos_a - $pos_b;
404+
});
405+
406+
return $locales;
407+
}
408+
388409
/**
389410
* Returns current locale name.
390411
*
@@ -423,9 +444,9 @@ public function getCurrentLocaleDirection()
423444
case 'Mong':
424445
case 'Tfng':
425446
case 'Thaa':
426-
return 'rtl';
447+
return 'rtl';
427448
default:
428-
return 'ltr';
449+
return 'ltr';
429450
}
430451
}
431452

src/config/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,9 @@
309309
//
310310
'hideDefaultLocaleInURL' => false,
311311

312+
// If you want to display the locales in particular order in the language selector you should write the order here.
313+
//CAUTION: Please consider using the appropriate locale code otherwise it will not work
314+
//Example: 'localesOrder' => ['es','en'],
315+
'localesOrder' => [],
316+
312317
];

0 commit comments

Comments
 (0)