getI18NMessageforLocale

Hi,

Could we get a function getI18NMessageforLocale(String key, String locale)?

Currently, the getI18NMessage is based on the current locale and returns the key between exclamation marks when the entry for the current locale is not found or when there is no entry found at all.

What I would like to do is:
1- test if there is a ‘base’ entry (locale field empty) for a certain Key and if not, create it
2- test if there is an entry for a certain locale/key and if not create it.

Currently, it seems to me that doing this whould involve looping through the entire set of keys for every key/locale combination.

Paul

You can also do a SQL query (not real column names - I don’t have the actual column names handy):

select count(message_key) where message_key = 'my.msg.key' and locale = 'fr'

If you get 0 - then no key, otherwise, key.

Hope this helps.

Tnx Bob.

Allthough it’s not the clean code I’d like, it certainly does the trick. Performance improved drastically :)

for all the people interested, here’s the code. Saves me a lot of time, typing in all the key’s I need. Basically, this code, when run, creates I18N keys for every form, every table and every dataprovider in the table, for a certain Server, a certain Locale and the current solution.

The code relies on a form called applicationtranslation, which is basically a view on your I18N table and a table called languages, in which I store the translations that I want to have available for my solution. The available translations are stored with a start and enddate.

You probably have to fiddle a bit with it to make it work, but it gives a nice idea of what’s possible.

//Configure:
var key_prefix = 'kydome';
var i18n_server = 'Kydome';

//Select Server
var server = plugins.dialogs.showSelectDialog('Select','Select the Server for which you like to create I18N keys:',databaseManager.getServerNames());
if (!server) return;

/* 	Start Language determination
	The Language determination depends on a table called languages,
	where available languages for your application are stored with a start and enddate.
	If your application doesn't work like this, this part needs to be customized to set the variable "locale" in another way
*/
		var maxReturnedRows = 1000;
		var query = 'select languagekey from languages where startdate < now() and (enddate is null or enddate > now())'; 
		var dataset = databaseManager.getDataSetByQuery('Kydome', query, null, maxReturnedRows);
		
		var locale = null;
		if (dataset.getMaxRowIndex() != 0)
		{
			var languages = dataset.getColumnAsArray(1);
			languages[languages.length] = 'Default';
			locale = plugins.dialogs.showSelectDialog('Select','Select the Locale for which you like to create I18N keys:',languages);
			if (!locale) 
			{
				return;
			}
			else if (locale == 'Default')
			{
				locale = null;
			}
		}
		else
		{
			var test = plugins.dialogs.showQuestionDialog('Question:','No active languages found. Do you want to create the default keys?','Yes','No');
			if(test == 'Yes')
			{
				locale = null;
			}
			else
			{
				return;
			}
		}
//End Language determination

var tables = databaseManager.getTableNames(server);
var count = 0;
for ( var i = 0 ; i < tables.length ; i++ )
{
	query = 'select * from messages where message_key = \''+key_prefix+'.table.'+tables[i]+'\' and message_language = \''+locale+'\''; 
	dataset = databaseManager.getDataSetByQuery(i18n_server, query, null, maxReturnedRows);

	if(dataset.getMaxRowIndex() == 0 )
	{
		count += 1;
		forms.applicationtranslation.controller.newRecord();
		forms.applicationtranslation.message_key = key_prefix+'.table.'+tables[i];
		forms.applicationtranslation.message_language =  locale;
		forms.applicationtranslation.message_value = utils.stringInitCap(tables[i]);
		forms.applicationtranslation.controller.saveData();
	}
	var JSTable =  databaseManager.getTable(server,tables[i]);
	var columns = JSTable.getColumnNames();
	
	for ( var j = 0 ; j < columns.length ; j++ )
	{
	query = 'select * from messages where message_key = \''+key_prefix+'.dataprovider.'+columns[j]+'\' and message_language = \''+locale+'\''; 
	dataset = databaseManager.getDataSetByQuery(i18n_server, query, null, maxReturnedRows);
		
		if( dataset.getMaxRowIndex() == 0 )
		{
			count += 1;
			forms.applicationtranslation.controller.newRecord();
			forms.applicationtranslation.message_key = key_prefix+'.dataprovider.'+columns[j];
			forms.applicationtranslation.message_language = locale;
			forms.applicationtranslation.message_value = utils.stringInitCap(columns[j]);
			forms.applicationtranslation.controller.saveData();
		}
	}
}

var formsArray =  forms.allnames;
for ( var j = 0 ; j < formsArray.length ; j++ )
{
	query = 'select * from messages where message_key = \''+key_prefix+'.form.'+formsArray[j]+'\' and message_language = \''+locale+'\''; 
	dataset = databaseManager.getDataSetByQuery(i18n_server, query, null, maxReturnedRows);

	if( dataset.getMaxRowIndex() == 0 )
	{
		count += 1;
		forms.applicationtranslation.controller.newRecord();
		forms.applicationtranslation.message_key = key_prefix+'.form.'+formsArray[j];
		forms.applicationtranslation.message_language =  locale; 
		forms.applicationtranslation.message_value = utils.stringInitCap(formsArray[j]);
		forms.applicationtranslation.controller.saveData();
	}
}
plugins.dialogs.showInfoDialog('Info:','There were '+count+' keys created.','OK');

Enjoy!

Paul

Paul,

THANK YOU for sharing the entire code!

GREAT STUFF!!

Tnx Bob,

Would be wonderfull if the getNamedFormElements function discussed in http://forum.servoy.com/viewtopic.php?t=3614&start=15 could be implemented. Then you can extend this code to also create the I18N keys for, for example buttons :D

Paul