Skip to content

Instantly share code, notes, and snippets.

@bobpattersonjr
Created March 16, 2011 20:54
Show Gist options
  • Save bobpattersonjr/873279 to your computer and use it in GitHub Desktop.
Save bobpattersonjr/873279 to your computer and use it in GitHub Desktop.
script to parse .csv files to .strings files for translations
#!/usr/bin/perl
#March 16th 2011
#script to parse .csv files to .strings files for translations
#author bob patterson <bob at bobpattersonjr dot com>
sub trim($);
sub escape_quotes($);
sub process($);
my $operation = trim($ARGV[0]);
%file_names = (
Greek => 'el',
Hungarian => 'hu',
ChineseSimplified => 'zh',
Romanian => 'ro',
Spanish => 'es',
Thai => 'th',
Czech => 'cs',
Korean => 'ko',
Vietnamese => 'vi',
Polish => 'pl',
Russian_ => 'ru',
Japanese => 'ja',
Malay => 'ms',
Slovenian => 'sl',
Dutch => 'nl',
Hebrew => 'he',
Italian => 'it',
German => 'de',
Turkish => 'tr',
Hindi => 'hi',
PortugueseBrazil => 'pt',
FrenchUniversalFR => 'fr',
Slovak => 'sk'
);
opendir MYDIR, ".";
@contents = grep !/^\.\.?$/, readdir MYDIR;
closedir MYDIR;
if($operation eq 'js'){
open wFILE, ">", "language_map.js" or die $!;
print wFILE "var language_map = {};\n";
}
foreach $readfile ( @contents )
{
@file_lang = split('.csv',$readfile);
open rFILE, "<", $readfile or die $!;
if($operation eq 'strings'){
open wFILE, ">", $file_names{$file_lang[0]}.".strings" or die $!;
print wFILE "# $file_lang[0] ************ Please save in UTF-8 format! ************\n";
}elsif($operation eq 'js'){
print wFILE "language_map['".$file_names{$file_lang[0]}."'] = {};\n";
}
while(my $line = <rFILE>){
if(!(grep /.*English.*/ , $line )){
chomp($line);
$line =~ s/"//g;
@translation = split(',',$line,2);
if($operation eq 'strings'){
$translation_0_string = process($translation[0]);
print wFILE ":: \"".$translation_0_string."\"\n";
print wFILE "-> \"".$translation[1]."\"\n";
}elsif($operation eq 'js'){
$translation_0_string = escape_quotes($translation[0]);
$translation_0_string = process($translation[0]);
$translation_1_string = escape_quotes($translation[1]);
print wFILE "language_map['".$file_names{$file_lang[0]}."']['".$translation_0_string."'] = '".$translation_1_string."';\n";
}
}
}
}
sub trim($){
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub escape_quotes($){
my $string = shift;
$string =~ s/'/\\'/g;
return $string;
}
sub process($){
my $string = shift;
$string =~ s/\(Your name\)/{*actor*}/g;
$string =~ s/\(Brand\)/{*fanpage_name*}/g;
$string =~ s/\(user answer choice\)/{*user_data_1*}/g;
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment