【问题标题】:Translate string from array keys won't work for more than one segment?从数组键翻译字符串不能用于多个段?
【发布时间】:2014-05-02 17:54:23
【问题描述】:

我只需要使用一组键及其翻译进行简单的字符串翻译(url)。

尝试这样:

function ruta_iso( $ruta ) {
    $slugs = array(
        'noticia' => array(
            'es' => 'noticia',
            'en' => 'post'
        ),
        'pregunta' => array(
            'es' => 'pregunta',
            'en' => 'question'
        ),
        'consejo' => array(
            'es' => 'consejo',
            'en' => 'tip'
        ),
        'noticias' => array(
            'es' => 'noticias',
            'en' => 'news'
        )
    );


    $idioma_defecto = 'es';
    $idioma = 'en' ;

    if ( ($idioma != $idioma_defecto) && ($ruta == '/') ) {
        return "/$idioma";
    } else if( $idioma !=  $idioma_defecto ){
        foreach($slugs as $key => $slug){
            if ( ( strpos("/$key/", $ruta ) === false ) ){
            }else {
                $ruta = str_replace( "/$key/", '/'.$slug[$idioma].'/' , $ruta );

            }
        }
        $ruta = "/$idioma$ruta"; 
    } else {

    }
    return $ruta;
}

echo '-------Ruta Iso '.ruta_iso('/noticias/'); /* Works! */

echo ' -------Ruta Iso '.ruta_iso('/noticias/noticia/'); /* Nope.. */

可以在这里测试:

http://codepad.org/3w3Vmncg

它似乎只为一只蛞蝓完成这项工作,但如果不止一只,甚至:

echo ' -------Ruta Iso '.ruta_iso('/noticias/blabla/'); /* Nope.. */

所以我不确定如何尝试,我的意思是,我没有破坏 foreach,为什么不检查每个字符串?

有吗?

【问题讨论】:

    标签: php arrays foreach str-replace


    【解决方案1】:

    您也可以尝试这样做:

    function ruta_iso( $ruta = '') {
        $slugs = array(
            'noticia' => array(
                'es' => 'noticia',
                'en' => 'post'
            ),
            'pregunta' => array(
                'es' => 'pregunta',
                'en' => 'question'
            ),
            'consejo' => array(
                'es' => 'consejo',
                'en' => 'tip'
            ),
            'noticias' => array(
                'es' => 'noticias',
                'en' => 'news'
            )
        );
    
        $idioma_defecto = 'es';
        $idioma = 'en' ;
    
        foreach( explode('/',  $ruta) as $data){
        if( !empty($data) ){
    
            if( array_key_exists($data, $slugs)){
                $result[] = $slugs[$data][$idioma];
            }else{
                $result[] = $data;
            }
        }
    }
        $result = '/'.$idioma.'/'.implode('/', $result).'/';
    
        return $result;
    }
    
    echo '-------Ruta Iso '.ruta_iso('/noticias/');
    
    echo ' -------Ruta Iso '.ruta_iso('/noticias/noticia/');
    
    echo '-------Ruta Iso '.ruta_iso('/noticias/noticia/the-new');
    

    结果:

    -------Ruta Iso /en/news/ -------Ruta Iso /en/news/post/
    -------Ruta Iso /en/news/post/the-new/
    

    【讨论】:

    • 嗨!感谢您的分析!但试试这个:ruta_iso('/noticias/noticia/the-new');$slugs 数组中没有the-new 的引用,但它仍然被什么都替换(或只是丢失),它返回:/en/news/post/ 而不是/en/news/post/the-new 对吗?
    • 我更改了我的代码,现在它会返回 /en/news/post/the-new
    • 是的!好像是这样!让我再测试一下.. ^^ 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 2017-03-11
    • 1970-01-01
    相关资源
    最近更新 更多