Meant to do this for a while as it was irritating me (both the lack of
multilingual support and the "" option when a default is specified).

Pretty much a copy-paste from sqlhtml.OptionsWidget.widget method with
2 improvements (as per the title):

1) The options get translated using the facility provided by T(string)

2) If you specify a default value for the field, the user-confusing ""
doesn't appear in the option-list.

Had to manually add a small bit of code to auto-select the default (I
guess it was implemented in some other part of the code for
OptionsWidget and was lost in the copy-paste).

Not sure if other options I'm not using get lost in the translation,
but it works well with my stuff so far.

If someone else posted a similar solution already, sorry for spamming.

The code:

def MultilingualOptionsWidget(field, value):
    default = dict(
        value=value,
        )
    attr = dict(
            _id = '%s_%s' % (field._tablename, field.name),
            _class = isinstance(field.type,str) and field.type or
None,
            _name = field.name,
            requires = field.requires,
            )

    requires = field.requires
    if not isinstance(requires, (list, tuple)):
        requires = [requires]
    if requires:
        if hasattr(requires[0], 'options'):
            options = requires[0].options()
            if field.default != None:
                options = options[1:]
        else:
            raise SyntaxError, 'widget cannot determine options of %s'
\
                % field
    opts = [(OPTION(T(v), _value=k) if k!=field.default else
OPTION(T(v), _value=k, _selected='selected')) for (k, v) in options]

    return SELECT(*opts, **attr)

Reply via email to