cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Messing, Elad" <elad.mess...@eml-d.villa-bosch.de>
Subject AW: Checking all keys are translated - done before ?
Date Tue, 04 Oct 2005 10:02:30 GMT
Hello Guys.
	It was a holiday in Germany (not all states) yesterday, so I picked up the glove, and wrote
a small Groovy script that does 2 things :

	1. It goes through the all the files in the project (with some binary exceptions) and look
for the "i18n:key" attribute - to collect keys . It than compare the keys to 1 translation
file - which is declared as the "master" translation file.
		
	This will make sure all of the translation made with the "i18n:key" method are in the master
translation file.
	A missing functionality here is the "i18n:attr" which I do not use currently. It is also
a bit trickier to implement.

	2. Another unrelated task is to open the master file, collect all keys, and make sure they
exist in all the other translation files.

	This will make sure that the translations to the other language is complete.
	

	I chose Groovy because this way you can run it in any platform, easily add it to your ant
build file, and it is also my first Groovy script, so it is a nice way to learn :-)

	Now - Take notice that it is completely primitive, tested only lightly, and could be made
a lot better with some more investment. But it works, and for me it is enough. 
	The idea is, however, that you guys will take it as a starting point, and make it even better.

	I will post it now for your reference. Do you think I should post it in the Wiki as well
? If so - where ?

	Feedback is appreciated.

Elad

*********************************************************************
def getKey (translation){
	// look for key=
	keyPos = translation.indexOf("key=")
	if (keyPos != -1 ){
		// then return everythig from the first " to the last "
		dataArray = translation.split("\"");
		return dataArray[1]
	}
}

def collectKeys (file){
	keys = []
	saveKey = {			
			key = getKey(it)
			if (key){
				keys[keys.size()] = key
			}
		}
	
	file.eachLine (saveKey)
	return keys
}

def findMissingKeysBetweenFiles(masterLanguage, dirName, fileName){ 	
  	println "Finding missing keys between translation files"
	println "findMissingKeysBetweenFiles : Master File name is ${dirName}\\${fileName}_${masterLanguage}.xml"

	
	// open it, and for each key - look for a key in all the other files.	
	masterFile = new File("${dirName}\\${fileName}_${masterLanguage}.xml")
	master_keys = collectKeys(masterFile)
	
	missing_keys = [:]
	
	validateTranslation = { file ->
			if (file.getName() != fileName+".xml" && !file.getName().contains("_${masterLanguage}.xml")
){
				println "validateTranslation: now working on file: " + file.getName()
				keys = collectKeys(file)
				missing_keys_in_file = []
				master_keys.each { key ->
//					println "validateTranslation: now working on ${key}"
					found = keys.findAll {trans_key -> trans_key == key}
//					println found
					if (found.size() == 0){
//						println "couldnt find ${key} in ${file.getName()}"
						missing_keys_in_file.add(key)
					}
				}
				if (missing_keys_in_file.size() > 0 ){
					missing_keys.put(file.getName(),missing_keys_in_file)
				}
			}
		}
	new File(dirName).eachFileMatch (~"${fileName}.*xml",validateTranslation) 
	
	
	 if (missing_keys.size() == 0 ){
	 	println "All keys in the master file exsitst in all translation files"
	 }	
	 else {
	 	missing_keys.each() { key, value -> 
	 			println "the folleing keys are missing in the file ${key} :"
	 			value.each() { println it}	 			
	 	}
	 	
	 }
}

