Skip to content

Commit 53716f6

Browse files
committed
Internationalization docs
1 parent 09488ad commit 53716f6

File tree

5 files changed

+113
-412
lines changed

5 files changed

+113
-412
lines changed

docs/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ General guides to using REST framework.
199199
* [3.1 Announcement][3.1-announcement]
200200
* [Kickstarter Announcement][kickstarter-announcement]
201201
* [Release Notes][release-notes]
202-
* [Credits][credits]
203202

204203
## Development
205204

@@ -314,7 +313,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
314313
[3.1-announcement]: topics/3.1-announcement.md
315314
[kickstarter-announcement]: topics/kickstarter-announcement.md
316315
[release-notes]: topics/release-notes.md
317-
[credits]: topics/credits.md
318316

319317
[tox]: http://testrun.org/tox/latest/
320318

docs/topics/3.1-announcement.md

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The pagination API has been improved, making it both easier to use, and more pow
1010

1111
Until now, there has only been a single built-in pagination style in REST framework. We now have page, limit/offset and cursor based schemes included by default.
1212

13-
The cursor based pagination scheme is particularly smart, and is a better approach for clients iterating through large or frequently changing result sets. The scheme supports paging against non-unique indexes, by using both cursor and limit/offset information. Credit to David Cramer for [this blog post](http://cramer.io/2011/03/08/building-cursors-for-the-disqus-api/) on the subject.
13+
The cursor based pagination scheme is particularly smart, and is a better approach for clients iterating through large or frequently changing result sets. The scheme supports paging against non-unique indexes, by using both cursor and limit/offset information. It also allows for both forward and reverse cursor pagination. Much credit goes to David Cramer for [this blog post](http://cramer.io/2011/03/08/building-cursors-for-the-disqus-api/) on the subject.
1414

1515
#### Pagination controls in the browsable API.
1616

@@ -34,22 +34,85 @@ We've made it easier to build versioned APIs. Built-in schemes for versioning in
3434

3535
When using a URL based scheme, hyperlinked serializers will resolve relationships to the same API version as used on the incoming request.
3636

37-
**TODO**: Example.
37+
For example, when using `NamespaceVersioning`, and the following hyperlinked serializer:
38+
39+
class AccountsSerializer(serializer.HyperlinkedModelSerializer):
40+
class Meta:
41+
model = Accounts
42+
fields = ('account_name', 'users')
43+
44+
The output representation would match the version used on the incoming request. Like so:
45+
46+
GET http://example.org/v2/accounts/10 # Version 'v2'
47+
48+
{
49+
"account_name": "europa",
50+
"users": [
51+
"http://example.org/v2/users/12", # Version 'v2'
52+
"http://example.org/v2/users/54",
53+
"http://example.org/v2/users/87"
54+
]
55+
}
3856

3957
## Internationalization
4058

4159
REST framework now includes a built-in set of translations, and supports internationalized error responses. This allows you to either change the default language, or to allow clients to specify the language via the `Accept-Language` header.
4260

43-
**TODO**: Example.
61+
You can change the default language by using the standard Django `LANGUAGE_CODE` setting:
62+
63+
LANGUAGE_CODE = "es-es"
64+
65+
You can turn on per-request language requests by adding `LocalMiddleware` to your `MIDDLEWARE_CLASSES` setting:
66+
67+
MIDDLEWARE_CLASSES = [
68+
...
69+
'django.middleware.locale.LocaleMiddleware'
70+
]
71+
72+
When per-request internationalization is enabled, client requests will respect the `Accept-Language` header where possible. For example, let's make a request for an unsupported media type:
73+
74+
**Request**
75+
76+
GET /api/users HTTP/1.1
77+
Accept: application/xml
78+
Accept-Language: es-es
79+
Host: example.org
80+
81+
**Response**
4482

45-
**TODO**: Credit.
83+
HTTP/1.0 406 NOT ACCEPTABLE
84+
85+
{
86+
"detail": "No se ha podido satisfacer la solicitud de cabecera de Accept."
87+
}
88+
89+
Note that the structure of the error responses is still the same. We still have a `details` key in the response. If needed you can modify this behavior too, by using a [custom exception handler][custom-exception-handler].
90+
91+
We include built-in translations both for standard exception cases, and for serializer validation errors.
92+
93+
The full list of supported languages can be found on our [Transifex project page](https://www.transifex.com/projects/p/django-rest-framework/).
94+
95+
If you only wish to support a subset of the supported languages, use Django's standard `LANGUAGES` setting:
96+
97+
LANGUAGES = [
98+
('de', _('German')),
99+
('en', _('English')),
100+
]
101+
102+
For more details, see the [internationalization documentation](internationalization.md).
103+
104+
Many thanks to [Craig Blaszczyk](https://github.com/jakul) for helping push this through.
46105

47106
## New field types
48107

49108
Django 1.8's new `ArrayField`, `HStoreField` and `UUIDField` are now all fully supported.
50109

51110
This work also means that we now have both `serializers.DictField()`, and `serializers.ListField()` types, allowing you to express and validate a wider set of representations.
52111

112+
If you're building a new 1.8 project, then you should probably consider using `UUIDField` as the primary keys for all your models. This style will work automatically with hyperlinked serializers, returning URLs in the following style:
113+
114+
http://example.org/api/purchases/9b1a433f-e90d-4948-848b-300fdc26365d
115+
53116
## ModelSerializer API
54117

55118
The serializer redesign in 3.0 did not include any public API for modifying how ModelSerializer classes automatically generate a set of fields from a given mode class. We've now re-introduced an API for this, allowing you to create new ModelSerializer base classes that behave differently, such as using a different default style for relationships.
@@ -85,6 +148,8 @@ And modify your settings, like so:
85148
]
86149
}
87150

151+
Thanks go to the latest member of our maintenance team, [José Padilla](https://github.com/jpadilla/), for handling this work and taking on ownership of these packages.
152+
88153
# What's next?
89154

90155
The next focus will be on HTML renderings of API output and will include:
@@ -93,4 +158,6 @@ The next focus will be on HTML renderings of API output and will include:
93158
* Filtering controls built-in to the browsable API.
94159
* An alternative admin-style interface.
95160

96-
This will either be made as a single 3.2 release, or split across two separate releases, with the HTML forms and filter controls coming in 3.2, and the admin-style interface coming in a 3.3 release.
161+
This will either be made as a single 3.2 release, or split across two separate releases, with the HTML forms and filter controls coming in 3.2, and the admin-style interface coming in a 3.3 release.
162+
163+
[custom-exception-handler]: ../api-guide/exceptions.md#custom-exception-handling

0 commit comments

Comments
 (0)