#!/usr/bin/perl our $outputPath = "/data/WebGUI/lib/WebGUI/i18n"; our $languageId = "en"; our $webguiPath = "/data/WebGUI"; our $editor_lang = "en"; our $extras_url = "/extras"; our $notest_no_UTF = "Charset of Your translation is not UTF-8. You must to convert it before to use this program."; #----no need to edit below this line ------------- use strict; use CGI qw (:standart); use CGI::Carp qw (fatalsToBrowser); use URI::Escape; #-----main---------------- $|=1; # disable output buffer our $cgi = CGI->new; our $editor_on; my $lang = getLanguage(); my $lang_charset = $lang->{charset} || "UTF-8"; if ($cgi->param("is_editor_on") ne "") { our $editor_cookie; $editor_cookie = $cgi->cookie(-name=>'visual_editor_on', -expires=>'+48h', -value=>[$cgi->param("is_editor_on")]); print $cgi->header( -cookie=>$editor_cookie, -expires=>'-1d', -charset=>$lang_charset ); $editor_on = $cgi->param("is_editor_on"); } else { print $cgi->header( -charset=>$lang_charset ); $editor_on = $cgi->cookie('visual_editor_on'); } if ($cgi->param("op") ne "") { print header($lang); if ($cgi->param("op") =~ /^[[:alpha:]]+$/) { my $cmd = "&www_".$cgi->param("op"); print eval($cmd); } else { print "

Stop Screwing Around

"; } print footer(); } else { print buildSiteFrames(); } #-----end main------------ sub buildSiteFrames { my $output = ' WebGUI Internationalization Editor '; return $output; } sub buildURL { my $op = shift; my $params = shift; my $url = $ENV{SCRIPT_NAME}.'?op='.$op; foreach my $param (keys %{$params}) { $url .= '&'.$param.'='.uri_escape($params->{$param}); } return $url; } sub fixFormData { my $value = shift; $value =~ s/\"/\"\;/g; $value =~ s/\&/\&\;/g; $value =~ s/\/\>\;/g; return $value; } sub footer { return ''; } sub getLanguage { my $load = $outputPath.'/'.$languageId.'.pm'; eval {require $load}; if ($@) { writeLanguage(); return getLanguage(); } else { my $cmd = "\$WebGUI::i18n::".$languageId."::LANGUAGE"; return eval ($cmd); } } sub getNamespaceItems { my $namespace = shift; my $languageId = shift || "English"; my $inLoop = shift; my $load; if ($languageId eq "English") { $load = $webguiPath.'/lib/WebGUI/i18n/English/'.$namespace.'.pm'; } else { $load = $outputPath.'/'.$languageId.'/'.$namespace.'.pm'; } eval {require $load}; if ($@ && !$inLoop) { writeNamespace($namespace); return getNamespaceItems($namespace,$languageId, 1); } else { my $cmd = "\$WebGUI::i18n::".$languageId."::".$namespace."::I18N"; return eval($cmd); } } sub getNamespaces { opendir (DIR,$webguiPath.'/lib/WebGUI/i18n/English/'); my @files = readdir(DIR); closedir(DIR); @files = sort @files; my @namespaces; foreach my $file (@files) { if ($file =~ /(.*?)\.pm$/) { push(@namespaces,$1); } } return \@namespaces; } sub header { my $lang = shift; my $editor_page; $editor_page = "\r\n"; $editor_page .= "\r\n"; $editor_page .= qq(\r\n); $editor_page .= qq(\r\n); $editor_page .= qq(\r\n); $editor_page .= qq(\r\n); $editor_page .= "\r\n"; if (!$editor_on == 1) { $editor_page .= "\r\n"; $editor_page .= ""; $editor_page .= "\r\n"; $editor_page .= "\r\n"; } $editor_page .= ""; return $editor_page; } sub preview { my $text = shift || "not yet defined"; $text = substr($text,0,50); $text =~ s/&/&/g; $text =~ s/\/>/g; return $text; } sub setLanguage { my $label = shift; my $toolbar = shift; my $charset = shift; my $output = "\tlabel => '".$label."',\n"; # $output .= "\tcharset => '".$charset."',\n"; $output .= "\ttoolbar => '".$toolbar."'\n"; writeLanguage($output); } sub setNamespaceItems { my $namespace = shift; my $tag = shift; my $message = shift; my $eng = getNamespaceItems($namespace); my $lang = getNamespaceItems($namespace,$languageId); $lang->{$tag}{message} = $message; $lang->{$tag}{message} =~ s/\|/\\\|/g; $lang->{$tag}{lastUpdated} = time(); my $output; foreach my $tag (keys %{$eng}) { $output .= "\t'".$tag."' => {\n"; $output .= "\t\tmessage => ".'q|'.$lang->{$tag}{message}."|,\n"; $output .= "\t\tlastUpdated => ".$lang->{$tag}{lastUpdated}; $output .= "\n\t},\n\n"; } writeNamespace($namespace,$output); } sub writeFile { my $filepath = shift; my $content = shift; my $mkdir = substr($filepath,1,(length($filepath)-1)); my @path = split("\/",$mkdir); $mkdir = ""; foreach my $part (@path) { next if ($part =~ /\.pm/); $mkdir .= "/".$part; mkdir($mkdir); } if (open(FILE,">".$filepath)) { print FILE $content; close(FILE); } else { print "ERROR writing file ".$filepath." because ".$1.".\n"; exit; } } sub writeLanguage { my $data = shift; my $output = "package WebGUI::i18n::".$languageId.";\n\n"; $output .= "use strict;\n\n"; $output .= "our \$LANGUAGE = {\n"; $output .= $data; $output .= "};\n\n"; my $translit_replaces_r = $cgi->param("translit_replaces"); $translit_replaces_r =~ s/\r//g; # For ***nix OS $output .= qq(sub makeUrlCompliant { my \$value = shift;\n); $output .= "##<-- start transliteration -->## ".$translit_replaces_r." ##<-- end transliteration -->##\n"; $output .= qq( \$value =~ s/\\s+\$//; #removes trailing whitespace \$value =~ s/^\\s+//; #removes leading whitespace \$value =~ s/^\\\\//; #removes leading slash \$value =~ s/ /-/g; #replaces whitespace with underscores \$value =~ s/\\.\$//; #removes trailing period \$value =~ s/[^A-Za-z0-9\\-\\.\\_\\/]//g; #removes all funky characters \$value =~ s/^\\///; #removes a preceeding / \$value =~ s/\\/\\//\\//g; #removes double / return \$value; }); $output .= "\n\n1;\n"; writeFile($outputPath.'/'.$languageId.'.pm', $output); ##if (!($lang_charset =~ /UTF-8/s)) {ConvertToUTF (); return '';} if (!($lang_charset =~ /UTF-8/s)) {ConvertToUTF (); $lang_charset = "UTF-8";} } sub writeNamespace { my $namespace = shift; my $data = shift; my $output = "package WebGUI::i18n::".$languageId."::".$namespace.";\n\n"; $output .= "our \$I18N = {\n"; $output .= $data; $output .= "};\n\n1;\n"; writeFile($outputPath.'/'.$languageId.'/'.$namespace.'.pm', $output); } sub www_displayMenu { my $output = 'HOME

