multiple language via session, two directories

前端 未结 2 1742
执念已碎
执念已碎 2020-12-22 12:40

So I just started web development on my new job, meaning I only have very basic knowledge of php, my html, css and js knowledge is a lot better. I\'m trying to build a basic

2条回答
  •  礼貌的吻别
    2020-12-22 13:01

    I agree with K0pernikus in that your solution won't scale well. You can write a simple translation library yourself in under an hour and you can then test it to see if it will be robust enough for your needs.

    The idea is to have language files without any logic in them. You simply call the file you need. All the logic about which language, file, translation key etc. is contained in your library.

    I'll keep it very simple and down to a single file; of course, the actual translations will each need to be stored in a file too.

    Directory structure:

    • /locale/en/messages.php
    • /locale/fr/messages.php
    • translator.php (this is the main library file and needs to be included on every page)

    Within each messages.php file you need to return an array of translation keys and their respective translation. The translation keys are what you will use in your views. These files will become large with many hundreds or thousands of lines for larger applications. If you intend to keep this home-grown solution you'll need to implement caching. That being said, I have files with hundreds of translations and don't notice any significant performance hit.

     'Matt\'s Marvelous Mysteries',
        ...
    
    
     'Les merveilleux mystères de Matt',
        ...
    

    Next, you need a library to read these translations and return the actual translation text to you. This file could simply be a collection of helper functions or a full-blown OOP system. For simplicity, here is a collection of helper methods that get the job done. It does not implement parameterized substitution and you can add that feature relatively easily by making t() accept a 2nd argument, but that is a whole different topic for another time.

    The main method in here is t(). It is very simple and accepts a single translation key. Ex. applicationName or greeting.

    Firstly, it tries to determine which language to use. It does this in a sequence of priority: URL, session, browser, fallback.

    • It first tries to get the language/locale from the URL by looking for a query string parameter named lang. If you think about it, that makes sense because a user intends to switch their language by clicking a link that says "English" or "French".
    • If it doesn't find one in the URL it then moves on to check for one in the session. Again, if it finds it there it uses it.
    • If it finds it neither in the URL nor the session, then it checks the browser/headers from the request.
    • Finally, if it isn't found in any of those 3 locations then it falls back to the default language as specified in $defaultLanguage.

    Once a language has been found, it puts it into the session so the next request doesn't need to go through all that again. It also loads the appropriate messages.php file based on the discovered language.

    Finally, once the language has been found and right file has been loaded into memory it searches for the given $key and returns the appropriate translation. If the $key is not found then it simply returns the given $key which will show up in your views so you know something went horribly wrong and you need to start debugging.

     0.8
                $languages = array_combine($lang_parse[1], $lang_parse[4]);
    
                // set default to 1 for any without q factor
                foreach ($languages as $lang => $val) {
                    if ($val === '') $languages[$lang] = 1;
                }
    
                // sort list based on value
                arsort($languages, SORT_NUMERIC);
            }
        }
    
        $supportedLanguages = getSupportedLanguages();
    
        foreach ($languages as $locale => $weighting) {
    
            // We're dealing with locale: Ex. en-US
            if (preg_match("/[a-z]{2}-[A-Z]{2}/", $locale)) {
                $browserLanguage = substr($locale, 0, 2);
            } else {
                // Probably dealing with a language: Ex. en
                $browserLanguage = $locale;
            }
    
            if (in_array($browserLanguage, $supportedLanguages)) {
                return $browserLanguage;
            }
        }
    
        return $defaultLanguage;
    }
    
    /**
     * Returns an array of languages this web application supports.
     *
     * @return array
     */
    function getSupportedLanguages()
    {
        return [
            'en',
            'fr'
        ];
    }
    

    To use it, save these methods into a file called translator.php and then include that file in every page you want to use translations.

    Sample:

    French';
    } else {
        echo 'English';
    }
    
    
    // Your code... blah blah
    
    // Ahh.. Finally, a translation!
    echo '

    ' . t('applicationName') . '

    ';

    Edit

    The last thing I will say is that there is a whole world out there of localization, internationalization (often abbreviated as i18n) which you can learn about.

    In my example, I simplistically called it language but often people referer to it as locale but that has a different meaning and syntax. For example, there is a difference between en_CA and en_US and en_GB; all of which are English but have regional differences.

    echo t('salutation');
    

提交回复
热议问题