在php类中使用和传递语言变量

时间:2016-07-28 13:34:38

标签: php class session variables translation

我需要一些帮助,这是我网站上唯一遗漏的部分。 我有一个时事通讯脚本和第一页上的 我正在使用我从codecanyon(PHPMLC)获得的多语言功能,该功能完美无缺。

这是带有表单的 index.php (我已经删除了不必要的html)。

<?php
require_once( "../PHPMLC/class/PHPMLC.php" );
$ml                        = new PHPMLC();
$ml_languages              = $ml->getLanguages();
$ml_selected_language_code = $ml->getCurrentLanguageCode();
$ml_strings                = $ml->getTranslatedStringsForCurrentLanguage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?php echo $ml_strings['prereg_m_title']; ?></title>
    <link href="/css/style.css" rel="stylesheet">
</head>
<body class="Site">
<form class="form-horizontal" id="newsletter" action="send.php" method="post" accept-charset="utf-8"
      enctype="multipart/form-data">
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-6">
            <input type="email" name="signup-email" class="form-control" id="signup-email" value=""
                   pattern="[A-Z-a-z-0-9-_@.]+" autocomplete="off" required />
            <input type="hidden" name="signup-gender" id="signup-gender" value="female" />
        </div>
    </div>
    <div class="form-group">
        <label for="inputPassword3"
               class="col-sm-2 control-label"><?php echo $ml_strings['prereg_country_label']; ?></label>
        <div class="col-sm-6">
            <select class="form-control" name="signup-country" required />
            <option value="" disabled selected><?php echo $ml_strings['prereg_please_select']; ?></option>
            <option value="Albania">Albania</option>
            <option value="Andorra">Andorra</option>
            <option value="Austria">Austria</option>
            <option value="Belarus">Belarus</option>
            <option value="Belgium">Belgium</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-6">
            <button type="submit" class="btn btn-info"><?php echo $ml_strings['prereg_submit_button']; ?></button>
        </div>
    </div>
</form>
</body>
</html>

表单将输入发送到文件“send.php”,该文件包含以下代码:

<?php
require 'inc/Database.class.php';

class Newsletter {
    private static $email;
    private static $gender;
    private static $country;
    private static $ip;
    private static $datetime = null;

    private static $valid = true;

    public function __construct() {
        die( 'Init function is not allowed' );
    }

    public static function register( $email ) {
        if ( ! empty( $_POST ) ) {
            self::$email   = $_POST['signup-email'];
            self::$gender  = $_POST['signup-gender'];
            self::$country = $_POST['signup-country'];

            //Test if it is a shared client
            if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
                self::$ip = $_SERVER['HTTP_CLIENT_IP'];
                //Is it a proxy address
            } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
                self::$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } else {
                self::$ip = $_SERVER['REMOTE_ADDR'];
            }

            self::$datetime = date( 'Y-m-d H:i:s' );

            if ( empty( self::$email ) ) {
                $status      = "error";
                $message     = "The email address field must not be blank";
                self::$valid = false;
            } else if ( ! filter_var( self::$email, FILTER_VALIDATE_EMAIL ) ) {
                $status      = "error";
                $message     = "You must fill the field with a valid email address";
                self::$valid = false;
            }

            if ( self::$valid ) {
                $pdo = Database::connect();
                $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                $existingSignup = $pdo->prepare( "SELECT COUNT(*) FROM signups WHERE signup_email_address='$email'" );
                $existingSignup->execute();
                $data_exists = ( $existingSignup->fetchColumn() > 0 ) ? true : false;

                if ( ! $data_exists ) {

                    $sql = "INSERT INTO signups (signup_email_address, signup_category, signup_country, signup_ip, signup_date) VALUES (:email, :gender, :country, :ip, :datetime)";
                    $q   = $pdo->prepare( $sql );

                    $q->execute(
                        array( ':email'    => self::$email,
                               ':gender'   => self::$gender,
                               ':country'  => self::$country,
                               ':ip'       => self::$ip,
                               ':datetime' => self::$datetime
                        ) );

                    if ( $q ) {
                        $status  = "success";
                        $message = "Your registration was successful";
                    } else {
                        $status  = "error";
                        $message = "An error occurred, please try again";
                    }
                } else {
                    $status  = "error";
                    $message = "This email is already registered";
                }
            }

            $data = array(
                'status'  => $status,
                'message' => $message
            );

            echo json_encode( $data );

            Database::disconnect();
        }
    }
}

if ( ! empty( $_POST ) ) {
    $email   = $_POST['signup-email'];
    $gender  = $_POST['signup-gender'];
    $country = $_POST['signup-country'];

    Newsletter::register( $email );
}
?>

我的问题是,我无法翻译send.php中的错误/警告消息。我的意思是我不能使用密钥$ml_strings['reg_successful'];而不是“您的注册成功”。它让我发疯,因为我是一个业余爱好者。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

send.php的执行与index.php的执行完全分开,因此您需要初始化两个文件中的$ml_strings变量。换句话说,将以下行添加到Newsletter中的send.php类:

$ml_strings = $ml->getTranslatedStringsForCurrentLanguage();