'.$languageId.'

'; my $namespaces = getNamespaces(); foreach my $namespace (@{$namespaces}) { $output .= '$namespace}).'" target="editor">'.$namespace.'
'; } return $output; } sub www_editItem { my $eng = getNamespaceItems($cgi->param("namespace")); my $lang = getNamespaceItems($cgi->param("namespace"),$languageId); my $output = ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= '' if ($eng->{$cgi->param("tag")}{context}); $output .= qq(
Visual editorparam("namespace").'
Tag'.$cgi->param("tag").'
Message
Original Message'.$eng->{$cgi->param("tag")}{message}.'
Message Context Info'.$eng->{$cgi->param("tag")}{context}.'
); return $output; } sub www_editItemSave { setNamespaceItems($cgi->param("namespace"),$cgi->param("tag"),$cgi->param("message")); return "Message saved.

".www_listItemsInNamespace(); } sub www_editLanguage { my $lang = getLanguage(); my $output = '

'; $output .= ''; if (!($lang_charset =~ /UTF-8/s)) { $output .= ''; } $output .= ''; # $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= '

!!!

'.$notest_no_UTF.'
Label
Character Set
Character Set  '.$lang_charset.'
Toolbar
Replaces for transliteration

Something like:

'; $output .= ' $value =~ s/Йа/J\\\'a/;
$value =~ s/йа/j\\\'a/;
$value =~ s/ЙА/J\\\'A/;
$value =~ s/Я/Ja/g;
$value =~ s/я/ja/g;

$value =~ s/^\\s+//;
$value =~ s/^\\\\//;
$value =~ s/ /_/g;
$value =~ s/\\.\$//;
$value =~ s/[^A-Za-z0-9\\-\\.\\_\\/]//g;
$value =~ s/^\\///;
$value =~ s/\\/\\//\\//g;
'; $output .= '
'; return $output; } sub www_editLanguageSave { setLanguage($cgi->param("label"), $cgi->param("toolbar"), $cgi->param("translit_replaces")); return "Language saved.

".www_editLanguage(); } sub www_listItemsInNamespace { my $eng = getNamespaceItems($cgi->param("namespace")); my $lang = getNamespaceItems($cgi->param("namespace"),$languageId); my $output = ''; $output .= ''; foreach my $tag (sort keys %{$eng}) { $output .= '{$tag}{lastUpdated} > $lang->{$tag}{lastUpdated}) { $output .= ' class="outOfDate"'; } $output .= '>'; } $output .= '
Namespace'.$cgi->param("namespace").'
$cgi->param("namespace"),tag=>$tag}).'">'.$tag.''; if ($lang->{$tag} ne "") { $output .= preview($lang->{$tag}{message}); } else { $output .= preview($eng->{$tag}{message}); } $output .= '
'; return $output; } sub ReadTranslit { my $translit_replaces_read = ''; open(TRANSLIT,"$outputPath/$languageId.pm") || die "$!\n"; my @translit = ; close(TRANSLIT); my $flag_T = 0; foreach my $translit (@translit) { if ($translit =~ /##<-- end transliteration -->##/) {$flag_T = 0;} if ($flag_T == 1) { $translit_replaces_read .= $translit; } if ($translit =~ /##<-- start transliteration -->##/) {$flag_T = 1;} } return $translit_replaces_read; } sub ConvertToUTF { use Text::Iconv; # Read list of files chdir ("$outputPath/$languageId/"); my @files = <*>; my $files_l; foreach my $file (@files) { open(FILE,"./$file") || die "$!\n"; my @files_l = ; close(FILE); my $lang_char = $lang_charset; $lang_char =~ s/windows-1251/CP1251/g; $lang_char =~ s/windows-1252/CP1252/g; my $converter = Text::Iconv->new("$lang_char", "UTF-8"); @files_l = $converter->convert("@files_l"); open(FILE,">$file") || die "$!\n"; print FILE "@files_l"; close(FILE); $files_l = ''; } }