Download the PHP package dominservice/data_locale_parser without Composer
On this page you can find all versions of the php package dominservice/data_locale_parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dominservice/data_locale_parser
More information about dominservice/data_locale_parser
Files in dominservice/data_locale_parser
Package data_locale_parser
Short Description Data Locale Parser is a package for Laravel 5.6 | 5.7 | 5.8 | 6.* | 7.* | 8.* | 9.* | 10.*, | 11.*, | 12.*, which lists all countries, currencies and languages, with names and ISO 3166 codes in all languages and data formats.
License MIT
Homepage https://github.com/dominservice/data_locale_parser
Informations about the package data_locale_parser
Data Locale Parser
Data Locale Parser is a package for Laravel 5.6 | 5.7 | 5.8 | 6. | 7. | 8. | 9.| 10.| 11., which lists all countries, currencies and languages, with names and ISO 3166-1 codes in all languages and data formats.
Installation
Require package via Composer: composer require dominservice/data_locale_parser
Usage
- Locale (en, en_US, fr, fr_CA...)
- If no locale is given (or if it is set to null), then it will default to 'en'
Declare ussage
(...)
(...)
Get all countries
Get all currencies
Get all Languages
All lists return a collection
Get country
Get currency
Get Language
Format an address based on country-specific format
Address Format Keys
The following keys can be used in the address data array for the formatAddress
function:
Key | Description | Example |
---|---|---|
address |
Primary address line (street address) | 1234 Some St. |
address2 |
Secondary address line | Floor #67 |
address3 |
Tertiary address line | Unit #123 |
city |
City name | San Francisco |
subdivision |
State, province, or region | CA |
postalCode |
Postal or ZIP code | 94105 |
countryCode |
ISO 3166-1 Alpha-2 country code (required) | US |
The function will format the address according to the country-specific format based on the countryCode
. If a specific country format is not available, it will use the international format.
Additional Address Parameters
The formatAddress
function accepts several additional parameters beyond the basic address data:
Parameter | Type | Description | Example |
---|---|---|---|
name |
string | Person's name to include at the beginning of the address | John Doe |
companyName |
string | Company name to include in the address | Example Company Ltd. |
vatNumber |
string | VAT identification number | GB123456789 |
phoneNumber |
string | Contact phone number | +1 (123) 456-7890 |
additionalFields |
array | Array of additional text lines to include | ['Customer ID: 12345'] |
These additional parameters will be included in the formatted address in the following order:
- Name (if provided)
- Company name (if provided)
- VAT number (if provided, prefixed with "VAT: ")
- Standard address lines (formatted according to country format)
- Phone number (if provided)
- Any additional fields (in the order they appear in the array)
If you have collected all data, you make use this code
Then You get
You may get one country full data
Language Handling Middleware
This package includes a middleware for handling language detection from URL paths and request headers. This allows you to create routes with language prefixes (e.g., /en/page
, /pl/page
) and automatically set the application locale based on the URL or header.
Configuration
Publish the configuration file:
This will create a config/data_locale_parser.php
file with the following options:
Usage
Register the middleware in your app/Http/Kernel.php
file:
Cookie Encryption
By default, Laravel encrypts all cookies. If you're using cookies for language storage (use_cookies
set to true
), you need to exclude the language cookie from encryption. Otherwise, the middleware might not be able to read the cookie correctly.
Add the language cookie name to the $except
array in your app/Http/Middleware/EncryptCookies.php
file:
If you've changed the cookie name in your configuration, make sure to use that name instead of 'language'.
Cookie Handling
This package sets cookies directly on the response object to ensure they are properly included in the response. The package automatically applies the web
middleware group to its routes to ensure that cookies work correctly.
Apply the middleware to your routes:
How It Works
-
URL Detection: The middleware checks if the first segment of the URL path is a valid language code. For API routes (URLs starting with a prefix defined in
api_prefixes
), it checks the second segment. -
Header Detection: If URL detection is disabled or fails, the middleware checks the request header specified in the configuration.
-
Default Locale: If no language is detected from the URL or header, the middleware uses the default locale specified in the configuration.
- Cookie Storage: If
use_cookies
is enabled, the language preference will be stored in a cookie. This allows the language to persist across requests without showing it in the URL. For non-API routes, the middleware will check for the language in the cookie before checking the URL.
Changing the Language
The package automatically registers a route for changing the language. By default, this route is /change-language/{language}
, but you can customize it in the configuration.
To create a language switcher in your application, you can use the following example:
When a user clicks on a language link, the following happens:
- The language is validated against the allowed locales and the language database.
- If valid, the language is set as the application locale.
- If
use_cookies
is enabled, the language preference is stored in a cookie. - The user is redirected back to the previous page using one of the following methods:
- Route Name-Based Redirection: If the previous page has a named route with a locale prefix (e.g.,
en.contact
), the controller will replace the locale in the route name and redirect to the new route with the same parameters. This allows for path translation (e.g.,/kontakt
to/contact
) when your routes are properly localized. - URL Segment Manipulation: If route name-based redirection fails or the route doesn't have a name, the controller falls back to URL segment manipulation:
- If
use_cookies
is enabled, the language prefix is removed from the URL. - If
use_cookies
is disabled, the language prefix is updated or added to the URL.
- If
- Route Name-Based Redirection: If the previous page has a named route with a locale prefix (e.g.,
This allows you to create a seamless language switching experience for your users, with or without showing the language in the URL, and with support for path translation when using named routes.
Setting Up Localized Routes
To take advantage of the route name-based redirection and path translation, you need to set up your routes with locale prefixes in their names. Here's an example:
With this setup, when a user switches from Polish to English while on the /pl/kontakt
page, they will be redirected to /en/contact
instead of just changing the language prefix.
Helper Functions
This package includes several helper functions to make working with localized routes and URLs easier. These functions are automatically loaded when you install the package.
RTL Detection
Checks if a locale is RTL (Right-to-Left). If no locale is provided, it uses the current application locale.
You can configure which locales are considered RTL in the config/data_locale_parser.php
file:
Route Translation
Gets a translated route key for a specific locale. This is useful for translating route names or segments.
Localized Routes
Generates a URL to a named route for the current locale.
Generates a URL to a named route for a specific locale.
Route Checking
Checks if the current route name matches any of the given patterns for the current locale.
URL Localization
Gets the URL for the current page in a different locale. This function tries to use route name-based redirection first, and falls back to URL segment manipulation if that fails.
Examples
For more advanced usage examples, check out the examples directory. It contains sample code demonstrating various features of the library, including:
- Enhanced language full data methods with caching, custom display locales, and advanced filtering
- Single language data retrieval
- Language handling middleware usage
- And more
Credits
All versions of data_locale_parser with dependencies
ext-intl Version *
ext-mbstring Version *
laravel/framework Version ^5.6|^5.7|^5.8|^6|^7|^8|^9|^10|^11|^12
arcreative/php-address-format Version ^0.0.2