1 package Text::Tradition::WitLanguage;
11 Text::Tradition::WitLanguage - add-on role to enable language awareness and
12 morphology functions to a Text::Tradition::Witness object. Please see
13 L<Text::Tradition::Morphology> for more information on the morphology
20 Accessor for the primary language of the tradition. Must correspond to one
21 of the Text::Tradition::Language::* modules in this package. Used for JSON
22 export of a language-regularized witness text.
28 use_ok( 'Text::Tradition' ); # with Language
29 use_ok( 'Text::Tradition::Witness' ); # with WitLanguage
38 predicate => 'has_language',
41 around 'language' => sub {
44 if( @_ && $_[0] ne 'Default' ) {
45 # We are trying to set the language; check that the corresponding
48 load( "Text::Tradition::Language::".$_[0] );
50 warn( "Cannot load language module for @_: $e" );
52 } elsif( !$self->has_language && $self->tradition->has_language ) {
53 return $self->tradition->language;
58 around 'export_as_json' => sub {
61 my $answer = $self->$orig( @_ );
62 if( $self->has_language || $self->tradition->has_language ) {
63 # If we do have a language, regularize the tokens in $answer.
64 my $mod = "Text::Tradition::Language::" . $self->language;
66 my $rsub = $mod->can( 'regularize' );
67 map { $_->{'n'} = $rsub->( $_->{'t'} ) } @{$answer->{tokens}};
68 if( exists $answer->{layertokens} ) {
69 map { $_->{'n'} = $rsub->( $_->{'t'} ) } @{$answer->{layertokens}};
72 warn "Please set a language to regularize a tradition";