Package Data | |
---|---|
Maintainer Username: | petercoles |
Maintainer Contact: | peterdcoles@gmail.com (Peter Coles) |
Package Create Date: | 2016-04-30 |
Package Last Update: | 2024-03-29 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-01-06 03:18:04 |
Package Statistics | |
---|---|
Total Downloads: | 238,511 |
Monthly Downloads: | 2,907 |
Daily Downloads: | 38 |
Total Stars: | 38 |
Total Watchers: | 5 |
Total Forks: | 9 |
Total Open Issues: | 0 |
Over the years, many of the projects I've worked on have resulted in multilingual sites. During that time the number of languages typically supported has increased and the sensitivty to the importance of dialectical differences has improved, which are good things.
The purpose of this package is to make managing language lists, such as those used in language pulldowns or form select fields easier to generate via a simple API that gives access to an industry-maintained list.
Data can be returned as a lookup array or an array of key-value pairs, where both the key and value labels can be set according to the needs of the software consuming them.
At the command line run
composer require petercoles/multilingual-language-list
If you're using Laravel 5.5 or later (and haven't disabled package discovery), you're done. Move on to the usage section below.
If you're using an older version of Laravel, then add the service provider to the providers entry in your config/app.php file
'providers' => [
// ...
PeterColes\Languages\LanguagesServiceProvider::class,
// ...
],
An optional facade is also available and can be enabled by adding the following to you config/app.php's aliases array
'Languages' => PeterColes\Languages\LanguagesFacade::class,
Once installed the package exposes two API methods: lookup() and keyValue(), each of which returns a list of countries ordered by the country name in the language being used.
The lookup
method takes three optional parameters and returns a collection.
The resulting collection will be cast to a json object by Laravel if returned as a response, or can be cast to an array if needed with the toArray() method.
Languages::lookup();
// returns
{
"ab": "Abkhazian",
...
"zu": "Zulu"
}
Languages::lookup(['en', 'fr', 'de']);
// returns
{
"en": "English",
"fr": "French",
"de": "German"
}
Languages::lookup(['en', 'fr', 'de'], 'fr');
// returns
{
"de": "allemand",
"en": "anglais",
"fr": "français"
}
Languages::lookup(['en', 'fr', 'de'], 'fr', true);
// returns
{
"allemand": "de",
"anglais": "en",
"français": "fr"
}
Languages::lookup(['en', 'fr', 'de', 'bs'], 'bs_Cyrl');
// returns
{
"bs": "босански",
"en": "енглески",
"de": "немачки",
"fr": "француски"
}
The keyValue
method takes four optional parameters:
Languages::keyValue();
// returns
[
{"key": "ab", "value": "Abkhazian"},
{"key": "aa", "value": "Afar"},
...
{"key": "za", "value": "Zhuang"},
{"key": "zu", "value": "Zulu"}
]
Languages::keyValue('minor');
// returns
[
{"key": "ab", "value": "Abkhazian"},
{"key": "ace", "value": "Achinese"},
{"key": "ach", "value": "Acoli"},
...
{"key": "gbz", "value": "Zoroastrian Dari"},
{"key": "zu", "value": "Zulu"},
{"key": "zun", "value": "Zuni"}
]
Languages::keyValue(['en', 'ja', 'zh'], 'zh', 'label', 'text');
// returns
[
{"label": "ja", "text": "日文"},
{"label": "en", "text": "英文"},
{"label": "zh", "text": "中文"}
]
Sometimes you might want to display a list of languages where each language is expressed in its own language and writing system e.g. one list with French as français, Japanese as 日本語 and Russian as русский. If so, we've got you covered.
By using the special "mixed" locale as the second parameter and a custom array as the first, the languages in that custom array will each be rendered in their own localised form, in the order given in the first parameter.
Languages::lookup(['en', 'fr', 'de', 'ja', 'ru', 'zh'], 'mixed');
// returns
{
"en": "English",
"fr": "français",
"de": "Deutsch",
"ja": "日本語",
"ru": "русский",
"zh": "中文",
}
Languages::keyValue(['en', 'fr', 'de', 'ja', 'ru', 'zh'], 'mixed');
// returns
[
{"key" => "en", "value" => "English"},
{"key" => "fr", "value" => "français"},
{"key" => "de", "value" => "Deutsch"},
{"key" => "ja", "value" => "日本語"},
{"key" => "ru", "value" => "русский"},
{"key" => "zh", "value" => "中文"}
]
As seen above, the mixed locale parameter can be used for generating lookups or key-value objects. The $flip, $key and $value parameters continue to work for the relevant list type in the same way as shown in the earlier sections.
This package was developed to meet a specific need and then generalised for wider use. If you have a use case not currently met, or see something that appears to not be working correctly, please raise an issue at the github repo.
This package is licensed under the MIT license.