PHP code example of dominservice / data_locale_parser
1. Go to this page and download the library: Download dominservice/data_locale_parser library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dominservice / data_locale_parser example snippets
use \Dominservice\DataLocaleParser\DataParser;
private $dataParser;
public function __construct() {
$this->dataParser = new DataParser();
}
$this->dataParser->getListCountries('en');
$this->dataParser->getListCurrencies('en');
$this->dataParser->getListLanguages('en');
$this->dataParser->getCountry('PL', 'en');
$this->dataParser->getCurrency('PLN', 'en');
$this->dataParser->geLanguage('pl_PL', 'en');
// Address data with keys like address, address2, city, subdivision, postalCode, countryCode
$addressData = [
'address' => '1234 Some St.',
'address2' => 'Floor #67',
'city' => 'San Francisco',
'subdivision' => 'CA',
'postalCode' => '94105',
'countryCode' => 'US'
];
// Basic address formatting (returns an array of address lines)
$formattedAddress = $this->dataParser->formatAddress($addressData);
// Format address with additional parameters
$formattedAddressComplete = $this->dataParser->formatAddress(
$addressData, // Address data
'John Doe', // Name
'Example Company Ltd.', // Company name
'GB123456789', // VAT number
'+1 (123) 456-7890', // Phone number
['Customer ID: 12345'] // Additional fields
);
// Format address with just some parameters (others will be null)
$formattedAddressPartial = $this->dataParser->formatAddress(
$addressData, // Address data
null, // No name
'Example Company Ltd.', // Company name
null, // No VAT number
'+1 (123) 456-7890' // Phone number
);
return [
// Whether to detect language from URL
'detect_from_url' => true,
// Whether to use cookies for language storage
// If true, the language preference will be stored in a cookie
// This allows the language to persist across requests without showing it in the URL
'use_cookies' => false,
// Whether to detect language from header
'detect_from_header' => true,
// The name of the header to check for language
'header_name' => 'Accept-Language',
// Default locale if no language is detected
'default_locale' => 'en',
// Allowed locales
'allowed_locales' => [
'en',
'pl',
'de',
'fr',
'es',
'en_GB',
'en_US',
],
// API prefixes
'api_prefixes' => [
'api',
],
// Cookie settings
'cookie_name' => 'language',
'cookie_lifetime' => 43200, // 30 days
// Language change route
// This route will be registered automatically by the service provider
'language_change_route' => 'change-language',
];
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array<int, string>
*/
protected $except = [
'language', // Add the language cookie name here (use the value from your config)
];
}
// Apply to specific routes
Route::get('/{any}', 'HomeController@index')->middleware('language')->where('any', '.*');
// Apply to route groups
Route::middleware(['language'])->group(function () {
Route::get('/', 'HomeController@index');
Route::get('/{any}', 'HomeController@index')->where('any', '.*');
});
// Apply to API routes
Route::prefix('api')->middleware(['language'])->group(function () {
Route::get('/{lang}/users', 'Api\UserController@index');
});