| 1 | package Mojolicious::Plugin::Fondation::I18N; | ||||||||||||||||||||||||||||||||
| 2 | 2 | $Mojolicious::Plugin::Fondation::I18N::VERSION = '0.02'; | |||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Fondation I18N plugin -- JSON-backed localization for the Fondation ecosystem | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin', -signatures; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::JSON qw(encode_json); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use I18N::LangTags; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use I18N::LangTags::Detect; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use POSIX qw(strftime setlocale LC_TIME); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | # myapp.conf | ||||||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||||||
| 17 | Fondation => { | ||||||||||||||||||||||||||||||||
| 18 | dependencies => ['Fondation::I18N'], | ||||||||||||||||||||||||||||||||
| 19 | }, | ||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | # In templates | ||||||||||||||||||||||||||||||||
| 23 | %= l('Welcome') | ||||||||||||||||||||||||||||||||
| 24 | <a href="/"><%= l 'Home' %></a> | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # In JavaScript (after %= i18n_js in layout) | ||||||||||||||||||||||||||||||||
| 27 | successBox(l("User saved successfully")); | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | This plugin provides real dictionary-based translation for Fondation | ||||||||||||||||||||||||||||||||
| 32 | applications, overriding the identity fallback helpers (C<l> and C<i18n_js>) | ||||||||||||||||||||||||||||||||
| 33 | from Fondation core. | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | =head2 How it works | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | A post-load action (declared via C<provides_actions =E<gt> ['I18N']>) scans | ||||||||||||||||||||||||||||||||
| 38 | every plugin's C<share/translations/E<lt>langE<gt>.json> at startup and merges | ||||||||||||||||||||||||||||||||
| 39 | them into a single lexicon per language. The merged lexicons are stored on | ||||||||||||||||||||||||||||||||
| 40 | C<$app> and exposed via the C<i18n_lexicons> helper. | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | At request time, a C<before_dispatch> hook detects the user's language | ||||||||||||||||||||||||||||||||
| 43 | (from URL prefix or C<Accept-Language> header) and caches the appropriate | ||||||||||||||||||||||||||||||||
| 44 | lexicon reference in C<$c-E<gt>stash('i18n_lexicon')>. The C<l()> helper | ||||||||||||||||||||||||||||||||
| 45 | then performs a single hash lookup -- no chasing. | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | =head2 Client-side JavaScript | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | The C<i18n_js> helper injects a C<E<lt>scriptE<gt>> tag with the current | ||||||||||||||||||||||||||||||||
| 50 | language's translations as a JSON object. Application JavaScript calls | ||||||||||||||||||||||||||||||||
| 51 | C<window.l('key')> for zero-overhead lookups. | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | An C</i18n/E<lt>langE<gt>.json> endpoint is also registered for dynamic | ||||||||||||||||||||||||||||||||
| 54 | lookups. | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||||||
| 59 | Fondation => { | ||||||||||||||||||||||||||||||||
| 60 | dependencies => [ | ||||||||||||||||||||||||||||||||
| 61 | { 'Fondation::I18N' => { | ||||||||||||||||||||||||||||||||
| 62 | default => 'en', | ||||||||||||||||||||||||||||||||
| 63 | support_url_langs => ['fr', 'en'], | ||||||||||||||||||||||||||||||||
| 64 | }}, | ||||||||||||||||||||||||||||||||
| 65 | ], | ||||||||||||||||||||||||||||||||
| 66 | }, | ||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | =head2 Options | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | =over | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =item C<default> | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | Fallback language when detection fails (default: C<en>). | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | =item C<support_url_langs> | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | Language codes that may appear as the first URL path segment | ||||||||||||||||||||||||||||||||
| 80 | (e.g. C</fr/users>). These are detected in C<before_dispatch>. | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | =back | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | =head1 TRANSLATION FILES | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | Plugins ship translations in C<share/translations/E<lt>langE<gt>.json>: | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | share/translations/en.json {"Home": "Home", "Save": "Save"} | ||||||||||||||||||||||||||||||||
| 89 | share/translations/fr.json {"Home": "Accueil", "Save": "Enregistrer"} | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | Keys are the English strings passed to C<l()>. Files from all plugins are | ||||||||||||||||||||||||||||||||
| 92 | merged at startup by the I18N action; last write wins for duplicate keys. | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | =over | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | =item L<Mojolicious::Plugin::Fondation> | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | Core framework providing the identity fallback helpers that this plugin overrides. | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | =item L<Mojolicious::Plugin::Fondation::I18N::Action::I18N> | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | Post-load action that scans and merges translation files at startup. | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | =back | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 111 | # fondation_meta -- declares itself to Fondation core | ||||||||||||||||||||||||||||||||
| 112 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 2 | ▶ | sub fondation_meta { | ||||||||||||||||||||||||||||||
|
Subroutine
fondation_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 115 | return { | ||||||||||||||||||||||||||||||||
| 116 | 2 | dependencies => [], | |||||||||||||||||||||||||||||||
| 117 | provides_actions => ['I18N'], | ||||||||||||||||||||||||||||||||
| 118 | defaults => { | ||||||||||||||||||||||||||||||||
| 119 | default => 'en', | ||||||||||||||||||||||||||||||||
| 120 | support_url_langs => [], | ||||||||||||||||||||||||||||||||
| 121 | fondation_clean => ['share/i18n/'], | ||||||||||||||||||||||||||||||||
| 122 | }, | ||||||||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 127 | # register -- set up hooks, helpers, and routes | ||||||||||||||||||||||||||||||||
| 128 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | sub register ($self, $app, $config) { | ||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=2 SCAR=9.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | my $default = $config->{default} // 'en'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"default"} // "en"
MC/DC0%0/1
$config->{"default"} // "en"
$config->{"default"}
Truth table50%1/2
A
A= $config->{"default"}
| |||||||||||||||||||||||||||||||||
| 133 | 2 | $self->{default} = $default; | |||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | # ── i18n_lexicons helper -- exposes translations loaded by Action::I18N ── | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 2 | ▶ | $app->helper(i18n_lexicons => sub { $app->{i18n_lexicons} //= {} }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$app->{"i18n_lexicons"} //= {}
MC/DC0%0/1
$app->{"i18n_lexicons"} //= {}
$app->{"i18n_lexicons"}
Truth table50%1/2
A
A= $app->{"i18n_lexicons"}
| |||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | # ── before_dispatch: detect language + cache lexicon ref in stash ─── | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 8 | ▶ | $app->hook(before_dispatch => sub ($c) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||
| 142 | 8 | $self->_detect_language($c, $config); | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | # Cache the current language's lexicon ref in stash for fast l() lookup | ||||||||||||||||||||||||||||||||
| 145 | 8 | ▶ | my $lang = $c->stash('i18n_lang') // $default; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$c->stash("i18n_lang") // $default
MC/DC0%0/2
$c->stash("i18n_lang") // $default
$c->stash("i18n_lang")
$default
Truth table33%1/3
A // B
A= $c->stash("i18n_lang")B= $default
| |||||||||||||||||||||||||||||||||
| 146 | 8 | ▶ | my $lexicon = $c->i18n_lexicons->{$lang} // {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$c->i18n_lexicons->{$lang} // {}
MC/DC0%0/1
$c->i18n_lexicons->{$lang} // {}
$c->i18n_lexicons->{$lang}
Truth table50%1/2
A
A= $c->i18n_lexicons->{$lang}
| |||||||||||||||||||||||||||||||||
| 147 | 8 | $c->stash(i18n_lexicon => $lexicon); | |||||||||||||||||||||||||||||||
| 148 | 2 | }); | |||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | # ── l() helper -- one hash lookup, no chasing ──────────────────────── | ||||||||||||||||||||||||||||||||
| 151 | # Overrides the identity fallback from Fondation core. | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 8 | ▶ | $app->helper(l => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||
| 154 | 8 | my $c = shift; | |||||||||||||||||||||||||||||||
| 155 | 8 | my $text = shift; | |||||||||||||||||||||||||||||||
| 156 | 8 | ▶ | my $lexicon = $c->stash('i18n_lexicon') // {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$c->stash("i18n_lexicon") // {}
MC/DC0%0/1
$c->stash("i18n_lexicon") // {}
$c->stash("i18n_lexicon")
Truth table50%1/2
A
A= $c->stash("i18n_lexicon")
| |||||||||||||||||||||||||||||||||
| 157 | 8 | ▶ | return $lexicon->{$text} // $text; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$lexicon->{$text} // $text
MC/DC0%0/2
$lexicon->{$text} // $text
$lexicon->{$text}
$text
Truth table66%2/3
A // B
A= $lexicon->{$text}B= $text
| |||||||||||||||||||||||||||||||||
| 158 | 2 | }); | |||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | # ── languages() helper -- get/set current language, refresh cache ───── | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 4 | ▶ | $app->helper(languages => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||
| 163 | 4 | my $c = shift; | |||||||||||||||||||||||||||||||
| 164 | 4 | my $lang = shift; | |||||||||||||||||||||||||||||||
| 165 | 4 | ▶ | if (defined $lang) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 166 | 2 | $c->stash(i18n_lang => $lang); | |||||||||||||||||||||||||||||||
| 167 | $c->stash(i18n_lexicon => | ||||||||||||||||||||||||||||||||
| 168 | 2 | ▶ | $c->i18n_lexicons->{$lang} // {}); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$c->i18n_lexicons->{$lang} // {}
MC/DC0%0/1
$c->i18n_lexicons->{$lang} // {}
$c->i18n_lexicons->{$lang}
Truth table50%1/2
A
A= $c->i18n_lexicons->{$lang}
| |||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | 4 | ▶ | return $c->stash('i18n_lang') // $default; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$c->stash("i18n_lang") // $default
MC/DC0%0/2
$c->stash("i18n_lang") // $default
$c->stash("i18n_lang")
$default
Truth table33%1/3
A // B
A= $c->stash("i18n_lang")B= $default
| |||||||||||||||||||||||||||||||||
| 171 | 2 | }); | |||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | # ── i18n_js helper -- injects current lexicon as JS object ──────────── | ||||||||||||||||||||||||||||||||
| 174 | # Overrides the identity fallback from Fondation core. | ||||||||||||||||||||||||||||||||
| 175 | # Called by layout via %== i18n_js before any app JS. | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | $app->helper(i18n_js => sub ($c) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | my $lang = $c->stash('i18n_lang') // $self->{default}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$c->stash("i18n_lang") // $self->{"default"}
MC/DC0%0/2
$c->stash("i18n_lang") // $self->{"default"}
$c->stash("i18n_lang")
$self->{"default"}
Truth table0%0/3
A // B
A= $c->stash("i18n_lang")B= $self->{"default"}
| |||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | # Memoize serialized JSON per language (lexicon loaded once at startup) | ||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | $app->{i18n_js_json}{$lang} //= do { | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$app->{"i18n_js_json"}{$lang} //= do {
my $lexicon = $c->i18n_lexicons->{$lang} // {};
my $json = encode_json($lexicon);
utf8::decode($json);
$json
}
MC/DC0%0/2
$app->{"i18n_js_json"}{$lang} //= do {
my $lexicon = $c->i18n_lexicons->{$lang} // {};
my $json = encode_json($lexicon);
utf8::decode($json);
$json
}
$app->{"i18n_js_json"}{$lang}
do {
my $lexicon = $c->i18n_lexicons->{$lang} // {};
my $json = encode_json($lexicon);
utf8::decode($json);
$json
}
Truth table0%0/3
A //= B
A= $app->{"i18n_js_json"}{$lang}B= do {
my $lexicon = $c->i18n_lexicons->{$lang} // {};
my $json = encode_json($lexicon);
utf8::decode($json);
$json
}
| |||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | my $lexicon = $c->i18n_lexicons->{$lang} // {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$c->i18n_lexicons->{$lang} // {}
MC/DC0%0/1
$c->i18n_lexicons->{$lang} // {}
$c->i18n_lexicons->{$lang}
Truth table0%0/2
A
A= $c->i18n_lexicons->{$lang}
| |||||||||||||||||||||||||||||||||
| 183 | 0 | my $json = encode_json($lexicon); | |||||||||||||||||||||||||||||||
| 184 | 0 | utf8::decode($json); | |||||||||||||||||||||||||||||||
| 185 | 0 | $json; | |||||||||||||||||||||||||||||||
| 186 | }; | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 0 | my $json = $app->{i18n_js_json}{$lang}; | |||||||||||||||||||||||||||||||
| 189 | 0 | return Mojo::ByteStream->new( | |||||||||||||||||||||||||||||||
| 190 | qq{<script>window._i18n=$json;window.l=function(k){return _i18n[k]||k;};</script>} | ||||||||||||||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||||||||||||||
| 192 | 2 | }); | |||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | # ── format_date helper -- locale-aware date formatting ──────────────── | ||||||||||||||||||||||||||||||||
| 195 | # Overrides the identity fallback from Fondation core. | ||||||||||||||||||||||||||||||||
| 196 | # Accepts DateTime objects, SQL datetime strings (YYYY-MM-DD HH:MM:SS), | ||||||||||||||||||||||||||||||||
| 197 | # or ISO 8601 strings (YYYY-MM-DDTHH:MM:SSZ). | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $app->helper(format_date => sub ($c, $val) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | return '' unless $val; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 201 | my $dt = ref $val | ||||||||||||||||||||||||||||||||
| 202 | ? $val | ||||||||||||||||||||||||||||||||
| 203 | 0 | : eval { Time::Piece->strptime($val, '%Y-%m-%d %H:%M:%S') } | |||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | || eval { Time::Piece->strptime($val, '%Y-%m-%dT%H:%M:%SZ') } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%d %H:%M:%S")
}
} or eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%dT%H:%M:%SZ")
}
} or return $val
eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%d %H:%M:%S")
}
} or eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%dT%H:%M:%SZ")
}
}
MC/DC0%0/2
eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%d %H:%M:%S")
}
} or eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%dT%H:%M:%SZ")
}
} or return $val
eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%d %H:%M:%S")
}
}
eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%dT%H:%M:%SZ")
}
}
Truth table0%0/3
A or B
A= eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%d %H:%M:%S")
}
}B= eval {
do {
"Time::Piece"->strptime($val, "%Y-%m-%dT%H:%M:%SZ")
}
}
| |||||||||||||||||||||||||||||||||
| 205 | || return $val; | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | my $lang = $c->stash('i18n_lang') // $default; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$c->stash("i18n_lang") // $default
MC/DC0%0/2
$c->stash("i18n_lang") // $default
$c->stash("i18n_lang")
$default
Truth table0%0/3
A // B
A= $c->stash("i18n_lang")B= $default
| |||||||||||||||||||||||||||||||||
| 208 | 0 | my $locale = $self->_locale_for($lang); | |||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if ($locale) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 210 | 0 | my $old = setlocale(LC_TIME); | |||||||||||||||||||||||||||||||
| 211 | 0 | setlocale(LC_TIME, $locale); | |||||||||||||||||||||||||||||||
| 212 | 0 | my $date = $dt->strftime('%x %R'); | |||||||||||||||||||||||||||||||
| 213 | 0 | setlocale(LC_TIME, $old); | |||||||||||||||||||||||||||||||
| 214 | 0 | utf8::decode($date); | |||||||||||||||||||||||||||||||
| 215 | 0 | return $date; | |||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | 0 | return $dt->strftime('%Y-%m-%d %H:%M'); | |||||||||||||||||||||||||||||||
| 218 | 2 | }); | |||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | # ── /i18n/<lang>.json endpoint for client-side JS ──────────────────── | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 2 | ▶ | $app->routes->get('/i18n/<lang>.json')->to(cb => sub ($c) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||
| 223 | 2 | my $lang = $c->param('lang'); | |||||||||||||||||||||||||||||||
| 224 | 2 | $lang =~ s/[^a-z_\-]//g; | |||||||||||||||||||||||||||||||
| 225 | 2 | ▶ | return $c->reply->not_found unless length $lang; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 2 | my $lexicon = $c->i18n_lexicons->{$lang}; | |||||||||||||||||||||||||||||||
| 228 | 2 | ▶ | return $c->reply->not_found unless $lexicon && %$lexicon; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$lexicon and %$lexicon
MC/DC50%1/2
$lexicon and %$lexicon
$lexicon
%$lexicon
Truth table66%2/3
A and B
A= $lexiconB= %$lexicon
| |||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | 1 | $c->render(json => $lexicon); | |||||||||||||||||||||||||||||||
| 231 | 2 | }); | |||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 238 | # _detect_language -- parse Accept-Language header, set stash | ||||||||||||||||||||||||||||||||
| 239 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | 8 | ▶ | sub _detect_language ($self, $c, $config) { | ||||||||||||||||||||||||||||||
|
Subroutine
_detect_language: called CC=6 SCAR=20.5
| |||||||||||||||||||||||||||||||||
| 242 | 8 | my @languages; | |||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | # 1) URL prefix detection | ||||||||||||||||||||||||||||||||
| 245 | 8 | ▶ | if (my $langs = $config->{support_url_langs}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 246 | 8 | ▶ | if (my $path = $c->req->url->path) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 247 | 8 | my $part = $path->parts->[0]; | |||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | if ($part && grep { $part eq $_ } @$langs) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$part and grep {$part eq $_;} @$langs
MC/DC0%0/2
$part and grep {$part eq $_;} @$langs
$part
grep {$part eq $_;} @$langs
Truth table33%1/3
A and B
A= $partB= grep {$part eq $_;} @$langs
| |||||||||||||||||||||||||||||||||
| 249 | 0 | unshift @languages, $part; | |||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | # 2) Accept-Language header | ||||||||||||||||||||||||||||||||
| 255 | 8 | my @header_langs = I18N::LangTags::implicate_supers( | |||||||||||||||||||||||||||||||
| 256 | I18N::LangTags::Detect->http_accept_langs( | ||||||||||||||||||||||||||||||||
| 257 | $c->req->headers->accept_language | ||||||||||||||||||||||||||||||||
| 258 | ) | ||||||||||||||||||||||||||||||||
| 259 | ); | ||||||||||||||||||||||||||||||||
| 260 | 8 | push @languages, @header_langs; | |||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | # 3) Default | ||||||||||||||||||||||||||||||||
| 263 | 8 | push @languages, $self->{default}; | |||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | 8 | ▶ | $c->stash(i18n_lang => $languages[0]) if $languages[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 269 | # _locale_for -- map language code to POSIX locale | ||||||||||||||||||||||||||||||||
| 270 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 2 | my %LOCALE_FOR = ( | |||||||||||||||||||||||||||||||
| 273 | fr => 'fr_FR.UTF-8', | ||||||||||||||||||||||||||||||||
| 274 | en => 'en_US.UTF-8', | ||||||||||||||||||||||||||||||||
| 275 | ro => 'ro_RO.UTF-8', | ||||||||||||||||||||||||||||||||
| 276 | zh => 'zh_CN.UTF-8', | ||||||||||||||||||||||||||||||||
| 277 | de => 'de_DE.UTF-8', | ||||||||||||||||||||||||||||||||
| 278 | es => 'es_ES.UTF-8', | ||||||||||||||||||||||||||||||||
| 279 | it => 'it_IT.UTF-8', | ||||||||||||||||||||||||||||||||
| 280 | pt => 'pt_PT.UTF-8', | ||||||||||||||||||||||||||||||||
| 281 | ru => 'ru_RU.UTF-8', | ||||||||||||||||||||||||||||||||
| 282 | ja => 'ja_JP.UTF-8', | ||||||||||||||||||||||||||||||||
| 283 | ko => 'ko_KR.UTF-8', | ||||||||||||||||||||||||||||||||
| 284 | ); | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | sub _locale_for ($self, $lang) { | ||||||||||||||||||||||||||||||
|
Subroutine
_locale_for: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | return $LOCALE_FOR{$lang} if $LOCALE_FOR{$lang}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 288 | 0 | my $short = substr($lang, 0, 2); | |||||||||||||||||||||||||||||||
| 289 | 0 | return $LOCALE_FOR{$short}; | |||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | 2 | 1; | |||||||||||||||||||||||||||||||