Skip to main content

class Language

<?php

class Language {

    private static $MESSAGES = array(
        'en'    =>  array(
            'user'  =>  array(
                'success'   =>  array(
                    'change_email_success'  =>  'Your email address has been changed successfully!<br>Please using new email address to login the next time.',
                    'reissue_activation_success'    =>  '新しいURLが再発行されました。メールをご確認ください。'
                ),
                'error'     =>  array(
                    'url_expired'           =>  'URLの有効期限が過ぎました。お手数ですが、再度ログイン情報を変更してください。',
                    'invalid_token'         =>  '認証コードは不正です。',
                    'email_duplicated'      =>  'This email address has already been registered.',
                    'activate_url_expired'  =>  'URLの有効期限が過ぎました。 お手数ですが、以下のボタンをクリックし、URLの再発行を行ってください。何かご不明な点がありましたら、info@kotora.jpまでご連絡ください。'
                )
            ),
            'clogo' =>  array(
                'confirm'   =>  array(
                    'delete'    =>  'Do you really want to delete?'
                )
            ),
            'common'    =>  array(
                'success'   =>  array(

                ),
                'error'     =>  array(
                    'unknown_error'     =>  '不明なエラー。',
                    'invalid_url'       =>  'Incorrect URL.',
                    'no_data'           =>  'There is no data.',
                    'invalid_action'    =>  array(
                        'nUser'         =>  'この機能をご利用になる場合は会員登録(クライアント)が必要です。会員登録(クライアント)をご希望の方は一度ログアウトして、会員登録ボタンをクリックしてください。ログアウトは<a href="login.php?logout=true">こちら</a>から',
                        'cUser'         =>  'この機能をご利用になる場合は会員登録(プロフェッショナル)が必要です。会員登録(プロフェッショナル)をご希望の方は一度ログアウトして、会員登録ボタンをクリックしてください。ログアウトは<a href="login.php?logout=true">こちら</a>から',
                        'nobody'        =>  'この機能はログインしていないと使用できない機能です。ログインしてからお使いください。<br>ログインは<a href="@loginUrl">こちら</a>。<br>まだ会員登録がお済みでない方<a href="@registerUrl">はこちら</a>。'
                    )
                )
            )
        ),
        'jp'    =>  array(
            'user'  =>  array(
                'success'   =>  array(
                    'change_email_success'  =>  'メールアドレスの変更が完了しました。次回ログインの際は、新しいメールアドレスをご使用ください。',
                    'reissue_activation_success'    =>  '新しいURLが再発行されました。メールをご確認ください。'
                ),
                'error'     =>  array(
                    'url_expired'           =>  'URLの有効期限が過ぎました。お手数ですが、再度ログイン情報を変更してください。',
                    'invalid_token'         =>  '認証コードは不正です。',
                    'email_duplicated'      =>  'メールアドレスは既に登録されています。',
                    'activate_url_expired'  =>  'URLの有効期限が過ぎました。 お手数ですが、以下のボタンをクリックし、URLの再発行を行ってください。何かご不明な点がありましたら、info@kotora.jpまでご連絡ください。'
                )
            ),
            'clogo' =>  array(
                'confirm'   =>  array(
                    'delete'    =>  '削除してよろしいでしょうか。'
                )
            ),
            'common'    =>  array(
                'success'   =>  array(

                ),
                'error'     =>  array(
                    'unknown_error'     =>  '不明なエラー。',
                    'invalid_url'       =>  'URLは不正です。',
                    'no_data'           =>  'データはありません。',
                    'invalid_action'    =>  array(
                        'nUser'         =>  'この機能をご利用になる場合は会員登録(クライアント)が必要です。会員登録(クライアント)をご希望の方は一度ログアウトして、会員登録ボタンをクリックしてください。ログアウトは<a href="login.php?logout=true">こちら</a>から',
                        'cUser'         =>  'この機能をご利用になる場合は会員登録(プロフェッショナル)が必要です。会員登録(プロフェッショナル)をご希望の方は一度ログアウトして、会員登録ボタンをクリックしてください。ログアウトは<a href="login.php?logout=true">こちら</a>から',
                        'nobody'        =>  'この機能はログインしていないと使用できない機能です。ログインしてからお使いください。<br>ログインは<a href="@loginUrl">こちら</a>。<br>まだ会員登録がお済みでない方<a href="@registerUrl">はこちら</a>。'
                    )
                )
            )
        )
    );

    /**
     * Get translated message
     *
     * @param string $messageId
     * @param array $replaceData
     * @return array|mixed|string
     */
    public static function getMessage($messageId, $replaceData = array())
    {
        // Separate keys and values of replace data into 2 arrays
        $placeHolders = !empty($replaceData) ? array_keys($replaceData) : array();
        $replaceValues = !empty($replaceData) ? array_values($replaceData) : array();

        return self::translateMessage($messageId, $placeHolders, $replaceValues);
    }

    /**
     * Get translated message by key in dot notation form
     * Support replace placeholder with real value
     *
     * @param string $id
     * @param array $placeHolders
     * @param array $replaceValues
     * @return array|mixed|string
     */
    public static function translateMessage($id, $placeHolders = array(), $replaceValues = array())
    {
        $keys = explode('.', $id);

        $returnMsg = self::$MESSAGES;

        if (empty($keys)) {
            $returnMsg = '';
        } else {
            foreach ($keys as $key) {
                $returnMsg = isset($returnMsg[$key]) ? $returnMsg[$key] : (is_string($returnMsg) ? $returnMsg : '');
            }
        }

        // Replace placeholder with real value
        if (!empty($returnMsg) && !empty($placeHolders) && !empty($replaceValues)) {

            if (count($placeHolders) == count($replaceValues)) {
                foreach ($placeHolders as $index => $placeHolder) {
                    $returnMsg = str_replace($placeHolder, $replaceValues[$index], $returnMsg);
                }
            }
        }

        return $returnMsg;
    }
}


Comments

Popular posts from this blog

Rand mm 10

https://stackoverflow.com/questions/2447791/define-vs-const Oh const vs define, many time I got unexpected interview question. As this one, I do not know much or try to study this. My work flow, and I believe of many programmer is that search topic only when we have task or job to tackle. We ignore many 'basic', 'fundamental' documents, RTFM is boring. So I think it is a trade off between the two way of study language. And I think there are a bridge or balanced way to extract both advantage of two method. There are some huge issue with programmer like me that prevent we master some technique that take only little time if doing properly. For example, some Red Hat certificate program, lesson, course that I have learned during Collage gave our exceptional useful when it cover almost all topic while working with Linux. I remember it called something like RHEL (RedHat Enterprise Linux) Certificate... I think there are many tons of documents, guide n books about Linux bu

Martin Fowler - Software Architecture - Making Architecture matter

  https://martinfowler.com/architecture/ One can appreciate the point of this presentation when one's sense of code smell is trained, functional and utilized. Those controlling the budget as well as developer leads should understand the design stamina hypothesis, so that the appropriate focus and priority is given to internal quality - otherwise pay a high price soon. Andrew Farrell 8 months ago I love that he was able to give an important lesson on the “How?” of software architecture at the very end: delegate decisions to those with the time to focus on them. Very nice and straight-forward talk about the value of software architecture For me, architecture is the distribution of complexity in a system. And also, how subsystems communicate with each other. A battle between craftmanship and the economics and economics always win... https://hackernoon.com/applying-clean-architecture-on-web-application-with-modular-pattern-7b11f1b89011 1. Independent of Frameworks 2. Testable 3. Indepe