#!/usr/bin/env raku # This script reads the translation files in tools/templates/L10N and # converts each file found ("xxx") in there (except "CORE") to a source file # in lib/RakuAST/Deparse/L10N/xxx.rakumod to provide deparsing for that # localization, and a source file in lib/L10N/xxx.rakumod to provide # activation of that localization as a slang. # always use highest version of Raku use v6.*; # Get the role generation logic use RakuAST::L10N; my $generator := $*PROGRAM-NAME; my $generated := DateTime.now.gist.subst(/\.\d+/,''); my $start := '#- start of generated part of localization'; my $end := '#- end of generated part of localization'; # For all available localizations for localization-files() -> $io { # Create translation hash my %translation := read-hash($io, :core); # Create the slang and slangification my str $language = $io.basename; my $slang := slangify($language, %translation); my $source := $slang.DEPARSE ~ Q:to/CODE/.subst('#LANGUAGE#',$language,:g); # The EXPORT sub that actually does the slanging my sub EXPORT($dontslang?) { unless $dontslang { my $LANG := $*LANG; $LANG.define_slang('MAIN', $LANG.slang_grammar('MAIN').^mixin(L10N::#LANGUAGE#) ); } BEGIN Map.new } CODE write-file "lib/L10N/$language.rakumod", $source, Q:to/DEFAULT/; # This file contains the ……… Slang of the Raku Programming Language #- start of generated part of localization #- end of generated part of localization # vim: expandtab shiftwidth=4 DEFAULT # Create the role for mixing in deparsing my $deparser := deparsify($language, %translation); write-file "lib/RakuAST/Deparse/L10N/$language.rakumod", $deparser.DEPARSE, Q:to/DEFAULT/; # This file contains the ……… deparsing logic for the Raku # Programming Language. #- start of generated part of localization #- end of generated part of localization # vim: expandtab shiftwidth=4 DEFAULT } sub write-file($filename, Str:D $src, Str:D $default) { my $io := $filename.IO; # slurp the whole file and set up writing to it my @lines = ($io.e ?? $io !! $default).lines; # for all the lines in the source that don't need special handling my $*OUT = $filename.IO.open(:w); while @lines { my $line := @lines.shift; # nothing to do yet unless $line.starts-with($start) { say $line; next; } say "$start ------------------------------------"; say "#- Generated on $generated by $generator"; say "#- PLEASE DON'T CHANGE ANYTHING BELOW THIS LINE"; say ""; # skip the old version of the code while @lines { last if @lines.shift.starts-with($end); } # Insert the actual logic print $src; # we're done for this role say ""; say "#- PLEASE DON'T CHANGE ANYTHING ABOVE THIS LINE"; say "$end --------------------------------------"; } # close the file properly $*OUT.close; } # vim: expandtab sw=4