help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] Iliad: Widget class initialization


From: Bèrto ëd Sèra
Subject: [Help-smalltalk] Iliad: Widget class initialization
Date: Thu, 30 Jul 2009 14:04:26 +0300

Hi!

I have a working rough prototype of the localization mechanism, but I
have a problem, the Widget class does not seem to be initialized,
which would be needed, since I'm using a class variable in
LocalizedWidget. The basic code is here at the bottom. I'm not
connected to the DB yet, so instead of storing the real UUIDs for data
retrieval I store a string and copy it over, just to verify that
things work. And they do, if you inspect a snippet like:

Ambaradan.Login initialize.
Ambaradan.Login new initialize localize: #register.

But on executing the app I get an
UndefinedObject(Object)>>doesNotUnderstand: #keysAndValuesDo:
(AnsiExcept.st:1556) error, which seems a clear symptom of a missing
class initialization. Is there a way I can force the initialization
from my bottom level (I can't seem to remember anything about it), or
should one ad it to the core code?

BTW, once it works I'll move this stuff to a more abstract system,
with the Ambaradan DB being just a possible case. I myself hate to be
forced to use this or that product, so I guess it is important to
deliver a generic system that can be used to plugin "some way of
localizing a string", rather than committing marriage to any solution.
The basic thing I like with this system is that messages should be
declared inside the widget that uses them, so we use just as much cash
as needed, and a coder does not have to walk a possibly very huge
table any time he needs to check something.

Berto

==================================================

Namespace current: Ambaradan [
  Iliad.Widget subclass: LocalizedWidget [
    <category: 'Ambaradan'>
    <comment: 'version 0.6.0.
               This abstract class implements the localization
mechanism for all widgets.
               A class variable holds a LookupTable of GUI messages ->
UUIDs, where the
               UUIDs are the keys to retrieve a profile from the
Ambaradan database.
               Class variables:
                 GuiStrings: LookupTable of associations in the form
aSelector -> aUuid, where
                             the Uuid can be used to identify all
existing translations of the
                             term from the distributed Ambaradan DB.
               Instance variables:
                 languageChoice:   an association aUuid -> aUuid,
describing a language/script couple.
                                   It is kept and checkd on every
operation, if it changes (i.e., the
                                   user has selected another GUI
language) localization must be
                                   recomputed, otherwise we use the
cached version.
                 localizedStrings: LookupTable of associations in the
form aSelector -> text, compiled
                                   evaluating the general message
UUIDs stored at class level against
                                   the current user.' >
    GuiStrings := nil.
    | languageChoice localizedStrings |

    LocalizedWidget class [
      initialize [
        "start the LookupTable where the uuids of the text strings
         used by the GUI will be stored. Subclasses take care of
         filing in their own set. This class variable is used to
         produce a localized instance variable (also a LookupTable)
         that instead of the UUID will contain and cache the actual
         text to be rendered."
        <category: 'initialization'>
        super initialize.
        GuiStrings := LookupTable new.
      ]

      guiAdd: anAssociation [
        "Register an association selector -> UUID. This is used by
         the subclasses to register a translatable selector. The
         associated UUID is used to retrieve the message from the DB."
        <category: 'accessing'>
        GuiStrings add: anAssociation.
      ]

      isAbstract [
        <category: 'testing'>
        ^true
      ]

    ]

    initialize [
        "Localize all entries in the class variable GuiStrings LookupTable
         according to the current GUI language."
        <category: 'initialization'>
        super initialize.
        languageChoice   := OrderedCollection new.
        languageChoice addAll: self currentGuiLanguage.
        localizedStrings := self guiLocalize.
    ]

    localize: aStringSelector [
      <category: 'localization'>
      "Return the current localization for a string."
      ^localizedStrings at: aStringSelector.
    ]

    guiLocalize [
      <category: 'localization'>
      "Compile a LookupTable of translated messages.
       This prototype version simply does a copy, the final version
       will load data from the database."
      | tmpLocalizedStrings |
      tmpLocalizedStrings := LookupTable new.
      GuiStrings keysAndValuesDo: [:key :value | tmpLocalizedStrings
at: key put: value].
      ^tmpLocalizedStrings.
    ]

    defaultLanguage [
      <category: 'session data'>
      "English -> English alphabet. Other values can be found
       consulting the ambaradan dictionary."
      | defaultCollection |
      defaultCollection := OrderedCollection new.
      defaultCollection add: 'd3cb9b56-7929-11de-b1a0-1b6f382237eb' ->
'd3be1be8-7929-11de-9453-23ab1b1f0512'.
      ^defaultCollection.
    ]

    currentGuiLanguage [
      "temporary hack, here will be loaded the values from the user's
preferences. They are used to
       produce the actual localization of the GUI strings."
      <category: 'session data'>
      ^nil ifNil: [^self defaultLanguage].
    ]

  ]
]

============================

and an example of derived widget (PageTemplate is a subclass of
Ambaradan.LocalizedWidget)

Namespace current: Ambaradan [
  Ambaradan.PageTemplate subclass: Login [


    Login class [

      initialize [
        "start the LookupTable where the uuids of the text strings
         used by the GUI will be stored. Subclasses take care of
         filing in their own set. This class variable is used to
         produce a localized instance variable (also a LookupTable)
         that instead of the UUID will contain and cache the actual
         text to be rendered."
        <category: 'initialization'>
        super initialize.
        self
          guiAdd: #username     -> 'username';
          guiAdd: #password     -> 'password';
          guiAdd: #loginButton  -> 'login';
          guiAdd: #register     -> 'Register new user';
          guiAdd: #lostPassword -> 'I lost my password!';
          guiAdd: #preferences  -> 'My preferences'.
      ]
    ]

    mainContent [
        ^[ :e |
            e h1: self localize: #register.
        ]
    ]

  ]
]
-- 
==============================
Constitution du 24 juin 1793 - Article 35. - Quand le gouvernement
viole les droits du peuple, l'insurrection est, pour le peuple et pour
chaque portion du peuple, le plus sacré des droits et le plus
indispensable des devoirs.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]