Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make basic localization #89

Open
emakei opened this issue Oct 9, 2024 · 0 comments
Open

Make basic localization #89

emakei opened this issue Oct 9, 2024 · 0 comments

Comments

@emakei
Copy link

emakei commented Oct 9, 2024

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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant