#FEATURE by cg
class: ResourcePack
added:
#resourcePackContainingKey:
#superPackHierarchyDo:
comment/format in: #localAt:
changed: #at:ifAbsent:
class: ResourcePack class
changed: #processResourceLine:encoding:file:printErrorWith:for:keepUselessTranslations:
--- a/ResourcePack.st Fri Feb 15 00:16:46 2019 +0100
+++ b/ResourcePack.st Wed Feb 20 13:14:20 2019 +0100
@@ -778,12 +778,20 @@
ignoreTranslation ifFalse:[
oldValue := aResourcePack at:name ifAbsent:nil.
oldValue notNil ifTrue:[
+ |fromInfo oldPack|
+
+ fromInfo := ''.
+ oldPack := aResourcePack resourcePackContainingKey:name.
+ oldPack ~~ aResourcePack ifTrue:[
+ fromInfo := ' (from ',oldPack packsFileName,')'
+ ].
+
oldValue ~= value ifTrue:[
printError value:('conflicting resource: "' , name , '"').
- printError value:(' oldValue: ' , oldValue printString).
- printError value:(' newValue: ' , value printString).
+ printError value:(' oldValue: "%1"%2' bindWith:oldValue with:fromInfo).
+ printError value:(' newValue: "%1"' bindWith:value).
] ifFalse:[
- printError value:('duplicate resource: "' , name , '"').
+ printError value:('duplicate resource: "%1"%2' bindWith:name with:fromInfo).
].
].
indirect ifTrue:[
@@ -803,7 +811,7 @@
]
"Modified: / 06-02-2014 / 15:33:03 / cg"
- "Modified: / 23-10-2018 / 15:43:38 / Claus Gittinger"
+ "Modified: / 20-02-2019 / 13:00:47 / Claus Gittinger"
!
resourceFileEntryFor:keyString to:nationalString
@@ -916,12 +924,14 @@
!
at:aKey ifAbsent:defaultValue
- "translate a string; search here, in my projects pack and in my superpack(s)"
+ "translate a string;
+ search here, in my projects pack and in my superpack(s)"
- |val pack projectPack alreadySearched toSearch more|
+ |val|
aKey isNil ifTrue:[ ^ defaultValue value ].
+ "/ optional statistics on key usage
usedKeys notNil ifTrue:[usedKeys add:aKey].
val := self localAt:aKey.
@@ -931,84 +941,22 @@
(val := cache at:aKey ifAbsent:nil) notNil ifTrue:[
^ val
].
-
- superPacks notEmptyOrNil ifTrue:[
- alreadySearched := Set new.
- toSearch := OrderedCollection withAll:superPacks.
- [toSearch notEmpty] whileTrue:[
- pack := toSearch removeFirst.
- (alreadySearched includes:pack packsFileName) ifFalse:[
- val := pack localAt:aKey.
- val notNil ifTrue:[
- cache at:aKey put:val.
- ^ val
- ].
- alreadySearched add:pack packsFileName.
- (more := pack superPacks) notEmptyOrNil ifTrue:[
- self assert:(more conform:[:each | each isNil or:[pack isKindOf:ResourcePack]]).
- toSearch addAll:more.
- ].
- ].
+
+ self superPackHierarchyDo:[:eachPack |
+ val := eachPack localAt:aKey.
+ val notNil ifTrue:[
+ cache at:aKey put:val.
+ ^ val
].
].
^ defaultValue value.
-"/ (projectPack := self projectPack) notNil ifTrue:[
-"/ val := projectPack localAt:aKey.
-"/ val notNil ifTrue:[
-"/ cache at:aKey put:val.
-"/ ^ val
-"/ ].
-"/ ].
-"/ alreadySearched := IdentitySet new.
-"/ projectPack notNil ifTrue:[ alreadySearched add:projectPack ].
-"/
-"/ pack := self superPack.
-"/ [ pack notNil ] whileTrue:[
-"/ val := pack localAt:aKey.
-"/ val notNil ifTrue:[
-"/ cache at:aKey put:val.
-"/ ^ val
-"/ ].
-"/
-"/ (projectPack := pack projectPack) notNil ifTrue:[
-"/ (alreadySearched includes:projectPack) ifFalse:[
-"/ val := projectPack localAt:aKey.
-"/ val notNil ifTrue:[
-"/ cache at:aKey put:val.
-"/ ^ val
-"/ ].
-"/ alreadySearched add:projectPack.
-"/ ].
-"/ ].
-"/ pack := pack superPack
-"/ ].
-"/
-"/ alreadySearched copy do:[:projectPack |
-"/ |p|
-"/
-"/ p := projectPack superPack.
-"/ [p notNil and:[(alreadySearched includes:p) not]] whileTrue:[
-"/ val := p localAt:aKey.
-"/ val notNil ifTrue:[
-"/ cache at:aKey put:val.
-"/ ^ val
-"/ ].
-"/ alreadySearched add:p.
-"/ p := p superPack.
-"/ ].
-"/ ].
-"/
-"/ val := defaultValue value.
-"/ cache at:aKey put:val.
-"/ ^ val
-
"Modified: / 18-09-2006 / 18:50:52 / cg"
- "Modified: / 26-10-2018 / 21:16:15 / Claus Gittinger"
+ "Modified: / 20-02-2019 / 12:51:01 / Claus Gittinger"
!
localAt:aKey
- "translate a string.
+ "translate a string from the local resourcePack; return nil if there is no xlation.
Some special 'intelligence' has been added:
if no value for aKey is found,
lookup aKey with first character caseChanged and change the result's first characters case.
@@ -1155,6 +1103,7 @@
"Modified: / 05-08-2010 / 16:52:32 / sr"
"Modified: / 27-11-2017 / 15:33:48 / cg"
"Modified: / 04-11-2018 / 12:33:50 / Claus Gittinger"
+ "Modified (comment): / 20-02-2019 / 12:47:16 / Claus Gittinger"
!
name:aKey default:default
@@ -1166,6 +1115,32 @@
^ self at:aKey ifAbsent:default
!
+resourcePackContainingKey:aKey
+ "returns the resolving resourcePack or nil."
+
+ |val|
+
+ aKey isNil ifTrue:[^ nil ].
+
+ val := self localAt:aKey.
+ val notNil ifTrue:[^ self].
+
+ self superPackHierarchyDo:[:eachPack |
+ val := eachPack localAt:aKey.
+ val notNil ifTrue:[
+ ^ eachPack.
+ ].
+ ].
+ ^ nil.
+
+ "
+ Workspace classResources resourcePackContainingKey:'Cancel'
+ WorkspaceApplication classResources resourcePackContainingKey:'Cancel'
+ "
+
+ "Created: / 20-02-2019 / 12:46:32 / Claus Gittinger"
+!
+
string:s
"translate (retrieve) a string - if not present, return s"
@@ -1456,6 +1431,32 @@
"Modified: / 07-09-2018 / 12:44:06 / Claus Gittinger"
!
+superPackHierarchyDo:aBlock
+ "evaluate aBlock for all of the searched super packs;
+ that is my project's pack and my superpack(s)"
+
+ |pack alreadySearched toSearch more|
+
+ superPacks notEmptyOrNil ifTrue:[
+ alreadySearched := Set new.
+ toSearch := OrderedCollection withAll:superPacks.
+ [toSearch notEmpty] whileTrue:[
+ pack := toSearch removeFirst.
+ (alreadySearched includes:pack packsFileName) ifFalse:[
+ aBlock value:pack.
+
+ alreadySearched add:pack packsFileName.
+ (more := pack superPacks) notEmptyOrNil ifTrue:[
+ self assert:(more conform:[:each | each isNil or:[pack isKindOf:ResourcePack]]).
+ toSearch addAll:more.
+ ].
+ ].
+ ].
+ ].
+
+ "Created: / 20-02-2019 / 12:49:56 / Claus Gittinger"
+!
+
whichPackIncludesKey:aKey
"for debugging: return the pack (alogn the super-pack chain), which
has a translation for a string"