Maybe it's ugly but for me forks just fine
use Cro::WebApp::Form;
enum Lang <DE EN ES GE PL>;
enum Country <DEU USA ESP GEO PLN>;
enum Strings (user-name => "user-name", password => "password", name => "name", company-name => "company-name", yes => "yes", no => "no", enter => "enter", cancel => "cancel", escape => "escape", submit => "submit");
my %STRINGS =
user-name => { GE => "მომხმარებლის სახელი", EN => "user name" },
password => { GE => "პაროლი", EN => "password" },
name => { GE => "სახელი", EN => "name" },
company-name => { GE => "კომპანიის სახელი", EN => "company name" },
yes => { GE => "დიახ", EN => "yes" },
no => { GE => "არა", EN => "no" },
enter => { GE => "შედი", EN => "enter" },
cancel => { GE => "გააუქმოს", EN => "cancel" },
escape => { GE => "გაქცევა", EN => "escape" },
submit => { GE => "წარადგინოს", EN => "submit" };
role L10N::Form does Cro::WebApp::Form is export {
has Lang $.lang is rw is hidden;
method !calculate-label($attr) {
my $label = do with $attr.?webapp-form-label {
# Explicitly provided label
return %STRINGS{$_}{$!lang} if so %STRINGS{$_}{$!lang};
return $_;
} else {
# Fall back to mangling the attribute name.
my $attr-name = $attr.name.substr(2);
return %STRINGS{$attr-name}{$!lang} if so %STRINGS{$attr-name}{$!lang};
my @words = $attr-name.split('-');
@words[0] .= tclc;
return @words.join(' ');
};
return $label;
}
method empty(Lang $lang) {
my $form = self.CREATE;
$form.lang = $lang;
return $form;
}
}
sub l10n(Lang $lang, $str --> Str) is export {
return %STRINGS{$str}{$lang} if so %STRINGS{$str}{$lang};
return "undefined";
}
and then in some other file
use Cro::WebApp::Form;
class LoginForm does L10N::Form { has $.user-name is label(Strings::name) };
LoginForm.empty(GE).HTML-RENDER-DATA;
gives result
{controls => [{label => სახელი, name => user-name, required => False, type => text} {label => Lang, name => lang, required => False, type => hidden, value => GE}], was-validated => False}
and
class LoginForm1 does L10N::Form { has $.user-name is required; has $.password is required; has $.company-name };
LoginForm1.empty(GE).HTML-RENDER-DATA;
gives
{controls => [{label => მომხმარებლის სახელი, name => user-name, required => True, type => text} {label => პაროლი, name => password, required => True, type => text} {label => კომპანიის სახელი, name => company-name, required => False, type => text} {label => Lang, name => lang, required => False, type => hidden, value => GE}], was-validated => False}
Maybe it's ugly but for me forks just fine
and then in some other file
gives result
and
gives