Class: MobyUtil::Localisation

Inherits:
Object
  • Object
show all
Defined in:
lib/tdriver/util/localisation/localisation.rb

Class Method Summary collapse

Class Method Details

.translation(logical_name, language, table_name, file_name = nil, plurality = nil, lengthvariant = nil) ⇒ Object

description

Function to fetch a translation for a given logical name from the localisation DB

arguments

logical_name

String
 description: Logical name (LNAME) of the item to be translated. If prefix for User Information or Operator Data are used then the appropiate retrieve methods will be called
 example: "txt_button_ok"
Symbol
 description: Symbol form of the logical name (LNAME) of the item to be translated.
 example: :txt_button_ok

language

String
 description: Name of the language column to be used. This is normally the language postfix found on the .ts, .qm translation files. On .loc file postfix numbers are mapped to similar language codes according to standards in Symbian literature and others, check the localization.db implementation file for the full mapping.
 example: "en" or "es_416" or "en_us"

table_name

String
 description: Name of the translation table to use from the localisation DB
 example: "B10_1_week201042_loc"

file_name

String
 description: Optional FNAME search argument for the translation. The FNAME column stores the application name that the translation belongs to
 example: "calendar"

plurality

String
 description: Optional PLURALITY search argument for the translation
 example: "a" or "singular"

lengthvariant

String
 description: Optional LENGTHVAR search argument for the translation (1-9)
 example: "1"

returns

String

description: Translation matching the logical_name
example: "Ok"

Array

description: If multiple translations have been found for the search conditions an Array with all Strings be returned
example: ["Ok", "OK"]

exceptions

LanguageNotFoundError

description: In case language is not found

LogicalNameNotFoundError

description: In case no logical name is not found for current language

TableNotFoundError

description: If the table name argument is not valid

SqlError

description: In case there are problems with the database connectivity


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/tdriver/util/localisation/localisation.rb', line 309

def self.translation( logical_name, language, table_name, file_name = nil , plurality = nil, lengthvariant = nil )

	raise LogicalNameNotFoundError.new( "Logical name cannot be nil" ) if logical_name == nil
	raise LanguageNotFoundError.new( "Language cannot be nil" ) if language == nil
	raise TableNotFoundError.new( "Table name cannot be nil" ) if table_name == nil
	
	# Avoid system column names for language columns and user only downcase
	language = language.to_s.downcase
	if language.eql? "id" or language.eql? "fname" or language.eql? "lname" or language.eql? "plurality" or language.eql? "lengthvar" 
		language += "_"
	end
	
	db_type =  $parameters[ :localisation_db_type, nil ]
	host =  $parameters[ :localisation_server_ip ]
	username = $parameters[ :localisation_server_username ]
	password = $parameters[ :localisation_server_password ]
	database_name = $parameters[ :localisation_server_database_name ]
	
	db_connection = DBConnection.new(  db_type, host, database_name, username, password )
	
	query_string = "select `#{ language }` from #{ table_name } where lname = '#{ logical_name }'"
	query_string += " and `FNAME` = '#{ file_name }'" unless file_name.nil?
	query_string += " and `PLURALITY` = '#{ plurality }'" unless plurality.nil?
	query_string += " and `LENGTHVAR` = '#{ lengthvariant }'" unless lengthvariant.nil?
	query_string += " and `#{ language }` <> \'#MISSING\'"
	
	begin
		# Returns a uniform set of results as an array of rows, rows beeing an array of values ( Array<Array<String>> )
		result = MobyUtil::DBAccess.query( db_connection, query_string )
	rescue
		# if column referring to language is not found then raise error for language not found
		raise LanguageNotFoundError.new( "No language '#{ language }' found" ) unless $!.message.index( "Unknown column" ) == nil
		raise SqlError.new( $!.message )
	end

	# Return only the first column of the row or and array of the values of the first column if multiple rows have been found
	raise LogicalNameNotFoundError.new( "No translation found for logical name '#{ logical_name }' in language '#{ language }' with given plurality and lengthvariant." ) if ( result.empty?)
	if result.length > 1
		# Result is an Array of rows (Array<String>)! We want the first column of each row.
		result_array = Array.new
		result.each do |row|
			result_array << row[0]
		end
		return result_array
	else
		# Result is an Array of rows (Array<String>)! We want the first column of the first row.
		return result[0][0] 
	end

end

.upload_translation_file(file, table_name, db_connection = nil, column_names_map = {}, record_sql = false) ⇒ Object

description

Function upload translations in a given Qt Linguist translation file to the localisation DB

arguments

file

String
 description: Path to Qt Linguist translation file to upload. Both .ts and .qm files are allowed.
 example: "accessories_ar.ts"

table_name

String
 description: Name of the translation table to use form the localisation DB
 example: "B10_1_week201042_loc"

db_connection

MobyUtil::DBConnection
 description: A DBConnection object contains all the connection details required to connect to a SQL DB (mysql or sqlite)

example: “MobyUtil::DBConnection.new(‘mysql’, ‘192.168.0.1’, ‘tdriver_locale’, ‘username’, ‘password’)”

column_names_map

Hash
 description: Use this parameter to change the default language names. The default language postfix translation files (.ts, .qm or .loc) as keys and the desired column names as values
 example: {"en" => "en_GB"}

record_sql

Bool
 description: When this flag is set to true then 
 example: true

returns

throws

ArgumentError

description: When arguments provided are not valid

Exception

description: When its not possible to parse the file provided

Raises:

  • (ArgumentError)


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/tdriver/util/localisation/localisation.rb', line 398

def self.upload_translation_file( file, table_name, db_connection = nil, column_names_map = {}, record_sql = false)	
	raise ArgumentError.new("") if file.nil? or file.empty?
	raise ArgumentError.new("") if table_name.nil? or table_name.empty?

	# Get a connection to the DB
	if db_connection.nil? or !db_connection.kind_of? MobyUtil::DBConnection
		db_type =  $parameters[ :localisation_db_type ]
		host =  $parameters[ :localisation_server_ip ]
		database_name = $parameters[ :localisation_server_database_name ]
		username = $parameters[ :localisation_server_username ]
		password = $parameters[ :localisation_server_password ]
		
		db_connection = DBConnection.new(  db_type, host, database_name, username, password )
	end
    if file.match(/.*\.ts/) or file.match(/.*\.qm/)
      # Check File and convert to TS File if needed
      tsFile = MobyUtil::Localisation.convert_to_ts( file )
      raise Exception.new("Failed to convert #{file} to .ts") if tsFile == nil	
      # Collect data for INSERT query from TS File
      language, data = MobyUtil::Localisation.parse_ts_file( tsFile, column_names_map )
      raise Exception.new("Error while parsing #{file}.") if language == nil or data == ""
	elsif file.match(/.*\.loc/)
      language, data = MobyUtil::Localisation.parse_loc_file( file, column_names_map )
      raise Exception.new("Error while parsing #{file}. The file might have no translations.") if language.nil? or language.empty? or data.nil? or data.empty?
    end
    # Upload language data to DB for current language file
	MobyUtil::Localisation.upload_data( language, data, table_name, db_connection, record_sql )
end