https://github.com/tonglil/localize
A library to localize location-based attributes based on regular expressions.
https://github.com/tonglil/localize
formatter localization php
Last synced: 8 months ago
JSON representation
A library to localize location-based attributes based on regular expressions.
- Host: GitHub
- URL: https://github.com/tonglil/localize
- Owner: tonglil
- License: mit
- Created: 2014-10-13T03:09:01.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-12-23T19:59:53.000Z (over 11 years ago)
- Last Synced: 2025-02-12T07:49:45.147Z (over 1 year ago)
- Topics: formatter, localization, php
- Language: PHP
- Homepage: http://tonglil.github.io/Localize
- Size: 875 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Localize
[](https://travis-ci.org/tonglil/Localize)
[](https://coveralls.io/r/tonglil/Localize)
[](https://scrutinizer-ci.com/g/tonglil/Localize/?branch=master)
[](https://packagist.org/packages/tonglil/Localize)
[](https://packagist.org/packages/tonglil/Localize)
[](https://packagist.org/packages/tonglil/Localize)
[](https://packagist.org/packages/tonglil/Localize)
[](https://insight.sensiolabs.com/projects/3ab73b7b-5159-4bfe-8c85-2a15d03f9579)
A library to localize location-based attributes and coerce values into desired formats based on regular expressions.
Note: *this is not a translation or i18n library.*
More locales to come - please feel free to submit a PR if you would like to help fill the missing holes!
## Contents
- [Installation](#install)
- [Locales](#locales)
- [Formats](#formats)
- [Examples](#examples)
- [API documentation](http://tonglil.github.io/Localize/).
## Install
The recommended way to install is through [Composer](http://getcomposer.org).
Update your project's composer.json file to include Localize:
```json
{
"require": {
"tonglil/localize": "1.*"
}
}
```
Then update the project dependencies to include this library:
```bash
composer update tonglil/localize
```
After installing, you need to require Composer's autoloader:
```php
require 'vendor/autoload.php';
```
## Locales
Country codes are based on [ISO 3166-1](http://en.wikipedia.org/wiki/ISO_3166-1).
Phone numbers can be formatted to [E.164 formatting](http://en.wikipedia.org/wiki/E.164)
Locales currently supported:
- CA
Planned locales:
- US
- FR
- GB
- AU
- CN
Locales are stored in [src/locales](src/locales) directory.
## Formats
The default formats currently supported:
- Region (province/state)
- Post code (postal/zip code)
- Country name
- Phone number (regional "de-facto" formatting or E.164 formatting)
## Examples
```php
use Localize\Localize;
// Create a new localize instance.
$localize = new Localize();
// Set the locale using a two digit ISO country code.
$localize->setLocale('CA');
$address = [
'address' => '525 Seymour Street',
'city' => 'Vancouver',
'region' => $localize->region('british columbia', true),
'postal_code' => $localize->postalCode('v6b3h7'),
'country' => $localize->country('CANADA', false),
'phone' => $localize->phone('5555555555'),
];
echo $address['region']; // BC
echo $address['postal_code']; // V6B 3H7
echo $address['country']; // Canada
echo $address['phone']; // 555-555-5555
// Region and country both accept a second parameter that formats the value to
// its short version when true, otherwise uses the long version by default.
echo $localize->region('ontario', true); // ON
echo $localize->region('ontario', false); // Ontario
// Postal code and phone number will attempt to massage a limit amount of
// formatting into the standard output.
echo $localize->phone('555 555-5555'); // 555-555-5555 regional "de-facto" formatting
echo $localize->phoneE164('+1 555 555-5555'); // 011-1-555-555-5555 full E.164 formatting
echo $localize->phoneE164('+1 555 555-5555', false); // +1-555-555-5555 common E.164 formatting
echo $localize->postalCode('V6b 3h7'); // V6B 3H7
// Basic validation is performed; if a match is not found and can not be
// massaged to a format, null is returned.
var_dump($localize->phone('abc-def-gehi')); // null
```