PHP Classes
elePHPant
Icontem

File: csv2po.php

Recommend this page to a friend!
  Classes of Joseluis Laso  >  Tradukoj PHP Po File Generator  >  csv2po.php  >  Download  
File: csv2po.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Tradukoj PHP Po File Generator
Convert locale CSV file to PO and send to Tradukoj
Author: By
Last change:
Date: 5 years ago
Size: 1,336 bytes
 

 

Contents

Class file image Download
<?php

/**
 * This file is to port csv translations files (look at sample.csv in this same folder) to a .po format
 */

$currentDir = dirname(__FILE__);

require_once(
$currentDir . '/vendor/autoload.php');
require_once(
$currentDir . '/Classes/Tools.php');

$args = new \JLaso\ConsoleArgs\ConsoleArgs($argv, array('help'), array('file', 'output'));

if(!(
$file = $args->getFile) || $args->hasHelp){
    print <<<EOD
Please, use this arguments to invoke this command:

    --help \tto see this help
    --file=filename.csv \tthe file to convert
    --output=filename.po\toptional, the name of the file where put the conversion

EOD;
    exit();
}

$input = $args->getFile;

if(!
$ouput = $args->getOutput){
   
$output = substr( $input, 0, strrpos( $input, "." ) );
}

if(!
file_exists($input)){
    die(
"File {$input} doesn't exist!");
}

$locale = "";
$data = array();

foreach(
file($input) as $row){
   
$row = str_replace(array("\n","\r","\r\n"), "", $row);
    if(
preg_match("/^locale=(?<locale>.*)$/", $row, $matches)){
       
$locale = $matches['locale'];
        continue;
    }
    if(
substr_count($row, ";")){
        list(
$key, $msg) = explode(";", $row);
       
$data[$key] = $msg;
    }
}

$poFile = Tools::generatePoFileFromTradukojData($output, $locale, $data);

print
"Done!, you have your output here: {$poFile}\n";