Skip to content

Commit

Permalink
docs(README): update README
Browse files Browse the repository at this point in the history
  • Loading branch information
velrest committed Aug 3, 2020
1 parent 5609651 commit b514e6c
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
ember-localized-model
==============================================================================

[Short description of the addon.]
Handle mutli-lang model fields where your JSON:API returns a dictionary / object
with all languages present.

__Example:__
```
{
id: "1",
type: "books",
attributes: {
name: {
"de": "Der Name des Windes",
"en": "The name of the wind"
}
}
}
```

Compatibility
------------------------------------------------------------------------------
Expand All @@ -23,8 +37,52 @@ ember install ember-localized-model
Usage
------------------------------------------------------------------------------

[Longer description of how to use the addon in apps.]
#### First steps

> You need `ember-intl` for this addon to work: `ember install ember-intl`
To use this addon, first you need to import and extend your model from `LocalizedModel`:
```js
import { LocalizedModel } from "ember-localized-model";

export default class YourModel extends LocalizedModel {}
```

To serialize the multi-lang field correctly you also need to add a serializer:
```js
import { LocalizedSerializer } from "ember-localized-model";

export default class YourModelSerializer extends LocalizedSerializer {}
```
Once this is done, you can start adding your localized field via the `@localizedAttr` decorator:
```js
import { LocalizedModel, localizedAttr } from "ember-localized-model";

export default class YourModel extends LocalizedModel {
@localizedAttr firstName;
@localizedAttr lastName;
}
```

#### Accessing the fields value
To read the model field you can simply use `yourModel.firstName`.

The locale the field displays depends on your `intl.primaryLocale`. You can
change your `primaryLocale` to display the model field in another locale.

If you want to switch locale for only one specific model, you can set
`yourModel.localizedFieldLocale` to the desired locale.

If you want to access the raw data as sent by the backend, you can use
`yourModel.getUnlocalizedField("firstName")`. This will return the raw data.

For example:
```
{
"de": "Johan",
"en": "John"
}
```

Contributing
------------------------------------------------------------------------------
Expand All @@ -35,4 +93,4 @@ See the [Contributing](CONTRIBUTING.md) guide for details.
License
------------------------------------------------------------------------------

This project is licensed under the [MIT License](LICENSE.md).
This project is licensed under the [GPL-3.0 License](LICENSE).

0 comments on commit b514e6c

Please sign in to comment.