def isBinary(file){
//	println "isBinary: Now working with ${file.getName()}"
	bin_extention = (
			file.getName().contains(".tar") ||
			file.getName().contains(".gif") ||
			file.getName().contains(".GIF") ||
			file.getName().contains(".jpeg") ||
			file.getName().contains(".jpg") ||
			file.getName().contains(".JPG") ||
			file.getName().contains(".tif") ||
			file.getName().contains(".TIF") ||
			file.getName().contains(".zip") ||
			file.getName().contains(".jar") ||
			file.getName().contains(".class") ||
			file.getName().contains(".java") ||
			file.getName().contains(".groovy") ||
			file.getName().contains(".tbl") ||
			file.getName().contains(".log")
		)

	if (bin_extention){
		//println "file ${file.getName()} is binary by extentions"
		return true
	}
	file.withReader {reader -> 
		if (reader != null){
			firstLine = reader.readLine()
			if (firstLine != null){
				for (char1 in firstLine.toCharArray() ){
//					println "isBinary: char1 in line is ${char1}"
					if (Character.isISOControl(char1) ){
							// control char means binary file
//							println "file ${file} is binary by characters"
							return true
					}
				}
			}
		}
	}
	return false
	
}


def findMissingKeysInCode(masterLanguage, dirName, fileName){
  	// Function - find missing translation between code and Master file
  	println "Finding missing keys in code files"
	println "findMissingKeysInCode: Master File name is ${dirName}\\${fileName}_${masterLanguage}.xml"

  	
  	
	all_keys = [:]
	missing_keys = []
	
	inspectFile = { file ->
		// do not insepct directories and some binaries
		// TODO - find a better way to find binaries ?
//		println "inspectFile: now working on the file: ${file.getName()}"
		if (!file.isDirectory() ) {
//			println "inspectFile: file is not a directory"
			if (!file.isHidden()) {
//				println "inspectFile: file is not hidden"
				if (file.getName().charAt(0) != '.' ){
//					println "inspectFile: file doenst have a '.' in the name"
					if (!isBinary(file)){
//						println "inspectFile: now working on the file: ${file}"
						text = file.getText()
				
					  	// searc and collect all :
					  		// 1. i18n:key
					  		// 2. TODO: i18n:attr - and then look for the attribute in the same XML tag.		
						raw_keys_text = text.split("i18n:key=\"").toList()
			//			println raw_keys_text[1]
			//			println raw_keys_text[1].split("\"") 
						// need to ignore first - the text before the first 'i18n:key="' text
						raw_keys_text.remove(0)
				
						raw_keys_text.each { raw_key->
			//				println raw_key.split("\"")
							all_keys.put( ( raw_key.split("\"") ) [0], "stam")
						}
					}
				}
			}
		}
	}
	// Make a search on all files
	new File(".").eachFileRecurse(inspectFile)
//	inspectFile.call (new File("content\\explorer\\Index.jx") )
//	println all_keys
	masterFile = new File("${dirName}\\${fileName}_${masterLanguage}.xml")
	master_keys = collectKeys(masterFile)
	
  	// for each key found - look it up in the master translation file
	all_keys.each() { key, value ->
		found = master_keys.findAll {trans_key -> trans_key == key}	
//		println found
		if (found.size() == 0){
//			println "couldnt find ${key} in master translation file"
			missing_keys.add(key)
		}
	}
	
  	// report the missing
	 if (missing_keys.size() == 0 ){
	 	println "All keys in the code fiels exsitst in the master file"
	 }	
	 else {
	 	missing_keys.each() { 
	 			println "the keys ${it} is missing in the master file"
	 	}
	 }
}

// The English translation file is the master file.
findMissingKeysInCode("en", "translations", "messages")
println "--------------------------------------------------"
findMissingKeysBetweenFiles("en", "translations", "messages")
  	
*********************************************************************

-----Urspr√ľngliche Nachricht-----
Von: Messing, Elad 
Gesendet: Freitag, 30. September 2005 15:25
An: users@cocoon.apache.org
Betreff: Checking all keys are translated - done before ?

Hello all
	I was wondering if anyone ever produced some code that :
	1. Checks that all translation files consists the same keys (everything is translated between
every language).
	2. Checks that all the keys scattered around the cocoon files appear in the translation files
.
	
	This is quite useful before producing a build - to make sure all translations were done.

	I tried looking for this in the message archive, but couldn't find it - although I am quite
sure someone already did it once..


	What do you say ? 

Elad Messing


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message