Last active
January 2, 2016 02:39
-
-
Save diegok/8238365 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reimplement "url_for" helper | |
my $mojo_url_for = *Mojolicious::Controller::url_for{CODE}; | |
my $i18n_url_for = sub { | |
my $self = shift; | |
my $url = $self->$mojo_url_for(@_); | |
# Absolute URL | |
return $url if $url->is_abs; | |
# discard target if any | |
shift if @_ && (!(@_ % 2) || !ref $_[0]); | |
# unwrap params | |
my %params = @_ == 1 ? %{$_[-1]} : @_; | |
# Detect lang | |
if (my $lang = $params{lang} || $self->stash('lang')) { | |
my $path = $url->path || []; | |
# Root | |
if (!$path->[0]) { | |
$path->parts([ $lang ]); | |
} | |
# No language detected | |
elsif ( ref $langs ne 'ARRAY' or not scalar grep { $path->contains("/$_") } @$langs ) { | |
unshift @{ $path->parts }, $lang; | |
} | |
} | |
return $url; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment