[Newbies] Re: Global string search and replace in an image

H. Hirzel hannes.hirzel at gmail.com
Tue Sep 11 13:25:47 UTC 2012


On 9/11/12, Andy Burnett <andy.burnett at knowinnovation.com> wrote:
> I thought Andy was talking about source code.
>>
>> Personally, if I have to rename a method, I search for all senders and
>> fix
>> them with copy&paste. Same for class renames, inst var renames, etc.
>> There
>> just are not that many occurrences, so fixing each one individually is
>> quick, plus I get to verify that the change is indeed what I wanted.
>>
>> There is a tool to automate a lot of this called the Refactoring Browser,
>> which many developers like, but I don't even have it in my image.
>>
>
> Thanks very much to both of you.
>
> Bert, you are quite right, I was talking about changing strings in source
> code - sorry, I should have made that clear.
>
> The problem is that we I am dealing with an image where a number of strings
> have been hard coded into various methods.

This is surely not good as it is difficult to maintain, i.e.
translation of the interface into another language is not easy
possible.

What I was looking for was a
> global - source code - search and replace.  Does that exist?  Or, is it
> possible to FileOut the entire source tree, do a search and replace
> externally, and then file it in again. I have tried this in the past, but
> the image never seems to work properly afterwards.

No I would do the refactoring within Squeak.


> Clearly, the correct way to solve the problem is to abstract the hard coded
> text into another object - or maybe a global variable?

Yes, a simple thing you can do is to shift the strings to the class
side and collect them there in a method protocol.

So in an instance method you access the string as

     self class myHelpStringHowToAchieveThis

and on the class side you have a method protocol labeled 'string
constants'. It contains for this case the method

myHelpStringHowToAchieveThis

	^'To achieve x you have to do this and that.'



The next step is as you write to have a global string dictionary.  For
example MyStrings.

You avoid hard coding the link to this dictionary in many methods you
have to access the dictionary indirectly.

So you access it with something like this

(Smalltalk at: #HHMyTextStrings) at: 'myHelpStringHowToAchieveThis'


The first part
(Smalltalk at: #HHMyTextStrings)
gives you the dictionary (a global variable) HHMyTextStrings without
hard coding a dependency on it during loading of the code.

then
dict at: 'myHelpStringHowToAchieveThis'
is a regular access to a string in a dictionary which keeps strings.

Note:

Instead of referencing a dictionary with HHMyTextStrings the object
might be more complex.

It could for example understand the message

HHMyTextStrings setDefaultLanguage: #French

and then serve the strings in French.

I hope this makes sense.

--Hannes




Perhaps I will just
> have to bite the bullet.  This would make it far easier for
> internationalisation etc.  Actually, are there any packages to help with
> internationalisation?
>
> Cheers
> Andy
>


More information about the Beginners mailing list