File Coverage

lib/Kephra/Config/Localisation.pm
Criterion Covered Total %
statement 12 95 12.6
branch 0 34 0.0
condition 0 17 0.0
subroutine 4 21 19.0
pod 0 12 0.0
total 16 179 8.9


line stmt bran cond sub pod time code
1             package Kephra::Config::Localisation;
2             our $VERSION = '0.08';
3            
4 1     1   1221 use strict;
  1         4  
  1         37  
5 1     1   5 use warnings;
  1         4  
  1         30  
6            
7 1     1   8 use File::Find();
  1         3  
  1         26  
8 1     1   6 use YAML::Tiny();
  1         1  
  1         1583  
9            
10             # handling config files under config/localisation
11             my %strings;
12 0 0   0     sub _set_strings { %strings = %{$_[0]} if ref $_[0] eq 'HASH' }
  0            
13 0     0 0   sub strings { \%strings }
14 0     0     sub _config { Kephra::API::settings()->{app}{localisation} }
15 0 0   0     sub _sub_dir { _config->{directory} if _config->{directory} }
16            
17             my %index;
18 0 0   0     sub _index { if (ref $_[0] eq 'HASH') {%index = %{$_[0]}} else { \%index } }
  0            
  0            
  0            
19             my $language;
20 0     0 0   sub language { $language }
21            
22 0     0 0   sub file { Kephra::Config::filepath( _sub_dir(), _config()->{file} ) }
23 0 0   0 0   sub set_file_name { file_name($_[0]) if defined $_[0]}
24             sub file_name {
25 0 0   0 0   if (defined $_[0]) { _config()->{file} = $_[0] } else { _config()->{file} }
  0            
  0            
26             }
27 0     0 0   sub set_lang_by_file { $language = $index{ _config()->{file} }{language} }
28            
29             #
30             sub load {
31 0     0 0   my $file = file();
32             # can only be conf because yaml tine doesnt support utf, 1 activates utf
33 0 0         my $l = Kephra::Config::File::load_conf( $file, 1 ) if defined $file;
34 0 0 0       $l = Kephra::Config::Default::localisation() unless $l and %$l;
35 0           %strings = %$l;
36 0           set_lang_by_file();
37             }
38            
39            
40             sub change_to {
41 0     0 0   my ($lang_file) = shift;
42 0 0         return unless $lang_file;
43 0           set_documentation_lang( _index()->{$lang_file}{iso_code} );
44 0           set_file_name( $lang_file );
45 0           Kephra::Config::Global::reload_tree();
46             }
47            
48             # open localisation file in the editor
49             sub open_file {
50 0     0 0   my $lang_file = shift;
51 0 0         $lang_file eq file_name()
52             ? Kephra::Config::open_file( _sub_dir(), $lang_file )
53             : Kephra::Document::add( Kephra::Config::filepath(_sub_dir(), $lang_file) );
54             }
55            
56             # create menus for l18n selection nd opening l18n files
57             sub create_menus {
58 0     0 0   my $l18n_index = _index();
59 0 0         return unless ref $l18n_index eq 'HASH';
60            
61 0           my $l18n = strings()->{commandlist}{help}{config};
62 0           my ($al_cmd, $fl_cmd) = ('config-app-lang', 'config-file-localisation');
63 0           my ($al_help, $fl_help) = Kephra::CommandList::get_property_list
64             ('help', $al_cmd, $fl_cmd);
65 0           my (@config_app_lang, @config_localisation);
66 0           for my $lang_file (sort keys %$l18n_index) {
67 0           my $lang_data = $l18n_index->{$lang_file};
68 0           my $lang = ucfirst $lang_data->{language};
69 0   0       my $lang_code = $lang_data->{iso_code} || '';
70 0           my $al_lang_cmd = "$al_cmd-$lang_code";
71 0           my $fl_lang_cmd = "$fl_cmd-$lang_code";
72 0           Kephra::CommandList::new_cmd( $al_lang_cmd, {
73             call => 'Kephra::Config::Localisation::change_to('."'".$lang_file."')",
74             state => 'Kephra::Config::Localisation::file_name() eq '."'".$lang_file."'",
75             label => $lang,
76             help => "$al_help $lang",
77             });
78 0           Kephra::CommandList::new_cmd( $fl_lang_cmd, {
79             call => 'Kephra::Config::Localisation::open_file('."'".$lang_file."')",
80             label => $lang,
81             help => "$fl_help $lang",
82             });
83 0           push @config_app_lang, 'item '.$al_lang_cmd;
84 0           push @config_localisation, 'item '.$fl_lang_cmd;
85             }
86 0           Kephra::Menu::create_static('config_localisation',\@config_localisation);
87 0           Kephra::Menu::create_static('config_app_lang', \@config_app_lang);
88             }
89            
90             sub refresh_index {
91 0     0 0   my $use_cache = Kephra::Config::Interface::_config()->{cache}{use};
92 0           my $index_file = Kephra::Config::filepath
93             (Kephra::Config::Interface::_cache_sub_dir(), 'index_l18n.yml');
94 0           my $l18n_dir = Kephra::Config::dirpath( _sub_dir() );
95            
96 0 0         my %old_index = %{ YAML::Tiny::LoadFile( $index_file ) } if -e $index_file;
  0            
97 0           my %new_index;
98            
99 0           my ($FH, $file_name, $age);
100 0           my $getmetaheader = qr|[\r\n]+(.*)[\r\n]+|s;
101 0           my $lines = qr/[\r\n]+/;
102 0           my $seperatekv = qr/\s*(\S+)\s*=\s*(.+)\s*/;
103             #$File::Find::prune = 0;
104             File::Find::find( sub {
105 0 0   0     return if -d $_;
106 0           $file_name = $_;
107 0           $age = Kephra::File::IO::get_age($file_name);
108             # if file is known and not refreshed just copy loaded data
109 0 0 0       if (exists $old_index{$file_name} and $age == $old_index{$file_name}{age}) {
110 0           $new_index{$file_name} = $old_index{$file_name};
111 0           return;
112             }
113 0           open $FH, "<", $file_name ; #:encoding(UTF-8)
114 0           binmode($FH, ":raw"); #:crlf
115 0           my ($chunk, $header, %filedata) = ('','');
116             #read just the meta data header
117 0           do {
118 0 0         return if eof $FH; # abort because no complete about header found
119 0           read $FH, $chunk, 1000;
120 0           $header .= $chunk;
121             } until $header =~ /$getmetaheader/;
122             # split to lines, delete spaces and extract keys and valuse
123 0           for (split /$lines/, $1){
124 0           /$seperatekv/;
125 0           $filedata{$1} = $2;
126             }
127 0           $filedata{'age'} = $age;
128             # filter out local backup files of l18n files - not save for any file ending
129 0 0         return if $filedata{language}.'.conf' ne $file_name;
130 0 0 0       $new_index{$file_name} = \%filedata
      0        
      0        
131             if defined $filedata{'purpose'}
132             and $filedata{'purpose'} eq 'global localisation'
133             # if its an stable enduser version the l18n strings has to be updated
134             and (not defined $Kephra::PATCHLEVEL or $filedata{'version'} eq $Kephra::VERSION);
135 0           }, $l18n_dir);
136            
137 0           YAML::Tiny::DumpFile($index_file, \%new_index);
138 0           _index(\%new_index);
139 0           \%new_index;
140             }
141            
142            
143             sub set_documentation_lang {
144 0     0 0   my $lang = shift;
145 0           return until $lang;
146 0 0         $lang = $lang eq 'de' ? 'deutsch' : 'english';
147 0           Kephra::Config::Global::merge_subfile_into_settings (
148             Kephra::Config::filepath('documentation', $lang.'.conf')
149             );
150             }
151            
152             1;
153