File: Language.pm

package info (click to toggle)
znuny 6.5.18-1
  • links: PTS
  • area: non-free
  • in suites: forky, sid
  • size: 205,344 kB
  • sloc: perl: 1,038,694; xml: 74,551; javascript: 65,276; sql: 23,574; sh: 417; makefile: 63
file content (1144 lines) | stat: -rw-r--r-- 32,414 bytes parent folder | download | duplicates (5)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
## nofilter(TidyAll::Plugin::Znuny::CodeStyle::STDERRCheck)

package Kernel::Language;

use strict;
use warnings;
use Pod::Strip;

use Exporter qw(import);
our @EXPORT_OK = qw(Translatable);    ## no critic

use File::stat;
use Digest::MD5;

use Kernel::System::DateTime;

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::System::DateTime',
    'Kernel::System::Log',
    'Kernel::System::Main',
    'Kernel::System::SysConfig',
);

=head1 NAME

Kernel::Language - global language interface

=head1 DESCRIPTION

All language functions.

=head1 PUBLIC INTERFACE

=head2 new()

create a language object. Do not use it directly, instead use:

    use Kernel::System::ObjectManager;
    local $Kernel::OM = Kernel::System::ObjectManager->new(
        'Kernel::Language' => {
            UserLanguage => 'de',
        },
    );
    my $LanguageObject = $Kernel::OM->Get('Kernel::Language');

=cut

