'From Pharo-1.1-11411 of 17 July 2010 [Latest update: #11411] on 10 September 2010 at 3:24:56 pm'!
Object subclass: #K3LanguagePack
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation'!

!K3LanguagePack commentStamp: '<historical>' prior: 0!
K3LanguagePack is superclass of classes containing key based translations. To use K3LanguagePack based translations one should subclass it in the following way:

K3LanguagePack 
	SpesificLanguagePackForMyProgram
		SpesificLanguagePackForMyProgram_fi
		SpesificLanguagePackForMyProgram_en
	SpesificLanguagePackForMyOtherProgram
		SpesificLanguagePackForMyOtherProgram_fi
		SpesificLanguagePackForMyOtherProgram_en
		
To get translation objects use the general class name. For example:
	translation := SpesificLanguagePackForMyProgram translationFor: #abcd
	
To get the translated text call:
	translation translate: 'fi'.
		!

K3LanguagePack subclass: #K3LanguagePackForTesting
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation-Test'!
K3LanguagePackForTesting subclass: #K3LanguagePackForTesting_en
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation-Test'!
K3LanguagePackForTesting subclass: #K3LanguagePackForTesting_fi
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation-Test'!
Error subclass: #K3LanguagePackNotFound
	instanceVariableNames: 'locale languagePack'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation'!
MAValidationDecoration subclass: #K3TranslatedValidationDecorator
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation-Magritte-Seaside'!

!K3TranslatedValidationDecorator commentStamp: '<historical>' prior: 0!
This is simple override for MAValidationDecoration to enable translation of error messages returned by Magritte validators.
Decorator only calls render for both the label and the error message.

Maybe this behaviour could be changed to the MAValidationDecoration itself...!

Object subclass: #K3Translation
	instanceVariableNames: 'translation item arguments'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation'!

!K3Translation commentStamp: '<historical>' prior: 0!
K3Translation represents something that can be translated using spesific procedure. That procedure is stored in translation instance variable.

Instance Variables:
	translation	<Block> Represents the actual translation logic. Block must take 3 parameters (language, item, argumets) and return the translated string.
	item	<Object> Something that translation block understands. Marks the meaning of translation.
	arguments	<Collection of: Object> Collection of arguments passed to translation. When doing the translation value message is send to every object of the collection and returned value is passed to translation block.!

TestCase subclass: #K3TranslationTest
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation-Test'!
TestCase subclass: #KorppiLanguagePackTest
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Korppi-Translation-Test'!

!K3LanguagePack methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 12:05'!
doesNotUnderstand: aMessage
	^self notTranslated.! !

!K3LanguagePack methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 10:50'!
notTranslated.
	"Because this is language pack subclass responsibility means that text is just not translated yet.
	This could be handled using exception and by defaul ask translation.
	Now we just return something.
	"
	^'not translated'! !


!K3LanguagePack class methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/10/2010 14:23'!
for: locale
	| actualClassName wantedClassName|
	wantedClassName := (self name,'_',locale).
	actualClassName := Smalltalk at: wantedClassName asSymbol ifAbsent:
	[K3LanguagePackNotFound ofType: self forLocale: locale].
	^actualClassName new.! !

!K3LanguagePack class methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/10/2010 14:51'!
locale
	"Returns the locale of the"
	^(self name subStrings: '_') at:2 ifAbsent:[Error signal: 'This class is not translated language pack.'].! !

!K3LanguagePack class methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/10/2010 15:13'!
translationFor: aKey
	"TODO: Arguments are not used currently."
	^K3Translation new item: aKey; translation: [:language :key :arguments |
		(self for: language) perform: key
		].
	! !


!K3LanguagePackForTesting methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/8/2010 11:42'!
testTranslation
	self subclassResponsibility.! !


!K3LanguagePackForTesting_en methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/8/2010 11:58'!
testTranslation 
	^'in English'.! !


