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
);

 $this->dataParser->parseAllDataPerCountry('pl_PL');

Illuminate\Support\Collection {▼
  #items: [
    0 => {#1992 ▼
      +"so": "PL"
      +"iso3": "POL"
      +"iso_nr": "616"
      +"fips": "PL"
      +"continent": "EU"
      +"tld": ".pl"
      +"phone": "48"
      +"postal_code_format": "##-###"
      +"postal_code_regex": "^\d{2}-\d{3}$"
      +"currency": {#1991 ▼
        +"name": "złoty polski"
        +"code": "PLN"
        +"symbol": "zł"
      }
      +"languages": array:1 [▼
        "pl" => "polski"
      ]
      +"country": "Polska"
      +"subdivision_iso3166": Illuminate\Support\Collection {#2275 ▼
        #items: array:16 [▼
          "PL-02" => array:2 [▼
            "name" => "Dolnośląskie"
            "name_ascii" => "Dolnoslaskie"
          ]
          "PL-04" => array:2 [▼
            "name" => "Kujawsko-pomorskie"
            "name_ascii" => "Kujawsko-pomorskie"
          ]
          "PL-06" => array:2 [▼
            "name" => "Lubelskie"
            "name_ascii" => "Lubelskie"
          ]
          "PL-08" => array:2 [▼
            "name" => "Lubuskie"
            "name_ascii" => "Lubuskie"
          ]
          "PL-10" => array:2 [▼
            "name" => "Łódzkie"
            "name_ascii" => "Lodzkie"
          ]
          "PL-12" => array:2 [▼
            "name" => "Małopolskie"
            "name_ascii" => "Malopolskie"
          ]
          "PL-14" => array:2 [▼
            "name" => "Mazowieckie"
            "name_ascii" => "Mazowieckie"
          ]
          "PL-16" => array:2 [▼
            "name" => "Opolskie"
            "name_ascii" => "Opolskie"
          ]
          "PL-18" => array:2 [▼
            "name" => "Podkarpackie"
            "name_ascii" => "Podkarpackie"
          ]
          "PL-20" => array:2 [▼
            "name" => "Podlaskie"
            "name_ascii" => "Podlaskie"
          ]
          "PL-22" => array:2 [▼
            "name" => "Pomorskie"
            "name_ascii" => "Pomorskie"
          ]
          "PL-24" => array:2 [▼
            "name" => "Śląskie"
            "name_ascii" => "Slaskie"
          ]
          "PL-26" => array:2 [▶]
          "PL-28" => array:2 [▼
            "name" => "Warmińsko-mazurskie"
            "name_ascii" => "Warminsko-mazurskie"
          ]
          "PL-30" => array:2 [▼
            "name" => "Wielkopolskie"
            "name_ascii" => "Wielkopolskie"
          ]
          "PL-32" => array:2 [▼
            "name" => "Zachodniopomorskie"
            "name_ascii" => "Zachodniopomorskie"
          ]
        ]
      }
    }

    ...
}


 $this->dataParser->parseAllDataPerCountry('pl_PL', 'PL');

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',
];

protected $routeMiddleware = [
    // Other middleware...
    'language' => \Dominservice\DataLocaleParser\Http\Middleware\LanguageMiddleware::class,
];

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');
});

<ul class="language-switcher">
    @foreach(config('data_locale_parser.allowed_locales') as $locale)
        <li>
            <a href="{{ route('language.change', ['language' => $locale]) }}">
                {{ strtoupper($locale) }}
            </a>
        </li>
    @endforeach
</ul>

// routes/web.php

// English routes
Route::prefix('en')->name('en.')->middleware(['language'])->group(function () {
    Route::get('/', 'HomeController@index')->name('home');
    Route::get('/contact', 'ContactController@index')->name('contact');
    Route::get('/about', 'AboutController@index')->name('about');
});

// Polish routes
Route::prefix('pl')->name('pl.')->middleware(['language'])->group(function () {
    Route::get('/', 'HomeController@index')->name('home');
    Route::get('/kontakt', 'ContactController@index')->name('contact');
    Route::get('/o-nas', 'AboutController@index')->name('about');
});

// German routes
Route::prefix('de')->name('de.')->middleware(['language'])->group(function () {
    Route::get('/', 'HomeController@index')->name('home');
    Route::get('/kontakt', 'ContactController@index')->name('contact');
    Route::get('/uber-uns', 'AboutController@index')->name('about');
});

locale_is_rtl(?string $locale = null): bool

if (locale_is_rtl('ar')) {
    // Arabic is an RTL language
}

if (locale_is_rtl()) {
    // The current locale is an RTL language
}

'locale_rtl' => [
    'ar',    // Arabic
    'fa',    // Persian (Farsi)
    'he',    // Hebrew
    'ur',    // Urdu
    'yi',    // Yiddish
    // ... other RTL languages
],

get_translated_route(string $locale, string $key): string

$translatedKey = get_translated_route('pl', 'contact');
// Returns 'kontakt' if a translation exists, otherwise 'pl.contact'

route_current_locale(string $route, mixed $parameters = [], bool $absolute = true): string

$url = route_current_locale('contact');
// If the current locale is 'en', returns URL for 'en.contact'

route_locale(string $locale, string $route, mixed $parameters = [], bool $absolute = true): string

$url = route_locale('pl', 'contact');
// Returns URL for 'pl.contact'

is_route_current_locale(mixed ...$patterns): bool

if (is_route_current_locale('contact', 'about')) {
    // Current route is either 'en.contact' or 'en.about' (assuming current locale is 'en')
}

get_localized_url(string $locale): string

$plUrl = get_localized_url('pl');
// If current URL is '/en/contact', returns '/pl/kontakt' (if route names are properly set up)
bash
php artisan vendor:publish --provider="Dominservice\DataLocaleParser\DataLocaleParserServiceProvider" --tag="config"