kmalakoff/knockback — Issue #8: Simplifying syntax for: {localizer: (value) -> return new kb.Localizer()}
Author: kmalakoff
Created: 2011-11-04 12:29:05+00:00
State: open

I think this is too verbose:

```coffeescript
viewModel = (model) ->
kb.observables(kb.locale_manager, {
label1: {key: 'view1.label1', localizer: (value) -> return new localizer_LocalizedString(value) }
label2: {key: 'view1.label2', localizer: (value) -> return new localizer_LocalizedString(value) }
label3: {key: 'view1.label3', localizer: (value) -> return new localizer_LocalizedString(value) }
label4: {key: 'view1.label4', localizer: (value) -> return new localizer_LocalizedString(value) }
}, this}

kb.observables(model, {
my_value1: {key: 'string_attribute', localizer: (value) -> return new localizer_LocalizedString(value)}
my_value2: {key: 'date_attribute1', localizer: (value) -> return new localizer_DateShort(value)}
my_value3: {key: 'date_attribute2', localizer: (value) -> return new localizer_DateTimeLong(value)}
}, this)
```
Originally, I had a hack that looked on the value for a factory function, but didn't deem that flexible or proper for a public release (plus it doesn't allow you to choose the format between localizer_DateShort and localizer_DateTimeLong.

How can we simplify this?

Option1: We could go the route of the lookup by type and giving the user an option to override the localizer. The above example would become:

```coffeescript
viewModel = (model) ->
kb.observables(kb.locale_manager, {
label1: {key: 'view1.label1' }
label2: {key: 'view1.label2' }
label3: {key: 'view1.label3' }
label4: {key: 'view1.label4' }
}, this}

kb.observables(model, {
my_value1: {key: 'string_attribute}
my_value2: {key: 'date_attribute1'}
my_value3: {key: 'date_attribute2', localizer: (value) -> return new localizer_DateTimeLong(value)}
}, this)
```
Although...I haven't packaged any localizers because I think that the user should implement localization however they like so they could do something like:

```coffeesscript
kb.addLocalizer((value) -> return (value instanceof LocalizedString) , localizer_LocalizedString)
```

Option2: use named localizers

```coffeescript
viewModel = (model) ->
kb.observables(kb.locale_manager, {
label1: {key: 'view1.label1', localizer: "LocalizedString" }
label2: {key: 'view1.label2', localizer: "LocalizedString" }
label3: {key: 'view1.label3', localizer: "LocalizedString" }
label4: {key: 'view1.label4', localizer: "LocalizedString" }
}, this}

kb.observables(model, {
my_value1: {key: 'string_attribute', localizer: "LocalizedString"}
my_value2: {key: 'date_attribute1', localizer: "DateShort"}
my_value3: {key: 'date_attribute2', localizer: "DateTimeLong"}
}, this)
```

The user would register them like:

```coffeesscript
kb.addLocalizer("LocalizedString", localizer_LocalizedString)
```

Option 3: Maybe a combination of Option1 and Option2?

Or maybe we should avoid a central registry of localizers? The main advantage of a central registry would be we could have a factory function like:

```coffeesscript
kb.localizer = (value, options, view_model) ->
(return new localizer_constructor(value, options, view_model) if checker(value) ) for checker, localizer_constructor of kb.localizers
return value # no localizer required
```

What is the best approach to reduce this verbosity?