Website Language English Solution--by changing the buffer to achieve

Source: Internet
Author: User
/*
Website Language English Solution--by changing the buffer to achieve
Function: According to the cookie to determine the user's choice of language, the default is Chinese (do not replace), when the user selects English in the page, then through the JS write the cookie language status code, this page through the judgment change the cookie to determine whether the language translation.
Description: In the front end through the JS to determine the user's choice of language, the user chooses English, JS write the cookie language state as ' EN ', the background program by judging the cookie saved language encoding to perform language translation operations.
Author: AIBHSC
How to use: in the following file to replace the Chinese and English, and then in the program portal file reference the file.
Application scenario: For the Enterprise website English is not used frequently, modify the program and the database is too expensive, but also must be in English website, you can try to use this program.
*/
  1. /*
  2. Web site language handlers--by changing the buffer to achieve
  3. Function: According to the cookie to determine the user's choice of language, the default is Chinese (do not replace), when the user selects English in the page, then through the JS write the cookie language status code, this page through the judgment change the cookie to determine whether the language translation.
  4. Description: In the front end through the JS to determine the user's choice of language, the user chooses English, JS write the cookie language state as ' EN ', the background program by judging the cookie saved language encoding to perform language translation operations.
  5. Author: AIBHSC
  6. How to use: in the following file to replace the Chinese and English, and then in the program portal file reference the file.
  7. Application scenario: For the Enterprise website English is not used frequently, modify the program and the database is too expensive, but also must be in English website, you can try to use this program.
  8. */
  9. Through one-dimensional array storage language pack, save space, convenient key processing, note: ① key value do not dislocation; ② long string precedence
  10. Language Pack, put the content to be translated here--start
  11. $CN []= ' user login '; $en []= ' userlogin ';
  12. $CN []= ' login '; $en []= ' login ';
  13. $CN []= ' registration '; $en []= ' Reg ';
  14. $CN []= ' home '; $en []= ' home ';
  15. $CN []= ' view '; $en []= ' View ';
  16. $CN []= ' whereabouts '; $en []= ' Local ';
  17. $CN []= ' drill weight '; $en []= ' Weight ';
  18. $CN []= ' state inspection '; $en []= ' NGTC ';
  19. $CN []= '; $en []= ';
  20. Language Pack--end
  21. A global definition that references a language pack.
  22. $GLOBALS [' GLOBAL_CN ']=& $cn;
  23. $GLOBALS [' global_en ']=& $en;
  24. $GLOBALS [' Global_lang ']=$_cookie[' User_lang ']?$_cookie[' User_lang ']: ' CN ';
  25. Cache ();//Perform language translation operations
  26. function cache () {
  27. Ob_start (Array ($this, "Autocache"))//Do not understand this line please see the PHP manual Call_user_func fourth Example "Example #4"
  28. }
  29. function Cn2en ($body) {//parameter: $body the content to be translated; $arr _cn Chinese Language pack; $arr _en English Language pack; return value: Replaced string
  30. When the two language pack key values are incorrect, the original value is returned without any action.
  31. if (count ($GLOBALS [' GLOBAL_CN ']) <>count ($GLOBALS [' global_en ']) {return $body;}
  32. When the user chooses English, the Chinese in the document is replaced by the language pack in English
  33. if ($GLOBALS [' Global_lang ']== ' EN ') {$body =str_ireplace ($GLOBALS [' GLOBAL_CN '], $GLOBALS [' global_en '], $body);}
  34. return $body;
  35. }
  36. function Autocache ($contents) {
  37. $contents = Cn2en ($contents);//Perform language translation
  38. return $contents;
  39. }
  40. ?>
Copy Code
  • Related Article

    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.