sub new {
    my ( $Type, %Param ) = @_;

    my $Self = {%Param};
    bless( $Self, $Type );

    # 0=off; 1=on; 2=get all not translated words; 3=get all requests
    $Self->{Debug} = 0;

    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
    my $MainObject   = $Kernel::OM->Get('Kernel::System::Main');
    my $LogObject    = $Kernel::OM->Get('Kernel::System::Log');

    # user language
    $Self->{UserLanguage} = $Param{UserLanguage}
        || $ConfigObject->Get('DefaultLanguage')
        || 'en';

    # check if language is configured
    my %Languages = %{ $ConfigObject->Get('DefaultUsedLanguages') };
    if ( !$Languages{ $Self->{UserLanguage} } ) {
        $Self->{UserLanguage} = 'en';
    }

    # take time zone
    $Self->{TimeZone} = $Param{UserTimeZone} || $Param{TimeZone} || Kernel::System::DateTime->OTRSTimeZoneGet();

    # Debug
    if ( $Self->{Debug} > 0 ) {
        $LogObject->Log(
            Priority => 'debug',
            Message  => "UserLanguage = $Self->{UserLanguage}",
        );
    }

    $Self->{Home}         = $ConfigObject->Get('Home') . '/';
    $Self->{DefaultTheme} = $ConfigObject->Get('DefaultTheme');
    $Self->{UsedWords}    = {};
    $Self->{UsedInJS}     = {};

    my $LanguageFile = "Kernel::Language::$Self->{UserLanguage}";

    # load text catalog ...
    if ( !$MainObject->Require($LanguageFile) ) {
        $LogObject->Log(
            Priority => 'error',
            Message  => "Sorry, can't locate or load $LanguageFile "
                . "translation! Check the Kernel/Language/$Self->{UserLanguage}.pm (perl -cw)!",
        );
    }
    else {
        push @{ $Self->{LanguageFiles} }, "$Self->{Home}/Kernel/Language/$Self->{UserLanguage}.pm";
    }

    my $LanguageFileDataMethod = $LanguageFile->can('Data');

    # Execute translation map by calling language file data method via reference.
    if ($LanguageFileDataMethod) {
        if ( $LanguageFileDataMethod->($Self) ) {

            # Debug info.
            if ( $Self->{Debug} > 0 ) {
                $LogObject->Log(
                    Priority => 'debug',
                    Message  => "Kernel::Language::$Self->{UserLanguage} load ... done.",
                );
            }
        }
    }
    else {
        $LogObject->Log(
            Priority => 'error',
            Message  => "Sorry, can't load $LanguageFile! Check if it provides Data method",
        );
    }

    # load action text catalog ...
    my $CustomTranslationModule = '';
    my $CustomTranslationFile   = '';

    # do not include addition translation files, a new translation file gets created
    if ( !$Param{TranslationFile} ) {

        # looking to addition translation files
        my @Files = $MainObject->DirectoryRead(
            Directory => $Self->{Home} . "Kernel/Language/",
            Filter    => "$Self->{UserLanguage}_*.pm",
        );
        FILE:
        for my $File (@Files) {

            # get module name based on file name
            my $Module = $File =~ s/^$Self->{Home}(.*)\.pm$/$1/rg;
            $Module =~ s/\/\//\//g;
            $Module =~ s/\//::/g;

            # Do we have a toplevel language without country code?
            if ( length $Self->{UserLanguage} == 2 ) {

                # Ignore sub-language translation files like (en_GB, en_CA, ...).
                #
                # This will not work for sr_Cyrl and sr_Latn, but in this case there is no "parent"
                #   language where this could be problematic.
                next FILE if $Module =~ /^Kernel::Language::[a-z]{2}_[A-Z]{2}$/;    # en_GB
                next FILE if $Module =~ /^Kernel::Language::[a-z]{2}_[A-Z]{2}_/;    # en_GB_ITSM*
            }

            # Remember custom files to load at the end.
            if ( $Module =~ /_Custom$/ ) {
                $CustomTranslationModule = $Module;
                $CustomTranslationFile   = $File;
                next FILE;
            }

            # load translation module
            if ( !$MainObject->Require($Module) ) {
                $LogObject->Log(
                    Priority => 'error',
                    Message  => "Sorry, can't load $Module! Check the $File (perl -cw)!",
                );
                next FILE;
            }
            else {
                push @{ $Self->{LanguageFiles} }, $File;
            }

            my $ModuleDataMethod = $Module->can('Data');

            if ( !$ModuleDataMethod ) {
                $LogObject->Log(
                    Priority => 'error',
                    Message  => "Sorry, can't load $Module! Check if it provides Data method.",
                );
                next FILE;
            }

            # Execute translation map by calling module data method via reference.
            if ( eval { $ModuleDataMethod->($Self) } ) {

                # debug info
                if ( $Self->{Debug} > 0 ) {
                    $LogObject->Log(
                        Priority => 'debug',
                        Message  => "$Module load ... done.",
                    );
                }
            }
        }

        # load custom text catalog ...
        if ( $CustomTranslationModule && $MainObject->Require($CustomTranslationModule) ) {

            push @{ $Self->{LanguageFiles} }, $CustomTranslationFile;

            my $CustomTranslationDataMethod = $CustomTranslationModule->can('Data');

            # Execute translation map by calling custom module data method via reference.
            if ($CustomTranslationDataMethod) {
                if ( eval { $CustomTranslationDataMethod->($Self) } ) {

                    # Debug info.
                    if ( $Self->{Debug} > 0 ) {
                        $LogObject->Log(
                            Priority => 'Debug',
                            Message  => "$CustomTranslationModule load ... done.",
                        );
                    }
                }
            }
            else {
                $LogObject->Log(
                    Priority => 'error',
                    Message  => "Sorry, can't load $CustomTranslationModule! Check if it provides Data method.",
                );
            }
        }
    }

    # if no return charset is given, use recommended return charset
    if ( !$Self->{ReturnCharset} ) {
        $Self->{ReturnCharset} = $Self->GetRecommendedCharset();
    }

    # get source file charset
    # what charset should I use (take it from translation file)!
    if ( $Self->{Charset} && ref $Self->{Charset} eq 'ARRAY' ) {
        $Self->{TranslationCharset} = $Self->{Charset}->[-1];
    }

    return $Self;
}

=head2 Translatable()

this is a no-op to mark a text as translatable in the Perl code.

Example:

    my $Selection = $LayoutObject BuildSelection (
        Data => {
            'and' => Translatable('and'),
            'or'  => Translatable('or'),
            'xor' => Translatable('xor'),
        },
        Name        => "ConditionLinking[_INDEX_]",
        Sort        => 'AlphanumericKey',
        Translation => 1,
        Class       => 'Modernize W50pc',
    );

=cut

sub Translatable {
    return shift;
}

=head2 Translate()

translate a text with placeholders.

    my $Text = $LanguageObject->Translate('Hello %s!', 'world');

