DBIx-Class-I18NColumns
view release on metacpan or search on metacpan
lib/DBIx/Class/I18NColumns.pm view on Meta::CPAN
Version 0.15
=cut
=head1 SYNOPSIS
package MySchema::Result::Song;
use strict;
use warnings;
use parent 'DBIx::Class';
__PACKAGE__->load_components( qw/ I18NColumns Core / );
__PACKAGE__->table( 'song' );
__PACKAGE__->add_columns(
'id',
{ data_type => 'INT', default_value => 0, is_nullable => 0 },
'author',
{ data_type => 'VARCHAR', default_value => "", is_nullable => 0, size => 255 },
);
__PACKAGE__->add_i18n_columns(
'title',
{ data_type => 'VARCHAR', default_value => "", is_nullable => 0, size => 255 },
'lyrics',
{ data_type => 'TEXT', default_value => "", is_nullable => 0 },
);
__PACKAGE__->set_primary_key( 'id' );
1;
# then, you have an auto generated resultset where title and lyrics are stored
# in different languages:
my $song = $myschema->resultset( 'Song' )->create({
author => 'Flopa',
title => 'Germinar',
lyrics => 'La vida se toma como el vino pura, y ...',
language => 'es',
});
print $song->title; # prints 'Germinar'
$song->language('en');
$song->title('To germinate'); # set title in english
$song->lyrics('traslated lyrics'); # set lyrics in english
$song->update; # store title and lyrics
print $song->title; # prints 'To Germinate'
print $song->title(['es']); # prints 'Germinar'
$song->language('es');
print $song->title; # prints 'Germinar'
=cut
=head1 DESCRIPTION
This module allows you to define columns that will store multiple values asociated to a language string and use it as normal columns. This is useful when you need to internationalize attributes of your DB entities.
This component will create a new resultset on your schema for each one that use it. The auto-created resultset will use the columns definition you give to add_i18n_columns() plus a FK and language columns. The i18n values of each language will reside...
Language will be propagated to relationships with result sources that also use this component.
=head1 METHODS
=head2 add_i18n_columns
Create internationalizable columns. The columns are created in the same
way you do with in L<add_columns|DBIx::Class::ResultSource/add_columns>.
If you don't specify the data_type, varchar will be used by default.
=cut
sub add_i18n_columns {
my $self = shift;
my @columns = @_;
$self->_i18n_columns( {} ) unless defined $self->_i18n_columns();
$self->resultset_class( 'DBIx::Class::ResultSet::I18NColumns' )
if $self->auto_resultset_class;
# Add columns & accessors
while ( my $column = shift @columns ) {
my $column_info = ref $columns[0] ? shift(@columns) : {};
$column_info->{data_type} = lc( $column_info->{data_type} || 'varchar' );
# Check column
$self->throw_exception( "Cannot override existing column '$column' with this i18n one" )
if ( $self->has_column($column) || exists $self->_i18n_columns->{$column} );
$self->_i18n_columns->{$column} = $column_info;
my $accessor = $column_info->{accessor} || $column;
# Add accessor
no strict 'refs';
*{ $self . '::' . $accessor } = sub {
my $self = shift;
$self->_i18n_method( $column => @_ );
};
}
$self->_create_i18n_result_source if $self->auto_i18n_rs;
$self->_add_schema_accessor;
$self->_add_languages_column;
}
=head2 language
Get or set the language for the row.
=cut
sub language {
my $self = shift;
if ( my $value = shift ) {
$self->result_source->schema->_language_last_set($value);
return $self->_language($value);
}
( run in 1.529 second using v1.01-cache-2.11-cpan-0b5f733616e )