Add multiple languages to ecshop

Source: Internet
Author: User
Requirement: the foreground uses English (en_us) by default. You can select Chinese (zh_cn), and the English page is still in English. Idea: Add lang to the accessed url],

Requirement: the foreground uses English (en_us) by default. You can select Chinese (zh_cn), and the English page is still in English. Idea: add the lang = zh_cn variable to the accessed url to use Chinese characters. If there is no lang variable, use the default language scheme: When the url does not have the lang variable, if the logged-on user sets $ _ SESSION ['lang '],

Requirements:
By default, the foreground uses English (en_us). You can select Chinese (zh_cn), and the English page is still in English.

Ideas:
Add the lang = zh_cn variable to the accessed url to use Chinese characters. If there is no lang variable, use the default language.

Solution:
When the url does not have the lang variable, if the logged-on user sets $ _ SESSION ['lang '], use $ _ SESSION ['lang'] as the default language, if $ _ COOKIE ['lang '] is set for a user who has not logged on, $ _ COOKIE ['lang'] is used as the default language; otherwise, 'en _ us' is used as the default language.

Implementation:
0. Input lang = zh_cn or lang = en_us into the url to load the Language Pack.
Open mongodes/init. php and add the following code after code $ _ CFG = load_config:

$ _ CFG ['lang '] = 'en _ us'; // The foreground uses English by default $ langList = array ('zh _ cn', 'en _ us '); // The language that can be used if (! Empty ($ _ REQUEST ['lang ']) & in_array ($ _ REQUEST ['lang'], $ langList )) {$ _ CFG ['lang '] = $ _ REQUEST ['lang']; // language parameter passed through URL}

Then
If (DEBUG_MODE & 1) = 1)
Previously added:

// If you customize the default language, reload the language if (empty ($ _ REQUEST ['lang ']) |! In_array ($ _ REQUEST ['lang '], $ langList) {if (! Empty ($ _ SESSION ['lang ']) & in_array ($ _ SESSION ['lang'], $ langList) & $ _ SESSION ['lang ']! = $ _ CFG ['lang ']) {$ _ CFG ['lang'] = $ _ SESSION ['lang ']; require (ROOT_PATH. 'AGES /'. $ _ CFG ['lang ']. '/common. php '); $ smarty-> assign ('lang', $ _ lang);} else if (! Empty ($ _ COOKIE ['lang ']) & in_array ($ _ COOKIE ['lang'], $ langList) & $ _ COOKIE ['lang ']! = $ _ CFG ['lang ']) {$ _ CFG ['lang'] = $ _ COOKIE ['lang ']; require (ROOT_PATH. 'AGES /'. $ _ CFG ['lang ']. '/common. php '); $ smarty-> assign ('lang', $ _ lang );}}

1. Implement the function of adding a language identifier after the url

/*** Add the language ID to the url */function url_lang ($ url) {if (! Empty ($ _ GET ['lang ']) & $ url [0]! = '#'&&! Preg_match ("/[&?] Lang = (.*?) /", $ Url) {$ anchor = strstr ($ url, '#'); // The anchor must be placed after the request variable if ($ anchor) {$ url = str_replace ($ anchor, '', $ url ); // remove the anchor} // The request variable already exists in the url, and add "& lang = $ _ GET ['lang ']". Otherwise, add "? Lang = $ _ GET ['lang '] "$ url. = (strpos ($ url ,'? ')! = False? '&':'? '). 'Lang ='. $ _ GET ['lang ']; $ url. = $ anchor; // Add an anchor} return $ url ;}

2. Modify the build_uri () function
// Add the following before return $ uri:
$ Uri = url_lang ($ uri );

3. Add the variable in the template, such as {$ var}, to {$ var | url} so that the program can automatically identify whether lang = zh_cn | en_us needs to be added.
Add a case before default: Of the cls_template: get_val () function. The Code is as follows:

case 'url' :    $p = 'url_lang(' . $p . ', 1)';    break;

4. Add the on-site link in the template to identify whether lang = zh_cn | en_us needs to be added (The on-site link does not need to be added)
4.1 Change {$ xxx} to {$ xxx | url} using a single variable as the url in the template}
4.2 If the template uses multiple variables as URLs without other strings, change the first variable to {$ xxx | url}
4.3 In the template, the variable mixed string is used as the url and added at the end of the url as needed
{If $ smarty. get. lang} & lang = {$ smarty. get. lang} {/if} or {if $ smarty. get. lang }? Lang = {$ smarty. get. lang} {/if}

5. Modify the jump url in the foreground program

------------

Solution 2: If you only need one page setting language and other pages change at the same time, we can use cookies to accommodate a large number of days.
1. Set Language
Add lang = xxx & set_lang = 1 to the url of the language to be set.

2. Use different languages

/* Load the system parameter */$ _ CFG = load_config (); // simon: select the language $ _ CFG ['lang '] = 'en _ us '; // The front-end uses English by default. // the Hong Kong, Macao, and Taiwan browsers use traditional Chinese if (preg_match ("/(zh \-tw) by default. | (zh \-hk) | (zh \-mo) | (zh \-hant)/I ", $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE ']) {$ _ CFG ['lang '] = 'zh _ tw ';} // elseif (preg_match ("/(zh \-cn) | (zh \-sg) | (zh \-hans) /I ", $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE ']) {$ _ CFG ['lang'] = 'zh _ cn';} if (! Empty ($ _ REQUEST ['lang ']) |! Empty ($ _ COOKIE ['lang ']) {$ lang = empty ($ _ REQUEST ['lang'])? $ _ COOKIE ['lang ']: $ _ REQUEST ['lang']; if (in_array ($ lang, array ('zh _ cn', 'zh _ tw ', 'en _ use') {$ _ CFG ['lang '] = $ lang; // language parameter passed through URL}/* load language file */require (ROOT_PATH. 'AGES /'. $ _ CFG ['lang ']. '/common. php '); // if (! Empty ($ _ GET ['set _ lang ']) {setcookie ('lang', $ _ CFG ['lang '], time () + 30*24*3600 );}

Original article address: add multiple languages to ecshop. Thank you for sharing it with the original author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.