GithubHelp home page GithubHelp logo

mojolicious-plugin-i18n's Introduction

NAME

Mojolicious::Plugin::I18N - Internationalization Plugin for Mojolicious

SYNOPSIS

# Mojolicious
$self->plugin('I18N');
% languages 'de';
%=l 'hello'

# Mojolicious::Lite (detect language from URL, i.e. /en/ or /de/)
plugin I18N => {namespace => 'MyApp::I18N', support_url_langs => [qw(en de)]};
%=l 'hello'

# Lexicon
package MyApp::I18N::de;
use Mojo::Base 'MyApp::I18N';

our %Lexicon = (hello => 'hallo');

1;

DESCRIPTION

Mojolicious::Plugin::I18N is internationalization plugin for Mojolicious It works with Mojolicious 4.0+.

Old namespace is Mojolicious::Plugin::I18N2.

OPTIONS

Mojolicious::Plugin::I18N supports the following options.

support_url_langs

plugin I18N => {support_url_langs => [qw(en de)]};

Detect language from URL.

support_hosts

plugin I18N => {support_hosts => { 'mojolicious.ru' => 'ru', 'mojolicio.us' => 'en' }};

Detect Host header and use language for that host.

no_header_detect

plugin I18N => {no_header_detect => 1};

Off header detect.

default

plugin I18N => {default => 'en'};

Default language for i18n, defaults to en.

namespace

plugin I18N => {namespace => 'MyApp::I18N'};

Lexicon namespace, defaults to the application class followed by ::I18N.

HELPERS

Mojolicious::Plugin::I18N implements helpers same as Mojolicious::Plugin::I18N.

l

%=l 'hello'
$self->l('hello');

Translate sentence.

languages

% languages 'de';
$self->languages('de');

Change languages.

METHODS

Mojolicious::Plugin::I18N inherits all methods from Mojolicious::Plugin::I18N and reimplements the following new ones.

register

$plugin->register;

Register plugin hooks and helpers in Mojolicious application.

DEBUG MODE

Mojolicious::Plugin::I18N has debug mode.

# debug mode on
BEGIN { $ENV{MOJO_I18N_DEBUG} = 1 };

# or
MOJO_I18N_DEBUG=1 perl script.pl

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.

AUTHORS

2011-2014 Anatoly Sharifulin <[email protected]>

2010-2012 Sebastian Riedel <[email protected]>

BUGS

Please report any bugs or feature requests to bug-mojolicious-plugin-i18n at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.htMail?Queue=Mojolicious-Plugin-I18N. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

COPYRIGHT & LICENSE

Copyright (C) 2011-2014 by Anatoly Sharifulin. Copyright (C) 2008-2012, Sebastian Riedel.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

mojolicious-plugin-i18n's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mojolicious-plugin-i18n's Issues

Receipt for bundling internationalized plugins

As far as I can see there is currently no option to bundle separated lexicons with a i18n aware plugin. This may be possible by providing a namespace array similar to Mojolicious::Plugins.

If I am wrong and there is a way in bundling lexicons with plugins, it should be named in the readme.

Thank you very much for maintaining this module!

Load language files outside of http server (e.g. Minion)

Hi,

when using Minion (job manager for Mojolicious, which ties nicely into the framework) to process background jobs the language files aren't being loaded by the I18N plugin. One use case where this would be nice to have is when you send localized emails in the background.

It think the problem lies within the way this plugin initializes. It uses the before_dispatch hook, which as far as I can see isn't being executed by a Minion worker.

Would there be a way to be able to load the language files even when the before_dispatch hook isn't being fired? Manually setting the desired language for such use cases would be fine, no need for auto detection I think.

Odd number of elements in hash assignment at lib/perl5/Mojolicious/Plugin/I18N.pm line 122.

It looks like the plugin does not allow a Mojo::URL object to be the first argument to url_for, though Mojolicious allows it and simply passes the URL through. It seems like the fix would be to add another check when trying to discard an element from @_:

-        shift if (@_ % 2 && !ref $_[0]) || (@_ > 1 && ref $_[-1]);
+        shift if (@_ % 2 && (!ref $_[0] || ref $_[0] eq 'Mojo::URL')) || (@_ > 1 && ref $_[-1]);

Installation fails on Mojolicious 6.14

If I try to install this plugin on Mojolicious 6.14 (Perl 5.22.0, perlbrew on Debian 8.1) I get the following error:

#   Failed test 'exact match for content'
#   at t/i18n_url_for.t line 89.
#          got: '<a href="http://example.com/widget?lang=ru&token_url=http://127.0.0.1:56057/login?next=%2Fauth">auth</a>
# '
#     expected: '<a href="http://example.com/widget?lang=ru&token_url=http://127.0.0.1:56057/login?next=/auth">auth</a>
# '

#   Failed test 'exact match for content'
#   at t/i18n_url_for.t line 95.
#          got: '<a href="http://example.com/widget?lang=ru&token_url=http://127.0.0.1:56057/ru/login?next=%2Fru%2Fauth">auth</a>
# '
#     expected: '<a href="http://example.com/widget?lang=ru&token_url=http://127.0.0.1:56057/ru/login?next=/ru/auth">auth</a>
# '

#   Failed test 'exact match for content'
#   at t/i18n_url_for.t line 101.
#          got: '<a href="http://example.com/widget?lang=en&token_url=http://127.0.0.1:56057/en/login?next=%2Fen%2Fauth">auth</a>
# '
#     expected: '<a href="http://example.com/widget?lang=en&token_url=http://127.0.0.1:56057/en/login?next=/en/auth">auth</a>
# '
# Looks like you failed 3 tests of 45.
t/i18n_url_for.t ............ 
Dubious, test returned 3 (wstat 768, 0x300)
Failed 3/45 subtests

Can't locate FH/I18N/.pm

When 'default' is not initialized I got this error:

(Can't locate FH/I18N/.pm in @INC (@INC contains: /home/kes/perl_lib/lib/perl5/x86_64-linux-gnu-thread-multi /home/kes/perl_lib/lib/perl5 /home/kes/work/x/site/app/lib /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at (eval 589)[/usr/local/share/perl/5.14.2/Mojolicious/Plugin/I18N.pm:196] line 1.

It seems you miss next unless $_ in you for loop at :190

Configure routes without processing by plugin

Hi

I have a question about plugin - how best way to do some route without processing by plugin and not added language part, for example i want to to the admin routes not have language part

URL issue with http proxy

Hello,

When using this plugin behind a reverse proxy with path, the urls generated by "url_for" doesn't work as expected anymore. We end up having a path that look like /{LANG}/{PATH}/foo instead of /{PATH}/{LANG}/foo.

The method used to set the reverse proxy path is taken from this blog post : https://mojolicious.io/blog/2019/03/18/reverse-proxy-with-path/

Here is a small app to reproduce the problem :

use Mojolicious::Lite -signatures;

plugin I18N => {namespace => 'MyApp::I18N', support_url_langs => [qw(en fr)]};

if ( my $path = $ENV{MOJO_REVERSE_PROXY} ) {
    my @path_parts = grep /\S/, split m{/}, $path;
    app->hook( before_dispatch => sub ($c) {
        my $url = $c->req->url;
        my $base = $url->base;
        push @{ $base->path }, @path_parts;
        $base->path->trailing_slash(1);
        $url->path->leading_slash(0);
    });
}

get '/' => sub ($c) {
  $c->render(text => $c->url_for('/foo'));
};

app->start;

Then :

MOJO_REVERSE_PROXY="/myapp" ./base_i18n.pl get /fr/
/fr/myapp/foo

And i was expecting /myapp/fr/foo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.