如何在CodeIgniter中将语言文件内容导出为CSV?

时间:2017-11-16 06:51:16

标签: php codeigniter csv export-to-csv

默认情况下,CodeIgniter具有语言类。我想将所有语言文件导出为CSV格式。是否有任何PHP脚本可用于解析和导出文件内容? 示例文件内容:

<?php 
$lang['Mymenu'] = "Mymenu"; 
$lang['AdminUser'] = "AdminUser";

语言文件也可以放在文件夹中,如

application/language/english
application/language/spanish ...etc

1 个答案:

答案 0 :(得分:0)

从要点中得到解决方案 https://gist.github.com/timneutkens/19e58a00ff8af4663c6502a1d89990a4

感谢您,但该解决方案不适用于递归语言目录。

我在这里发布修改后的版本:

<?php
/**
 * PHP version 5.4
 * @category Export
 * @package  Language
 * @author   Tim Neutkens <tim@weprovide.com>
 * @license  MIT <https://opensource.org/licenses/MIT>
 * @link     <weprovide.com>
 */

define('ENVIRONMENT', 'production');
/**
 * Placeholder for base_url function
 *
 * @param string $path path part of url
 *
 * @return string
 */
function base_url($path = '')
{
    return '/'.$path;
}

/**
 * Placeholder for base_url function
 *
 * @param string $path path part of url
 *
 * @return string
 */
function site_url($path = '')
{
    return '/'.$path;
}

/**
 * Export csv to file location
 *
 * @param array $data csv data
 * @param string $location location to put file
 *
 * @return string
 */
function export_csv($data = [], $location = '',$file_name)
{
   // $file_name = 'export-lang-'.time().'.csv';
    $file_name = $file_name.'.csv';
    $file_path = $location.'/'.$file_name;
    $file      = @fopen($file_path, 'w');

    fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));

    foreach ($data as $key => $value) {
        fputcsv($file, [$key, $value]);
    }

    fclose($file);

    return $file_path;
}

/**
 * Load language
 *
 * @param string $path path to language directory.
 *
 * @return array
 */
function load_language($path = '')
{
    // Loop through translations
    /*$files = glob($path.'/*.php');
    foreach ($files as $filename) {
        include_once $filename;
    }*/

    $di = new RecursiveDirectoryIterator($path);
    foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
    if(!is_dir($filename))
      include_once $filename; //echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
    }

    return $lang;
}

$lang_array = array('arabic','chinese','english','french','german','italian','japanese','korean','polish','portuguese','russian','spanish','turkish','vietnam'); // List of language folders
$i=1;
$file_arr = array();
$lang1 = $lang2 = $lang3 = $lang4 = $lang5 = $lang6 = $lang7 = $lang8 = $lang9 = $lang10 = $lang11 = $lang12 = $lang13 = $lang14 = '' ;
foreach ($lang_array as $language) {

    ${"lang" . $i} =  load_language('application/language/'.$language);
    export_csv( ${"lang" . $i} , '/xampp/htdocs/CodeIginiter/language',$language);
    // Lang gets filled by the above includes
    $i++;
}

将文件放在CodeIgniter的根目录中并执行。将下载CSV文件,可以轻松导出到Excel并用于语言翻译。

相关问题