!K3LanguagePackForTesting_fi methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/8/2010 11:58'!
testTranslation 
	^'suomeksi'! !


!K3LanguagePackNotFound methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/8/2010 12:40'!
locale: aByteString ofLanguegPack: aClass 
	locale := aByteString.
	languagePack := aClass.
	self messageText: 'No locale for ',languagePack name.	! !


!K3LanguagePackNotFound class methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/8/2010 12:41'!
ofType: aClass forLocale: aByteString 
	(self new) locale: aByteString ofLanguegPack: aClass; signal.! !


!K3TranslatedValidationDecorator methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 10:43'!
renderErrorsOn: html
	(html unorderedList)
		class: 'errors';
		with: [ self errors do: [ :e | html listItem with: [ html render: e tag label; text:': ';render: e messageText ] ] ]! !


!K3Translation methodsFor: 'accessing' stamp: 'PanuSuominen 9/9/2010 08:58'!
arguments
	^ arguments! !

!K3Translation methodsFor: 'accessing' stamp: 'PanuSuominen 9/9/2010 08:58'!
item
	^ item! !

!K3Translation methodsFor: 'accessing' stamp: 'PanuSuominen 9/9/2010 09:02'!
translation
	^translation! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 08:48'!
arguments: anArray
	"Sets the arguments that are passed to translation block. Arguments can be
	blocks or objects. Blocks are evaluated every time before translation mehtod.
	"
	arguments := anArray.! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 10:16'!
do: aBlock
	self asString do: aBlock.! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 09:04'!
evaluatedArguments
	"Returns array of evaluated arguments. Blocks in arguments are
	evaluated and regular objects simply returned."

	^ self arguments isNil
		ifTrue: [ Array new ]
		ifFalse: [ 
			arguments
				collect: [ :i | 	i value ]]! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 09:30'!
isEmpty
	^false.! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 08:40'!
item: anObject
	"Item that represents the data to be translated. Item should be understood by
	the block set using translation: method."
	item := anObject.! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/10/2010 15:02'!
printOn: aStream
	"
	Prints this translation as is. Currenlty it can not determine language so only item is printed.
	Should think some way to lure language here.
	"
	aStream nextPutAll: self item.! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/10/2010 15:02'!
renderOn: canvas
	"Method for enabling seaside translation. To make this work session should contain language method."
	canvas text: (self translate: (canvas session language)).! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 09:19'!
translate: language
	"Performs translation using translation block."

	^self translation value: language value: self item value value: self evaluatedArguments.! !

!K3Translation methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/9/2010 09:32'!
translation: aBlock
	"Receives block that can have two parameters.
	First parameter is the language.
	Second parameter is the item to be translated.
	Third is array of parameters.
	"
	aBlock isBlock ifFalse: [Error signal: 'Currently I only understand blocks!!'].
	translation := aBlock.! !


!K3TranslationTest methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/10/2010 15:06'!
testArgumentBlock
	|t a|
	a := 1.
	t := K3Translation new translation: [:l :s :p | 
		self should: [2 = p size] description: 'Wrong size argument array. Size was ', p size asString.
		p first asString, p second asString
		]; item: #string.
	t arguments: {[a]. 2}.
	self should: ['12' = (t translate: 'fi')].
	a:=2.
	self should: ['22' = (t translate: 'fi')].! !

!K3TranslationTest methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/10/2010 15:07'!
testTranslationBlock
	|t|
	t := K3Translation new translation: [:l :s :p|
		self should:[l = 'en'].
		 s asString]; item: #string.
	self should: ['string' = (t translate: 'en')].! !


!KorppiLanguagePackTest methodsFor: 'as yet unclassified' stamp: 'PanuSuominen 9/10/2010 14:43'!
testTranslationFor
	| t |
	t := K3LanguagePackForTesting translationFor: #testTranslation.
	self should:['suomeksi' = (t translate: 'fi')].
	self should:['in English' = (t translate: 'en')].! !