=cut

sub Translate {
    my ( $Self, $Text, @Parameters ) = @_;

    $Text //= '';

    $Text = $Self->{Translation}->{$Text} || $Text;

    return $Text if !@Parameters;

    for my $Count ( 0 .. $#Parameters ) {
        return $Text if !defined $Parameters[$Count];
        $Text =~ s/\%(s|d)/$Parameters[$Count]/;
    }

    return $Text;
}

=head2 FormatTimeString()

formats a timestamp according to the specified date format for the current
language (locale).

    my $Date = $LanguageObject->FormatTimeString(
        '2009-12-12 12:12:12',  # timestamp
        'DateFormat',           # which date format to use, e. g. DateFormatLong
        0,                      # optional, hides the seconds from the time output
    );

Please note that the TimeZone will not be applied in the case of DateFormatShort (date only)
to avoid switching to another date.

If you only pass an ISO date ('2009-12-12'), it will be returned unchanged.
Invalid strings will also be returned with an error logged.

=cut

sub FormatTimeString {
    my ( $Self, $String, $Config, $Short ) = @_;

    my $LogObject = $Kernel::OM->Get('Kernel::System::Log');

    return '' if !$String;

    $Config ||= 'DateFormat';
    $Short  ||= 0;

    # Valid timestamp
    if ( $String =~ /(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/ ) {
        my $ReturnString = $Self->{$Config} || "$Config needs to be translated!";

        my $DateTimeObject = $Kernel::OM->Create(
            'Kernel::System::DateTime',
            ObjectParams => {
                String => $String,
            },
        );

        if ( !$DateTimeObject ) {
            $LogObject->Log(
                Priority => 'error',
                Message  => "Invalid date/time string $String.",
            );

            return $String;
        }

        # Convert to time zone, but only if we actually display the time!
        # Otherwise the date might be off by one day because of the TimeZone diff.
        if ( $Self->{TimeZone} && $Config ne 'DateFormatShort' ) {
            $DateTimeObject->ToTimeZone( TimeZone => $Self->{TimeZone} );
        }

        my $DateTimeValues = $DateTimeObject->Get();

        my $Year      = $DateTimeValues->{Year};
        my $Month     = sprintf "%02d", $DateTimeValues->{Month};
        my $MonthAbbr = $DateTimeValues->{MonthAbbr};
        my $Day       = sprintf "%02d", $DateTimeValues->{Day};
        my $DayAbbr   = $DateTimeValues->{DayAbbr};
        my $Hour      = sprintf "%02d", $DateTimeValues->{Hour};
        my $Minute    = sprintf "%02d", $DateTimeValues->{Minute};
        my $Second    = sprintf "%02d", $DateTimeValues->{Second};

        if ($Short) {
            $ReturnString =~ s/\%T/$Hour:$Minute/g;
        }
        else {
            $ReturnString =~ s/\%T/$Hour:$Minute:$Second/g;
        }
        $ReturnString =~ s/\%D/$Day/g;
        $ReturnString =~ s/\%M/$Month/g;
        $ReturnString =~ s/\%Y/$Year/g;

        $ReturnString =~ s{(\%A)}{$Self->Translate($DayAbbr);}egx;
        $ReturnString
            =~ s{(\%B)}{$Self->Translate($MonthAbbr);}egx;

        # output time zone only if it differs from OTRS' time zone
        if (
            $Config ne 'DateFormatShort'
            && $Self->{TimeZone}
            && $Self->{TimeZone} ne Kernel::System::DateTime->OTRSTimeZoneGet()
            )
        {
            return $ReturnString . " ($Self->{TimeZone})";
        }

        return $ReturnString;
    }

    # Invalid string passed? (don't log for ISO dates)
    if ( $String !~ /^(\d{2}:\d{2}:\d{2})$/ ) {
        $LogObject->Log(
            Priority => 'notice',
            Message  => "No FormatTimeString() translation found for '$String' string!",
        );
    }

    return $String;

}

=head2 GetRecommendedCharset()

DEPRECATED. Don't use this function any more, 'utf-8' is always the internal charset.

Returns the recommended charset for frontend (based on translation
file or utf-8).

    my $Charset = $LanguageObject->GetRecommendedCharset();

=cut

sub GetRecommendedCharset {
    my $Self = shift;

    return 'utf-8';
}

=head2 GetPossibleCharsets()

Returns an array of possible charsets (based on translation file).

    my @Charsets = $LanguageObject->GetPossibleCharsets();

=cut

sub GetPossibleCharsets {
    my $Self = shift;

    return @{ $Self->{Charset} } if $Self->{Charset};
    return;
}

=head2 Time()

Returns a time string in language format (based on translation file).

    $Time = $LanguageObject->Time(
        Action => 'GET',
        Format => 'DateFormat',
    );

    $TimeLong = $LanguageObject->Time(
        Action => 'GET',
        Format => 'DateFormatLong',
    );

    $TimeLong = $LanguageObject->Time(
        Action => 'RETURN',
        Format => 'DateFormatLong',
        Year   => 1977,
        Month  => 10,
        Day    => 27,
        Hour   => 20,
        Minute => 10,
        Second => 05,
    );

These tags are supported: %A=WeekDay;%B=LongMonth;%T=Time;%D=Day;%M=Month;%Y=Year;

Note that %A only works correctly with Action GET, it might be dropped otherwise.

Also note that it is also possible to pass HTML strings for date input:

    $TimeLong = $LanguageObject->Time(
        Action => 'RETURN',
        Format => 'DateInputFormatLong',
        Mode   => 'NotNumeric',
        Year   => '<input value="2014"/>',
        Month  => '<input value="1"/>',
        Day    => '<input value="10"/>',
        Hour   => '<input value="11"/>',
        Minute => '<input value="12"/>',
        Second => '<input value="13"/>',
    );

Note that %B may not work in NonNumeric mode.

=cut

sub Time {
    my ( $Self, %Param ) = @_;

    # check needed stuff
    for my $Needed (qw(Action Format)) {
        if ( !$Param{$Needed} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Need $Needed!",
            );
            return;
        }
    }
    my $ReturnString = $Self->{ $Param{Format} } || 'Need to be translated!';
    my ( $Year, $Month, $MonthAbbr, $Day, $DayAbbr, $Hour, $Minute, $Second );

    # set or get time
    if ( lc $Param{Action} eq 'get' ) {

        my $DateTimeObject = $Kernel::OM->Create(
            'Kernel::System::DateTime',
            ObjectParams => {
                TimeZone => $Self->{TimeZone},
            },
        );
        my $DateTimeValues = $DateTimeObject->Get();

        $Year      = $DateTimeValues->{Year};
        $Month     = sprintf "%02d", $DateTimeValues->{Month};
        $MonthAbbr = $DateTimeValues->{MonthAbbr};
        $Day       = sprintf "%02d", $DateTimeValues->{Day};
        $DayAbbr   = $DateTimeValues->{DayAbbr};
        $Hour      = sprintf "%02d", $DateTimeValues->{Hour};
        $Minute    = sprintf "%02d", $DateTimeValues->{Minute};
        $Second    = sprintf "%02d", $DateTimeValues->{Second};
    }
    elsif ( lc $Param{Action} eq 'return' ) {
        $Year   = $Param{Year}   || 0;
        $Month  = $Param{Month}  || 0;
        $Day    = $Param{Day}    || 0;
        $Hour   = $Param{Hour}   || 0;
        $Minute = $Param{Minute} || 0;
        $Second = $Param{Second} || 0;

        my @MonthAbbrs = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
        $MonthAbbr = defined $Month && $Month =~ m/^\d+$/ ? $MonthAbbrs[ $Month - 1 ] : '';
    }

    # do replace
    if ( ( lc $Param{Action} eq 'get' ) || ( lc $Param{Action} eq 'return' ) ) {
        my $Time = '';
        if ( $Param{Mode} && $Param{Mode} =~ /^NotNumeric$/i ) {
            if ( !$Second ) {
                $Time = "$Hour:$Minute";
            }
            else {
                $Time = "$Hour:$Minute:$Second";
            }
        }
        else {
            $Time  = sprintf( "%02d:%02d:%02d", $Hour, $Minute, $Second );
            $Day   = sprintf( "%02d",           $Day );
            $Month = sprintf( "%02d",           $Month );
        }
        $ReturnString =~ s/\%T/$Time/g;
        $ReturnString =~ s/\%D/$Day/g;
        $ReturnString =~ s/\%M/$Month/g;
        $ReturnString =~ s/\%Y/$Year/g;
        $ReturnString =~ s{(\%A)}{defined $DayAbbr ? $Self->Translate($DayAbbr) : '';}egx;
        $ReturnString
            =~ s{(\%B)}{defined $MonthAbbr ? $Self->Translate($MonthAbbr) : '';}egx;
        return $ReturnString;
    }

    return $ReturnString;
}

