NAME

Locale::Maketext::Lexicon::DB - Dynamically load lexicon from a database table

VERSION

version 1.141830

SYNOPSIS

package MyApp::Maketext;

use Moose;
use DBI;
use Cache::Memcached::Fast;

BEGIN { extends 'Locale::Maketext::Lexicon::DB'; }

has '+dbh' => (
    builder => '_build_dbh',
);

sub _build_dbh {
    my $self = shift;

    return DBI->connect( ... );
}

has '+cache' => (
    builder => '_build_cache',
);

sub _build_cache {
    my $self = shift;

    return Cache::Memcached::Fast->new({
        servers => [ ... ],
    });
}

has '+cache_expiry_seconds' => (
    default => 3_600,
);

has '+lex' => (
    default => 'myapp',
);

has '+auto' => (
    default => 1,
);

has '+language_mappings' => (
    default => sub {
        {
            en_gb   => [qw(en_gb en)],
            en_us   => [qw(en_us en)],
            en      => [qw(en)],
        }
    },
);


package main;

my $handle = MyApp::Maketext->get_handle('en_gb');
# or, to use the environment to get the language from locale settings:
# my $handle = MyApp::Maketext->get_handle;

print $handle->maketext('ui.homepage.title', $name);

DESCRIPTION

This module enables you to crate a Locale::Maketext lexicon in your database. The lexicon is compiled when get_handle is called on the class. If a cache is defined then the lexicon is retrieved from the cache instead of hitting the database each time. A class method is provided to clear the cache i.e. to invalidate it if the lexicon in the DB changes.

METHODS

get_handle ([@languages])

Returns a Locale::Maketext::Lexicon::DB::Handle for this lexicon. if @languages are not supplied then inspects the environment to get the set locale.

clear_cache

Clears the cache (if set) for this lexicon. Used to invalidate the cache if the database has changed.

DATABASE TABLE

Your database should be in this format (this DDL is for SQLite).

CREATE TABLE lexicon (
    id INTEGER PRIMARY KEY NOT NULL,
    lang VARCHAR NOT NULL,
    lex VARCHAR NOT NULL,
    lex_key TEXT NOT NULL,
    lex_value TEXT NOT NULL
);
id

The primary key for the table

lang

The locale string for the language for this entry

lex

A key to identify the entire lexicon in the table. This enables you to set define more than one lexicon in the table

lex_key

The key for the lexicon entry. This is the value passed to the maketext method on the handle

lex_value

The value for the lexicon entry

AUTHOR

Pete Smith <pete@cubabit.net>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Pete Smith.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.