Language translation using PHP

后端 未结 6 1599
名媛妹妹
名媛妹妹 2021-01-03 13:51

Hi i am devloping sample site in php i need to translate whole website in to persian. how can it possible in php?? I have tried using the following code.. This code will wor

6条回答
  •  情书的邮戳
    2021-01-03 14:13

    If I were you, I'd do it like this:

    /inc/lang/en.lang.php

    define('_HELLO', 'Hello');
    

    /inc/lang/fa.lang.php

    define('_HELLO', 'سلام');
    

    index.php

    // $_SESSION['lang'] could be 'en', 'fa', etc.
    require_once '/inc/lang/' . $_SESSION['lang'] . 'lang.php';
    
    echo _HELLO;
    

    Benchmark: Constants vs. Variables

    Here you see why I offered using Constants not Variables:

    const.php

    echo memory_get_usage() . '
    '; // output: 674,576 for ($i = 0; $i <= 10000; $i++) { define($i, 'abc'); } echo memory_get_usage() . '
    '; // output: 994,784

    var.php

    echo memory_get_usage() . '
    '; // output: 674,184 for ($i = 0; $i <= 10000; $i++) { $$i = 'abc'; } echo memory_get_usage() . '
    '; // output: 2,485,176

提交回复
热议问题