-
-
Save whaleinvasion/cf733073fc55346aef2538611bb62929 to your computer and use it in GitHub Desktop.
Convert a comma-separated value CSV file to Gettext PO format
(i18n, internationalization, localization, translation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* Convert a comma-separated value CSV file to Gettext PO format. | |
* | |
* @copyright Nick Freear, 2 October 2013. | |
*/ | |
$path = '/Users/Nick'; | |
$csv_file = $path . "/Downloads/iSpot SPANISH v. luis lopez-sangil - Spanish.csv"; | |
$locale = 'es'; | |
$id = 'iSpot'; | |
$source = basename($csv_file); | |
$po_file = $path . '/workspace2/'. strtolower($id) .'-csv.'. strtolower($locale) .'.po'; | |
$row_min = 1; # 0-based. | |
$col_msgid = 1; # 0-based. | |
$col_msgstr= 2; | |
$col_notes = 3; | |
$duplicates_check = array(); | |
$output = get_header($locale, $source, $id); | |
$row = 1; | |
if (($handle = fopen($csv_file, "r")) !== FALSE) { | |
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { | |
$num = count($data); | |
$row++; | |
if (! isset($data[$col_msgid]) || ''==$data[$col_msgid]) { | |
echo "Skipping empty msgid, row: $row" .PHP_EOL; | |
continue; | |
} | |
$msgid = addslashes($data[$col_msgid]); | |
if (in_array($msgid, $duplicates_check)) { | |
echo "Skipping duplicate: $msgid" .PHP_EOL; | |
continue; | |
} | |
$duplicates_check[] = $msgid; | |
if (isset($data[$col_notes])) { | |
$output .= PHP_EOL. "#. Context: $row, ". $data[$col_notes] .PHP_EOL; | |
} | |
$output .= 'msgid "'. $msgid .'"' .PHP_EOL; | |
if (isset($data[$col_msgstr])) { | |
$output .= 'msgstr "'. addslashes($data[$col_msgstr]) .'"' .PHP_EOL; | |
} | |
else { | |
$output .= 'msgstr ""' .PHP_EOL; | |
} | |
} | |
fclose($handle); | |
} else { | |
die("Error opening CSV file: $csv_file"); | |
} | |
$bytes = file_put_contents($po_file, $output); | |
function get_header($locale = NULL, $source = NULL, $id = NULL, $charset = 'UTF-8') { | |
return <<<EOH | |
# $id language/translation. | |
# | |
# Source: $source | |
# | |
# Copyright (c) 2013 The Open University. | |
# This file is distributed under the same license as the PACKAGE package. | |
# IET-OU <EMAIL@ADDRESS>, YEAR. | |
# | |
#, fuzzy | |
msgid "" | |
msgstr "" | |
"Project-Id-Version: $id\\n" | |
"Report-Msgid-Bugs-To: iet-webmaster+@+open.ac.uk\\n" | |
"POT-Creation-Date: 2013-10-02 14:00+0100\\n" | |
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n" | |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" | |
"Language-Team: LANGUAGE <[email protected]>\\n" | |
"Language: $locale\\n" | |
"MIME-Version: 1.0\\n" | |
"Content-Type: text/plain; charset=$charset\\n" | |
"Content-Transfer-Encoding: 8bit\\n" | |
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n" | |
EOH; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment