Jump to content

You'll never be lost in translation again


Recommended Posts

Hi All!Wrote a small, but usefull script today that I would like to share with anyone that might need it.It extracts specific constants that's used to translate projects:(It can be a #### to get all the constants manually. I guess I'm not alone with this problem, and not alone on solving it, but here's my code anyway)

<?php$contents = file_get_contents( 'file.php' );$prefix = 'MYPROJ';$save_to_file = false;$filename = "english.php";$matches = array();preg_match_all( '/_'.$prefix.'[A-Z0-9_]+/', $contents, $matches );$text = "<?php/*** @package MyProject* @version v0.1b* @copyright It's Me* @license CC-GNU GPL v2*/\n\n";foreach ($matches[0] as $match) {	$text .= 'define( '.$match.', "" );'."\n";}$text .= "\n?>\n";if ($save_to_file) {	file_put_contents( $filename, $text );} else {	echo $text;}?>

(You can also download it via https://kachtus.net/projects/get_trans_strings.phps or https://kachtus.net/projects/get_trans_strings.phpt)You can change the values to what you need (filenames, projectname atc). The script can output to either a file (using $filename) or to standard output (console, browser etc).I prefer the last, and do like this (console)

$ php5 get_trans_strings.php > languages/english.php

You can also put it up on your server (together with the "latest" project code) and give the link to the script to let translators to get the latest translation file.Some modifications to the script might be:

  • Getting info from more than one file
  • Getting the filename to save to from input
  • Getting the file to get the info from from input
  • etc

The script is adaptet for this sort of "translation constants" that look like this:

<span><?php echo _MYPROJ_TITLE ?></span>

and the language file:

<?phpdefine( _MYPROJ_TITLE, "My Project" );?>

I now there other ways to translate a script (using assoc. arrays etc), but this is the way I like the most. One advantage is that if there's no definition of the constant, the name of the constant is displayed instead, which is very helpfull..Good Luck and Don't Panic!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...