AmberDB
view release on metacpan or search on metacpan
docs/EN.AmberDB-Locale_User-Guide.md view on Meta::CPAN
`AmberDB::Locale` is a **locale-aware text processing engine** designed for multilingual applications. Written in Perl, it provides the following core capabilities:
| Capability | Description |
|---|---|
| Case conversion | Locale-aware `uc`, `lc`, `ucfirst` |
| Sorting | Unicode Collation Algorithm (UCA) based |
| ASCII transliteration | Convert accented characters to plain ASCII (slugs, IDs) |
| Number â Text | Written-out numbers for invoices/documents |
| Date/time formatting | Locale-specific date formats |
| Number/currency formatting | Grouping separators, decimal separators, symbol placement |
| HTML entity decoding | Named + numeric entity decode |
| Plural rules | CLDR-based plural form selection |
| UTF-8 safe substring | Character-based slicing (not byte-based) |
**Architectural principle:** Engine logic lives in `AmberDB::Locale.pm`, while language data resides in `AmberDB::Locale::Lang::*` packages as **pure data**. Engine and data are completely separated.
---
## 2. Architecture
```
AmberDB::Locale â Main engine (all logic here)
âââ AmberDB::Locale::Lang::gb â Global Base data (default/fallback)
âââ AmberDB::Locale::Lang::en â English data
âââ AmberDB::Locale::Lang::tr â Turkish data
âââ AmberDB::Locale::Lang::de â German data
âââ AmberDB::Locale::Lang::fr â French data
âââ AmberDB::Locale::Lang::es â Spanish data
âââ AmberDB::Locale::Lang::ru â Russian data
âââ AmberDB::Locale::Lang::az â Azerbaijani data
âââ AmberDB::Locale::Lang::ar â Arabic data
âââ AmberDB::Locale::Lang::ja â Japanese data
âââ AmberDB::Locale::Currency â ISO 4217 universal currency data
```
Each `Lang::*` module contains only a single subroutine called `data()` and returns a hash-ref. **No logic is included.**
---
## 3. Object Construction (Constructor)
```perl
use AmberDB::Locale;
# 1) Named-param API (recommended)
my $lang = AmberDB::Locale->new(language => "tr");
# 2) Hashref API
my $lang = AmberDB::Locale->new({ language => "de" });
# 3) Positional string API
my $lang = AmberDB::Locale->new("fr");
# 4) No args â defaults to "gb" (Global Base)
my $lang = AmberDB::Locale->new();
```
### Internal behaviors
- **Caching:** A second `new()` call for the same language returns the existing instance (singleton-like).
- **Alias support:** Labels like `"global"`, `"gl"`, `"turkish"`, `"tr_tr"`, `"tr-tr"` are automatically mapped to canonical codes.
- **Fallback:** If an unknown language is requested, a `cluck` warning is emitted and it falls back to `gb`.
- **Collator:** Attempts to load `Unicode::Collate::Locale`; if it fails, falls back to base `Unicode::Collate`, and if that's also unavailable, uses a custom `sort_map` Schwartzian transform.
### AmberDB integration
```perl
# Language is automatically pulled from cfg
my $db = AmberDB->new(cfg => { language => "tr" });
$db->uc("ıÄdır"); # works via inheritance: IÄDIR
```
---
## 4. Language Data Structure (`data()` Hash-Ref)
Complete schema of the structure returned by each language module:
```perl
{
uc_map => { }, # Character mapping applied before uc()
lc_map => { }, # Character mapping applied before lc()
sort_map => { }, # Custom sort weights (if no collator)
alphabet_chars => "...", # Safe character class for normalize()
accent_map => { }, # normalize() accent â local equivalent
ascii_map => { }, # to_ascii() pre-mapping before NFD
numbers => { }, # num2text number words
html_entities => { }, # Locale-specific extra HTML entities
months => [ ], # 12-element month names
days => [ ], # 7-element day names (Sunday â Saturday)
number_format => { }, # Decimal/grouping separator settings
default_currency => "",
currency_position => "prefix|suffix",
currency_space => 0|1,
date_format => { }, # short, medium, long, full, time, datetime
plural_rule => "...", # CLDR plural rule expression
}
```
---
## 5. Public API - Method Reference
### 5.1 Case Conversions
#### `uc($string)` - Convert to uppercase
```perl
my $tr = AmberDB::Locale->new(language => "tr");
$tr->uc("ıÄdır"); # "IÄDIR"
$tr->uc("istanbul"); # "İSTANBUL" (i â İ, Turkish rule)
my $de = AmberDB::Locale->new(language => "de");
$de->uc("straÃe"); # "STRASSE" (Ã â SS)
```
> **Turkish detail:** The mapping `'i' => "\x{130}"` inside `uc_map` is applied **before** Perl's `CORE::uc()` call. This ensures the `i â İ` conversion is done correctly.
#### `lc($string)` - Convert to lowercase
```perl
( run in 0.497 second using v1.01-cache-2.11-cpan-4716db65a44 )