Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Tool for Translations

posted by jmtucu 8 years ago

Hi! I'm using the internal function to translate all the text from my app using

fText::compose('Users');

My question is, there's an app or library to extract all that text inside the compose() function?

Something like this -> http://awurl.com/8ruBcUoMV

Thanks!

All that Flourish has is the MessagesList page which includes messages from Flourish. You can see a list of the messages on the page and also download a PHP array template that would allow entering the translations.

I can't remember off of the top of my head if I use the tokenizer for this. I am guessing I do. I'll see if I can dig it up at some point and throw it up on GitHub.

posted by wbond 8 years ago

Thanks Will for your answer!

I've downloaded the MessageList file but I'm looking some tool to extract the text from my own files, similar to the link above.

posted by jmtucu 8 years ago

Right, I presumed you were looking functionality to pull the messages out of your code, that's what I meant by I'll try to dig the code up.

This is by no means pretty, but it might be useful as some place to start. Or maybe not.

$max_message_length = 0;
$messages = array();
foreach ($files as $orig_file) {
	$contents = file_get_contents($svn_dir . $orig_file);
	preg_match_all('#(?<=Exception\\()\\s*\\'(.*?)\\'|(?<=Exception\\()\\s*"(.*?)"|(?<=::compose\\()\\s*\\'(.*?)\\'|(?<=::compose\\()\\s*"(.*?)"#', $contents, $matches, PREG_SET_ORDER);
	$start_pos = 0;
	foreach ($matches as $match) {
		$match_text = $match[1] . $match[2] . $match[3] . $match[4];
		$pos = strpos($contents, $match_text, $start_pos);
		$line_num = substr_count(substr($contents, 0, $pos), "\\n") + 1;
		
		if (!isset($messages[$match_text])) {
			$messages[$match_text] = array();
		}
		$messages[$match_text][] = $orig_file . ':' . $line_num;
		
		$match_text = str_replace('\\\\$', '$', $match_text);
		$match_text = str_replace('\\\\"', '"', $match_text);
		$match_text = str_replace('\\\\\\'', '\\'', $match_text);
		$len = strlen(str_replace("'", "\\\\'", $match_text));
		if ($len > $max_message_length) {
			$max_message_length = $len;
		}
		
		$start_pos = $pos + 2;
	}
}

function keycmp($a, $b)
{
	return strcasecmp($a, $b);
}

uksort($messages, "keycmp");
posted by wbond 8 years ago

Excellent Will!

I made a few changes but the your idea was great, thanks again!

This is very useful, maybe you can add it to a library :)

posted by jmtucu 8 years ago