From 45958240f241bb97a0c7e80934a783977ff1ba16 Mon Sep 17 00:00:00 2001 From: Elijah Perrault Date: Fri, 14 Jan 2011 21:01:44 -0700 Subject: [PATCH] New method of translation. --- bin/auto | 9 +++++++ lang/en.alf | 18 +++++++++++++ src/API/Std.pm | 11 ++++++++ src/Parser/Lang.pm | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 lang/en.alf create mode 100644 src/Parser/Lang.pm diff --git a/bin/auto b/bin/auto index 35272d8..531ca11 100755 --- a/bin/auto +++ b/bin/auto @@ -14,8 +14,10 @@ use Data::Dumper; use FindBin qw($Bin); our $Bin = $Bin; unshift(@INC, "$Bin/../src"); +require API::Std; #require DB::Flatfile; require Parser::Config; +require Parser::Lang; #require Parser::IRC; #require IRC::Standard; #require Sys::Full; @@ -30,3 +32,10 @@ use constant { our $CONF = Parser::Config->new("auto.conf") or print "Failed to open configuration file.\n" and exit; our %SETTINGS = $CONF->parse; + +my @lclv = API::Std::conf_get("locale"); +our $LOCALE = $lclv[0][0]; +my @lang = split('_', $LOCALE); +Parser::Lang::parse($lang[0]) or print ".\n" and exit; +undef @lang; +undef @lclv; diff --git a/lang/en.alf b/lang/en.alf new file mode 100644 index 0000000..293e7d9 --- /dev/null +++ b/lang/en.alf @@ -0,0 +1,18 @@ +# Auto IRC Bot. An advanced, lightweight and powerful IRC bot. +# Copyright (C) 2010-2011 Xelhua Development Team (doc/CREDITS) +# This program is free software; rights to this code are stated in doc/LICENSE. + +# English translation file. + + +# Permission denied - Permission denied +msge "Permission_denied" "Permission denied" + +# Access denied - Access denied +msge "Access_denied" "Access denied" + +# Not enough parameters - Not enough parameters +msge "Not_enough_parameters" "Not enough parameters" + +# Unknown command - Unknown command +msge "Unknown_command" "Unknown command" diff --git a/src/API/Std.pm b/src/API/Std.pm index 3c50e1d..0af33b6 100644 --- a/src/API/Std.pm +++ b/src/API/Std.pm @@ -11,6 +11,8 @@ package API::Std; our @ISA = qw(Exporter); our @EXPORT_OK = qw(conf_get trans); +my %LANGE; + # Configuration value getter. sub conf_get @@ -49,7 +51,16 @@ sub conf_get # Translation subroutine. sub trans { + my ($id) = @_; + $id =~ s/ /_/g; + if (defined $API::Std::LANGE{$id}) { + return $API::Std::LANGE{$id}; + } + else { + $id =~ s/_/ /g; + return $id; + } } 1; diff --git a/src/Parser/Lang.pm b/src/Parser/Lang.pm new file mode 100644 index 0000000..94088d1 --- /dev/null +++ b/src/Parser/Lang.pm @@ -0,0 +1,64 @@ +# Auto IRC Bot. An advanced, lightweight and powerful IRC bot. +# Copyright (C) 2010-2011 Xelhua Development Team (doc/CREDITS) +# This program is free software; rights to this code are stated in doc/LICENSE. +use strict; +use warnings; + +# Language file parser. +package Parser::Lang; + + +# Parser. +sub parse +{ + my ($lang) = @_; + + # Check that the language file exists. + unless (-e "$Auto::Bin/../lang/$lang.alf") { + return 0; + } + + # Open, read and close the file. + open ALF, "<$Auto::Bin/../lang/$lang.alf" or return 0; + my @fbuf = ; + close ALF; + + # Iterate the file buffer. + foreach my $buff (@fbuf) { + if (defined $buff) { + # Space buffer. + my @sbuf = split(' ', $buff); + + # Check for all required values. + if (!defined $sbuf[0] or !defined $sbuf[1] or !defined $sbuf[2]) { + # Missing a value. + next; + } + + # Make sure the first value is "msge". + if ($sbuf[0] ne "msge") { + # It isn't. + next; + } + + my $id = $sbuf[1]; + my $val = $sbuf[2]; + + # If the translation is multi-word, continue to parse. + if (defined $sbuf[3]) { + for (my $i = 3; $i < scalar(@sbuf); $i++) { + $val .= " ".$sbuf[$i]; + } + } + + # Save to memory. + $id =~ s/"//g; + $val =~ s/"//g; + $API::Std::LANGE{$id} = $val; + } + } + return 1; +} + + +1;