File: I18n.pm

package info (click to toggle)
libtest-bdd-cucumber-perl 0.75-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 680 kB
  • sloc: perl: 7,905; makefile: 8; sh: 5
file content (152 lines) | stat: -rw-r--r-- 3,064 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package Test::BDD::Cucumber::I18n;
$Test::BDD::Cucumber::I18n::VERSION = '0.75';
=encoding utf8

=head1 NAME

Test::BDD::Cucumber::I18N - Internationalization

=head1 VERSION

version 0.75

=head1 DESCRIPTION

Internationalization of feature files and step definitions.

=head1 SYNOPSIS

  use Test::BDD::Cucumber::I18n
      qw(languages has_language langdef);

  # get codes of supported languages
  my @supported_languages = languages();

  # look up if a language is supported
  my $language_is_supported = has_language('de');

  # get definition of a language
  my $langdef = langdef('de');

  # get readable keyword definitions
  my $string = readable_keywords

=cut

use strict;
use warnings;
use utf8;

use base 'Exporter';

our @EXPORT_OK =
  qw(languages langdef has_language readable_keywords keyword_to_subname);

use Test::BDD::Cucumber::I18N::Data;

my $langdefs = _initialize_language_definitions_from_shared_json_file();

sub _initialize_language_definitions_from_shared_json_file {

    # Parse keywords hash for all supported languages from the JSON file
    my $langdefs = Test::BDD::Cucumber::I18N::Data::language_definitions();

    # strip asterisks from the keyword definitions since they don't work yet
    for my $language ( keys %$langdefs ) {
        my $langdef = $langdefs->{$language};
        for my $key ( keys %$langdef ) {
            $langdef->{$key} =~ s{[*]\s*[|]}{};
        }
    }

    return $langdefs;
}

=head1 METHODS

=head2 languages

Get codes of supported languages.

=cut

sub languages {
    return keys %$langdefs;
}

=head2 has_language($language)

Check if a language is supported.  Takes as argument the language
abbreviation defined in C<share/i18n.json>.

=cut

sub has_language {
    my ($language) = @_;
    exists $langdefs->{$language};
}

=head2 langdef($language)

Get definition of a language.  Takes as argument the language abbreviation
defined in C<share/i18n.json>.

=cut

sub langdef {
    my ($language) = @_;

    return unless has_language($language);
    return $langdefs->{$language};
}

=head2 readable_keywords($string, $transform)

Get readable keyword definitions.

=cut

sub readable_keywords {
    my ( $string, $transform ) = @_;

    my @keywords = split( /\|/, $string );

    @keywords = map { $transform->($_) } @keywords if $transform;

    return join( ', ', map { '"' . $_ . '"' } @keywords );
}

=head2 keyword_to_subname

Return a keyword into a subname with non-word characters removed.

=cut

sub keyword_to_subname {
    my ($word) = @_;

    # remove non-word characters so we have a decent sub name
    $word =~ s{[^\p{Word}]}{}g;

    return $word;
}

=head1 LANGUAGES

Languages are defined in a JSON-based hash in the __DATA__ section of
L<Test::BDD::Cucumber::I18N::Data>, and have been lifted from the
Gherkin distribution.

=head1 AUTHOR

Gregor Goldbach C<glauschwuffel@nomaden.org>
(based on the works of Pablo Duboue)

=head1 LICENSE

  Copyright 2019-2020, Erik Huelsmann
  Copyright 2014-2019, Gregor Goldbach; Licensed under the same terms as Perl

=cut

1;