=head2 LanguageChecksum()

This function returns an MD5 sum that is generated from all loaded language files and their modification timestamps.
Whenever a file is changed, added or removed, this checksum will change.

=cut

sub LanguageChecksum {
    my $Self = shift;

    # Create a string with filenames and file modification times of the loaded language files.
    my $LanguageString = '';
    for my $File ( @{ $Self->{LanguageFiles} } ) {

        # get file metadata
        my $Stat = stat($File);

        if ( !$Stat ) {
            print STDERR "Error: cannot stat file '$File': $!";
            return;
        }

        $LanguageString .= $File . $Stat->mtime();
    }

    return Digest::MD5::md5_hex($LanguageString);
}

=head2 GetTTTemplateTranslatableStrings()

Returns an array of translation strings from tt templates.

    my @TranslationStrings = $LanguageObject->GetTTTemplateTranslatableStrings(
        ModuleDirectory => "$Home/...",  # optional, translates the Znuny module in the given directory
    );

Returns:

    my @TranslationStrings = (
        {
            Location => "TT Template: Kernel/Output/HTML/Templates/Standard/AdminACL.tt",
            Source   => 'Actions',
        }
    );

=cut

sub GetTTTemplateTranslatableStrings {
    my ( $Self, %Param ) = @_;

    my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

    my @TranslationStrings;
    $Param{ModuleDirectory} ||= '';

    my $TemplatesDirectory = $Param{ModuleDirectory}
        ? "$Param{ModuleDirectory}/Kernel/Output/HTML/Templates/$Self->{DefaultTheme}"
        : "$Self->{Home}/Kernel/Output/HTML/Templates/$Self->{DefaultTheme}";

    my @TemplateList;
    if ( -d $TemplatesDirectory ) {
        @TemplateList = $MainObject->DirectoryRead(
            Directory => $TemplatesDirectory,
            Filter    => '*.tt',
            Recursive => 1,
        );
    }

    my $CustomTemplatesDir = "$Param{ModuleDirectory}/Custom/Kernel/Output/HTML/Templates/$Self->{DefaultTheme}";
    if ( $Param{ModuleDirectory} && -d $CustomTemplatesDir ) {
        my @CustomTemplateList = $MainObject->DirectoryRead(
            Directory => $CustomTemplatesDir,
            Filter    => '*.tt',
            Recursive => 1,
        );
        push @TemplateList, @CustomTemplateList;
    }

    for my $File (@TemplateList) {

        my $ContentRef = $MainObject->FileRead(
            Location => $File,
            Mode     => 'utf8',
        );

        if ( !ref $ContentRef ) {
            die "Can't open $File: $!";
        }

        my $Content = ${$ContentRef};

        $File =~ s{^.*/(Kernel/.+?)}{$1}smx;

        # do translation
        $Content =~ s{
            Translate\(
                \s*
                (["'])(.*?)(?<!\\)\1
        }
        {
            my $Word = $2 // '';

            # unescape any \" or \' signs
            $Word =~ s{\\"}{"}smxg;
            $Word =~ s{\\'}{'}smxg;

            if ( $Word && !$Self->{UsedWords}->{$Word}++ ) {

                push @TranslationStrings, {
                    Location => "TT Template: $File",
                    Source   => $Word,
                };
            }

            '';
        }egx;
    }

    return @TranslationStrings;
}

=head2 GetJSTemplateTranslatableStrings()

Returns an array of translation strings from JS templates.

    my @TranslationStrings = $LanguageObject->GetJSTemplateTranslatableStrings(
        ModuleDirectory  => "$Home/...",  # optional, translates the Znuny module in the given directory
    );

Returns:

    my @TranslationStrings = (
        {
            Location => "JS Template: Kernel/Output/JavaScript/Templates/Standard/Agent/TicketZoom/FormDraftDeleteDialog.html.tmpl",
            Source   => 'Cancel',
        }
    );

=cut

sub GetJSTemplateTranslatableStrings {
    my ( $Self, %Param ) = @_;

    my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

    my @TranslationStrings;
    $Param{ModuleDirectory} ||= '';

    # Add strings from .html.tmpl files (JavaScript templates).
    my $JSTemplatesDirectory = $Param{ModuleDirectory}
        ? "$Param{ModuleDirectory}/Kernel/Output/JavaScript/Templates/$Self->{DefaultTheme}"
        : "$Self->{Home}/Kernel/Output/JavaScript/Templates/$Self->{DefaultTheme}";

    my @JSTemplateList;
    if ( -d $JSTemplatesDirectory ) {
        @JSTemplateList = $MainObject->DirectoryRead(
            Directory => $JSTemplatesDirectory,
            Filter    => '*.html.tmpl',
            Recursive => 1,
        );
    }

    my $CustomJSTemplatesDir
        = "$Param{ModuleDirectory}/Custom/Kernel/Output/JavaScript/Templates/$Self->{DefaultTheme}";
    if ( $Param{ModuleDirectory} && -d $CustomJSTemplatesDir ) {
        my @CustomJSTemplateList = $MainObject->DirectoryRead(
            Directory => $CustomJSTemplatesDir,
            Filter    => '*.html.tmpl',
            Recursive => 1,
        );
        push @JSTemplateList, @CustomJSTemplateList;
    }

    for my $File (@JSTemplateList) {

        my $ContentRef = $MainObject->FileRead(
            Location => $File,
            Mode     => 'utf8',
        );

        if ( !ref $ContentRef ) {
            die "Can't open $File: $!";
        }

        my $Content = ${$ContentRef};

        $File =~ s{^.*/(Kernel/.+?)}{$1}smx;

        # Find strings marked for translation.
        $Content =~ s{
            \{\{
            \s*
            (["'])(.*?)(?<!\\)\1
            \s*
            \|
            \s*
            Translate
        }
        {
            my $Word = $2 // '';

            # Unescape any \" or \' signs.
            $Word =~ s{\\"}{"}smxg;
            $Word =~ s{\\'}{'}smxg;

            if ( $Word && !$Self->{UsedWords}->{$Word}++ ) {
                push @TranslationStrings, {
                    Location => "JS Template: $File",
                    Source   => $Word,
                };
            }

            # Also save that this string was used in JS (for later use in Loader).
            $Self->{UsedInJS}->{$Word} = 1;

            '';
        }egx;
    }

    return @TranslationStrings;
}

=head2 GetPerlModuleTranslatableStrings()

Returns an array of translation strings from Perl modules mark with Translatable or Translate.

    my @TranslationStrings = $LanguageObject->GetPerlModuleTranslatableStrings(
        ModuleDirectory  => "$Home/...",  # optional, translates the Znuny module in the given directory
    );

Returns:

    my @TranslationStrings = (
        {
            Location => "Perl Module: Kernel/Modules/AdminACL.pm",
            Source   => 'This field is required',
        }
    );

=cut

sub GetPerlModuleTranslatableStrings {
    my ( $Self, %Param ) = @_;

    my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

    my @TranslationStrings;
    $Param{ModuleDirectory} ||= '';

    # add translatable strings from Perl code
    my $PerlModuleDirectory = $Param{ModuleDirectory}
        ? "$Param{ModuleDirectory}/Kernel"
        : "$Self->{Home}/Kernel";

    my @PerlModuleList;
    if ( -d $PerlModuleDirectory ) {
        @PerlModuleList = $MainObject->DirectoryRead(
            Directory => $PerlModuleDirectory,
            Filter    => '*.pm',
            Recursive => 1,
        );
    }

    # include Custom folder for modules
    my $CustomKernelDir = "$Param{ModuleDirectory}/Custom/Kernel";
    if ( $Param{ModuleDirectory} && -d $CustomKernelDir ) {
        my @CustomPerlModuleList = $MainObject->DirectoryRead(
            Directory => $CustomKernelDir,
            Filter    => '*.pm',
            Recursive => 1,
        );
        push @PerlModuleList, @CustomPerlModuleList;
    }

    # Include some additional folders for modules.
    for my $AdditionalFolder ( 'var/packagesetup', 'var/processes/examples', 'var/webservices/examples' ) {
        if ( $Param{ModuleDirectory} && -d "$Param{ModuleDirectory}/$AdditionalFolder" ) {
            my @PackageSetupModuleList = $MainObject->DirectoryRead(
                Directory => "$Param{ModuleDirectory}/$AdditionalFolder",
                Filter    => '*.pm',
                Recursive => 1,
            );
            push @PerlModuleList, @PackageSetupModuleList;
        }
    }

    FILE:
    for my $File (@PerlModuleList) {

        next FILE if ( $File =~ m{cpan-lib}xms );
        next FILE if ( $File =~ m{Kernel/Config/Files}xms );

        my $ContentRef = $MainObject->FileRead(
            Location => $File,
            Mode     => 'utf8',
        );

        if ( !ref $ContentRef ) {
            die "Can't open $File: $!";
        }

        $File =~ s{^.*/(Kernel/)}{$1}smx;
        $File =~ s{^.*/(var/)}{$1}smx;

        my $Content = ${$ContentRef};

        # Remove POD
        my $PodStrip = Pod::Strip->new();
        $PodStrip->replace_with_comments(1);
        my $Code;
        $PodStrip->output_string( \$Code );
        $PodStrip->parse_string_document($Content);

        # Purge all comments
        $Code =~ s{^ \s* # .*? \n}{\n}xmsg;

        # do translation
        $Code =~ s{
            (?:
                ->Translate | Translatable
            )
            \(
                \s*
                (["'])(.*?)(?<!\\)\1
        }
        {
            my $Word = $2 // '';

            # unescape any \" or \' signs
            $Word =~ s{\\"}{"}smxg;
            $Word =~ s{\\'}{'}smxg;

            # Ignore strings containing variables
            my $SkipWord;
            $SkipWord = 1 if $Word =~ m{\$}xms;

            if ( $Word && !$SkipWord && !$Self->{UsedWords}->{$Word}++ ) {

                push @TranslationStrings, {
                    Location => "Perl Module: $File",
                    Source => $Word,
                };

            }
            '';
        }egx;
    }

    return @TranslationStrings;
}

=head2 GetXMLTranslatableStrings()

Returns an array of translation strings from JS templates.

    my @TranslationStrings = $LanguageObject->GetXMLTranslatableStrings(
        ModuleDirectory  => "$Home/...",  # optional, translates the Znuny module in the given directory
    );

Returns:

    my @TranslationStrings = (
        {
            Location => "XML Definition:  scripts/database/initial_insert.xml",
            Source   => 'This field is required',
        }
    );

=cut

sub GetXMLTranslatableStrings {
    my ( $Self, %Param ) = @_;

    my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

    my @TranslationStrings;
    $Param{ModuleDirectory} ||= '';

    # add translatable strings from DB XML
    my @XMLFiles = $MainObject->DirectoryRead(
        Directory => "Kernel/Config/Files/XML",
        Recursive => 1,
        Filter    => '*.xml',
    );

    push @XMLFiles, "scripts/database/initial_insert.xml";

    if ( $Param{ModuleDirectory} ) {
        @XMLFiles = $MainObject->DirectoryRead(
            Directory => "$Param{ModuleDirectory}",
            Recursive => 1,
            Filter    => '*.sopm',
        );
    }

    FILE:
    for my $File (@XMLFiles) {

        my $ContentRef = $MainObject->FileRead(
            Location => $File,
            Mode     => 'utf8',
        );

        if ( !ref $ContentRef ) {
            die "Can't open $File: $!";
        }

        $File =~ s{^.*/(scripts/)}{$1}smx;
        $File =~ s{//}{/}g;
        if ( $Param{ModuleDirectory} ) {
            $File =~ s{^.*/(.+\.sopm)}{$1}smx;
        }

        my $Content = ${$ContentRef};
        next FILE if !$Content;

        # do translation
        $Content =~ s{
            <(Data|Description)[^>]+Translatable="1"[^>]*>(.*?)</\1>
        }
        {
            my $Word = $2 // '';
            if ( $Word && !$Self->{UsedWords}->{$Word}++ ) {
                push @TranslationStrings, {
                    Location => "XML Definition: $File",
                    Source   => $Word,
                };
            }
            '';
        }egx;
    }

    return @TranslationStrings;
}

=head2 GetJSTranslatableStrings()

Returns an array of translation strings from JS.

    my @TranslationStrings = $LanguageObject->GetJSTranslatableStrings(
        ModuleDirectory  => "$Home/...",  # optional, translates the Znuny module in the given directory
    );

Returns:

    my @TranslationStrings = (
        {
            Location => "JS File: var/httpd/htdocs/js/Core.Agent.Admin.ACL",
            Source   => 'Add all',
        }
    );

=cut

sub GetJSTranslatableStrings {
    my ( $Self, %Param ) = @_;

    my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

    my @TranslationStrings;
    $Param{ModuleDirectory} ||= '';

    # add translatable strings from JavaScript code
    my $JSDirectory = $Param{ModuleDirectory}
        ? "$Param{ModuleDirectory}/var/httpd/htdocs/js"
        : "$Self->{Home}/var/httpd/htdocs/js";

    my @JSFileList;
    if ( -d $JSDirectory ) {
        @JSFileList = $MainObject->DirectoryRead(
            Directory => $JSDirectory,
            Filter    => '*.js',
            Recursive => 1,
        );
    }

    FILE:
    for my $File (@JSFileList) {

        my $ContentRef = $MainObject->FileRead(
            Location => $File,
            Mode     => 'utf8',
        );

        if ( !ref $ContentRef ) {
            die "Can't open $File: $!";
        }

        # skip js cache files
        next FILE if ( $File =~ m{\/js\/js-cache\/}xmsg );

        my $Content = ${$ContentRef};

        # skip third-party files without custom markers
        if ( $File =~ m{\/js\/thirdparty\/}xmsg ) {
            next FILE if ( $Content !~ m{\/\/\s*OTRS}xmsg );
        }

        $File =~ s{^.*\/(var/httpd/htdocs/js.+?)}{$1}smx;

        # Purge all comments
        $Content =~ s{^ \s* // .*? \n}{\n}xmsg;

        # do translation
        $Content =~ s{
            (?:
                Core.Language.Translate
            )
            \(
                \s*
                (["'])(.*?)(?<!\\)\1
        }
        {
            my $Word = $2 // '';

            # unescape any \" or \' signs
            $Word =~ s{\\"}{"}smxg;
            $Word =~ s{\\'}{'}smxg;

            if ( $Word && !$Self->{UsedWords}->{$Word}++ ) {

                push @TranslationStrings, {
                    Location => "JS File: $File",
                    Source   => $Word,
                };

            }

            # also save that this string was used in JS (for later use in Loader)
            $Self->{UsedInJS}->{$Word} = 1;

            '';
        }egx;
    }

    return @TranslationStrings;
}

=head2 GetSysConfigTranslatableStrings()

Returns an array of translation strings from SysConfig.

    my @TranslationStrings = $LanguageObject->GetSysConfigTranslatableStrings();

Returns:

    my @TranslationStrings = (
        {
            Location => "SysConfig",
            Source   => 'Add all',
        }
    );

=cut

sub GetSysConfigTranslatableStrings {
    my ( $Self, %Param ) = @_;

    my @Strings = $Kernel::OM->Get('Kernel::System::SysConfig')->ConfigurationTranslatableStrings();
    my @TranslationStrings;

    STRING:
    for my $String ( sort @Strings ) {

        next STRING if !$String || $Self->{UsedWords}->{$String}++;

        push @TranslationStrings, {
            Location => 'SysConfig',
            Source   => $String,
        };
    }

    return @TranslationStrings;
}

1;

=head1 TERMS AND CONDITIONS

This software is part of the OTRS project (L<https://otrs.org/>).

This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.

=cut