doukucai0099 2015-11-05 09:35
浏览 26

PHP常量的翻译替代方法

I've been working in translations for customly designed webs for a while and those needed translations, so what I'm currently doing for translating the text is the following:

I have multiple files that have a bunch of PHP files that contain the strings in multiple languages. Each file has the same constant declared exactly the same. Like this:

define('MENU_ITEM1','WHO ARE WE?');
define('MENU_ITEM2','PROFESSIONAL ACTIVITY');

define('MENU_ITEM1','¿QUIÉN SOMOS?');
define('MENU_ITEM2','ACTIVIDAD PROFESIONAL ');

Then, on the menu I have the language selector. The select creates a get variable on the url displaying the selected language, like this:

<select id="select" onChange="idioma(this.value);"> 
        <option value="ES">Español (ES)</option>
        <option value="EN">English (EN)</option>
        ...
</select>

This calls a function that adds a get to the url. Like this (for every language):

location.href=url+"?lang=ES";

So in another file, we have a PHP switch that takes the language from the url and creates a cookie that includes the right language file. If the cookie is not set, we have a default language. It would be something like this:

<?php 

if(isSet($_GET['lang'])){
    $lang = $_GET['lang'];

     $_SESSION['lang'] = $lang;

    setcookie('lang', $lang, time() + (3600 * 24 * 30));

    }else if(isSet($_SESSION['lang'])){
        $lang = $_SESSION['lang'];
    }else if(isSet($_COOKIE['lang'])){
        $lang = $_COOKIE['lang'];
    }else{
        $lang = 'ES';
    }

switch ($lang) {

    case 'EN':
    $lang_file = 'en/lang.en.php'; 
    break;

    case 'ES':
    $lang_file = 'es/lang.es.php'; 
    break;

    default:
    $lang_file = 'es/lang.es.php';

}

include_once $lang_file;

?>

So by this teeeedious process, the web is traslated. Issue: I have the slight feeling that this is not an optimal solution, so if anyone has a way to fix it or do it in any other way, it would be so helpful. Note that I do not work with WordPress, so WP plugins are out of the question (I worked with them, I know they exist). I've seen webs that use subdomains (en.something.com) or other folders (something.com/en/) but I'm not really sure how to do that.

  • 写回答

1条回答 默认 最新

  • douruoshen1449 2015-11-05 10:11
    关注

    Trust me I've been there before. I've written functionality for a website that now uses more than 5 languages.

    In my approach I used CSV files (Comma-Separated Values). You could start your development with the following code.

    Your Spanish translation file es.csv

    'Home', 'Inicio'
    'Page 1', 'Pagina 1'
    'Page 2', 'Pagina 2'
    'Page 3', 'Pagina 3'
    

    The first column of words would be your default language which would be defined within a function.

    <a href="#"><?php __("Home"); ?>
    

    Then you would use a switch statement like the one you have on top to capture which language the user choose. The __() function would look something like this. Where input would be "home"

    $csv = array_map('str_getcsv', file($file));
    // Logic (where it would do the translation)
    // It could look for the input in the $cvs variable if it exist then use the next value of the current array. Else keep the text the same.
    

    Your cvs file would need to make the exact words to be translated

    I hope this makes sense.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法