'From etoys2.3 of 2 December 2007 [latest update: #1870] on 6 January 2008 at 12:24:43 pm'! "Change Set: TranslateGuide-tk Date: 6 January 2008 Author: Ted Kaehler Code for translating all text in a Guide to another language when the Guide is loaded from disk. This is just the code. It is not turned on. More work is needed on the speed."! !QuickGuideHolderMorph commentStamp: 'tk 12/7/2007 15:08' prior: 0! This is the Flap that holds the Guides. Shows one guide at a time, ! Object subclass: #TextDomainManager instanceVariableNames: '' classVariableNames: 'ClassCategories Classes DefaultDomain DomainInfos LoneClasses' poolDictionaries: '' category: 'System-Localization'! !TextDomainManager commentStamp: 'tk 1/4/2008 16:08' prior: 0! I manages mapping from class category to textdomain. Class variables: ClassCategories IdentityDictionary -- classCategory -> domainName Classes IdentityDictionary -- class name (a Symbol) -> domainName (a cache only!!) DefaultDomain String -- the default domain name DomainInfos Dictionary -- domainName -> a TextDomainInfo LoneClasses IdentityDictionary -- class name (a Symbol) -> domainName. For classes whose entire category are not all in the same domain (BookMorph and QuickGuideMorph) TextDomainManager registerCategoryPrefix: 'DrGeoII' domain: 'DrGeoII'. TextDomainManager unregisterDomain: 'DrGeoII'. TextDomainManager registerClass: #QuickGuideMorph domain: 'quickguides'. TextDomainManager registerClass: #QuickGuideHolderMorph domain: 'quickguides'. ! !QuickGuideHolderMorph methodsFor: 'file in/file out' stamp: 'tk 1/4/2008 10:48'! load "If 'guide.00x.pr' is present, take the one with the largest x. If only '.sexp.data.gz', then use it" | dir m fileName f unzipped zipped ours proj | self submorphs size > 0 ifTrue: [^ self]. dir _ (FileDirectory on: (Smalltalk imagePath)) directoryNamed: 'QuickGuides'. "#('xxx.001.pr' 'xxx.035.pr' 'xxx.sexp.data.gz') asSortedCollection ('xxx.001.pr' 'xxx.035.pr' 'xxx.sexp.data.gz')" ours _ dir fileNames select: [:fName | (fName beginsWith: guideName) and: [(fName endsWith: '.pr') or: [fName endsWith: '.sexp.data.gz']]]. ours _ ours asSortedCollection. ours size = 0 ifTrue: [^ self]. fileName _ ours size > 1 ifTrue: [ours at: (ours size - 1) "most recent .pr file"] ifFalse: [ours last "sexp"]. proj _ fileName endsWith: '.pr'. Cursor wait showWhile: [ proj ifFalse: [ unzipped _ WriteStream on: ByteArray new. f _ dir readOnlyFileNamed: fileName. zipped _ GZipReadStream on: f. unzipped nextPutAll: zipped contents. m _ BookMorph bookFromPagesInSISSFormat: (DataStream on: (ReadStream on: (unzipped contents))) next. f close]. proj ifTrue: [ m _ self loadPR: fileName dir: dir. m ifNil: [^ self]]. m position: 0@0. self position: 0@0. self extent: m extent. m setNamePropertyTo: guideName. m beSticky. self translateGuide: m. self addMorph: m. ]. ! ! !QuickGuideHolderMorph methodsFor: 'file in/file out' stamp: 'tk 1/6/2008 12:21'! translateGuide: guideBook "Look at the current language, and translate every string in the book. Does change the book. A new translation will happen every time the book is loaded from disk. Version on the disk is the master in English." true ifTrue: [^ self]. "turned off for now" guideBook allMorphsDo: [:mm | (mm isKindOf: TextMorph) ifTrue: [ mm wrapFlag: true. "want wrap to bounds" "What if user does not want this?" mm contents: mm contents string translated]].! ! !String methodsFor: 'translating' stamp: 'tk 1/4/2008 11:08'! translated "answer the receiver translated to the default language" | domain here | here _ thisContext sender receiver class. here == Text ifTrue: [here _ thisContext sender sender receiver class]. "ignore super call" domain := TextDomainManager domainForClass: here. "AA ifNil: [AA _ 45. self halt]." "only once for debug" "AA _ nil" ^ self translatedTo: LocaleID current inDomain: domain! ! !String methodsFor: 'translating' stamp: 'tk 1/6/2008 12:21'! translatedTo: localeID inDomain: aDomainName "answer the receiver translated to the given locale id in the textdomain" ^ NaturalLanguageTranslator translate: self toLocaleID: localeID inDomain: aDomainName! ! !Text methodsFor: 'converting' stamp: 'tk 12/23/2007 12:42'! translated ^ string translated! ! !TextDomainManager class methodsFor: 'accessing' stamp: 'tk 1/2/2008 17:27'! registerClass: className domain: aDomainName LoneClasses ifNil: [LoneClasses _ IdentityDictionary new]. LoneClasses at: className put: aDomainName. self refresh. "moves it to Classes" ! ! !TextDomainManager class methodsFor: 'class initialization' stamp: 'tk 1/4/2008 16:09'! initialize " TextDomainManager initialize " ClassCategories _ IdentityDictionary new. Classes _ IdentityDictionary new. DomainInfos _ Dictionary new. self defaultDomain: 'etoys'. self registerClass: #QuickGuideMorph domain: 'quickguides'. self registerClass: #QuickGuideHolderMorph domain: 'quickguides'. ! ! !TextDomainManager class methodsFor: 'private' stamp: 'tk 1/2/2008 17:28'! refresh ClassCategories _ IdentityDictionary new. Classes _ IdentityDictionary new. DomainInfos keysAndValuesDo: [:domainName :domainInfo | domainInfo matchedSystemCategories do: [:cat | ClassCategories at: cat ifPresent: [self error: 'category ', (cat asString) , ' belongs to multiple domains']. ClassCategories at: cat put: domainName. (SystemOrganization listAtCategoryNamed: cat ) do: [ :cls | Classes at: cls put: domainName. ] ] ]. Classes addAll: LoneClasses.! ! TextDomainManager initialize!