From 309b5d264166e07510d6cbc54d681268d07957aa Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Fri, 2 Jan 2015 11:07:35 +0000 Subject: [PATCH 01/21] instructions on how to translate REST framework error messages --- docs/topics/internationalisation.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 docs/topics/internationalisation.md diff --git a/docs/topics/internationalisation.md b/docs/topics/internationalisation.md new file mode 100644 index 000000000..01f968915 --- /dev/null +++ b/docs/topics/internationalisation.md @@ -0,0 +1,34 @@ +# Internationalisation +REST framework ships with translatable error messages. You can make these appear in your language enabling [Django's standard translation mechanisms](https://docs.djangoproject.com/en/1.7/topics/i18n/translation) and by translating the messages into your language. + +## How to translate REST Framework errors + + +This guide assumes you are already familiar with how to translate a Django app. If you're not, start by reading [Django's translation docs](https://docs.djangoproject.com/en/1.7/topics/i18n/translation). + + +#### To translate REST framework error messages: + +1. Pick an app where you want the translations to be, for example `myapp` + +2. Add a symlink from that app to the installed `rest_framework` + ``` + ln -s /home/user/.virtualenvs/myproject/lib/python2.7/site-packages/rest_framework/ rest_framework + ``` + + To find out where `rest_framework` is installed, run + + ``` + python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" + ``` + +3. Run Django's `makemessages` command in the normal way, but add the `--symlink` option. For example, if you want to translate into Brazilian Portuguese you would run + ``` + manage.py makemessages --symlink -l pt_BR + ``` + +4. Translate the `django.po` file which is created as normal. This will be in the folder `myapp/locale/pt_BR/LC_MESSAGES`. + +5. Run `manage.py compilemessages` as normal + +6. Restart your server From 7ad7dd6a4292157ed5bcbaacb60b6ccc93fcf201 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 24 Dec 2014 18:26:17 +0000 Subject: [PATCH 02/21] match DRF style guide --- docs/topics/internationalisation.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/topics/internationalisation.md b/docs/topics/internationalisation.md index 01f968915..552fdd273 100644 --- a/docs/topics/internationalisation.md +++ b/docs/topics/internationalisation.md @@ -1,10 +1,10 @@ # Internationalisation -REST framework ships with translatable error messages. You can make these appear in your language enabling [Django's standard translation mechanisms](https://docs.djangoproject.com/en/1.7/topics/i18n/translation) and by translating the messages into your language. +REST framework ships with translatable error messages. You can make these appear in your language enabling [Django's standard translation mechanisms][django-translation] and by translating the messages into your language. ## How to translate REST Framework errors -This guide assumes you are already familiar with how to translate a Django app. If you're not, start by reading [Django's translation docs](https://docs.djangoproject.com/en/1.7/topics/i18n/translation). +This guide assumes you are already familiar with how to translate a Django app. If you're not, start by reading [Django's translation docs][django-translation]. #### To translate REST framework error messages: @@ -16,19 +16,28 @@ This guide assumes you are already familiar with how to translate a Django app. ln -s /home/user/.virtualenvs/myproject/lib/python2.7/site-packages/rest_framework/ rest_framework ``` - To find out where `rest_framework` is installed, run + --- + + **Note:** To find out where `rest_framework` is installed, run ``` python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" ``` -3. Run Django's `makemessages` command in the normal way, but add the `--symlink` option. For example, if you want to translate into Brazilian Portuguese you would run + --- + + + +3. Run Django's `makemessages` command in the normal way, but add the `--symlink` option. For example, if you want to translate into Brazilian Portuguese you would run ``` manage.py makemessages --symlink -l pt_BR ``` -4. Translate the `django.po` file which is created as normal. This will be in the folder `myapp/locale/pt_BR/LC_MESSAGES`. +4. Translate the `django.po` file which is created as normal. This will be in the folder `myapp/locale/pt_BR/LC_MESSAGES`. 5. Run `manage.py compilemessages` as normal 6. Restart your server + + +[django-translation]: https://docs.djangoproject.com/en/1.7/topics/i18n/translation From 2781903a5a0be7f5314de54ff2dbc8ef393eab0a Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Fri, 26 Dec 2014 12:52:17 +0000 Subject: [PATCH 03/21] Add info about how django chooses which language to use --- docs/topics/internationalisation.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/topics/internationalisation.md b/docs/topics/internationalisation.md index 552fdd273..a0aab7533 100644 --- a/docs/topics/internationalisation.md +++ b/docs/topics/internationalisation.md @@ -40,4 +40,22 @@ This guide assumes you are already familiar with how to translate a Django app. 6. Restart your server + +## How Django chooses which language to use +REST framework will use the same preferences to select which language to display as Django does. You can find more info in the [django docs on discovering language preferences][django-language-preference]. For reference, these are + +1. First, it looks for the language prefix in the requested URL +2. Failing that, it looks for the `LANGUAGE_SESSION_KEY` key in the current user’s session. +3. Failing that, it looks for a cookie +4. Failing that, it looks at the `Accept-Language` HTTP header. +5. Failing that, it uses the global `LANGUAGE_CODE` setting. + +--- + +**Note:** You'll need to include the `django.middleware.locale.LocaleMiddleware` to enable any of the per-request language preferences. + +--- + + [django-translation]: https://docs.djangoproject.com/en/1.7/topics/i18n/translation +[django-language-preference]: https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#how-django-discovers-language-preference From 0b8a83bd624673cb0a05e01c691729ccee3a8782 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Sun, 28 Dec 2014 18:20:41 +0000 Subject: [PATCH 04/21] update internationalisation instructions to prevent symlinking; add base .po file --- docs/topics/internationalisation.md | 52 +++- .../locale/en_US/LC_MESSAGES/django.po | 277 ++++++++++++++++++ 2 files changed, 318 insertions(+), 11 deletions(-) create mode 100644 rest_framework/locale/en_US/LC_MESSAGES/django.po diff --git a/docs/topics/internationalisation.md b/docs/topics/internationalisation.md index a0aab7533..6470ee033 100644 --- a/docs/topics/internationalisation.md +++ b/docs/topics/internationalisation.md @@ -9,12 +9,40 @@ This guide assumes you are already familiar with how to translate a Django app. #### To translate REST framework error messages: -1. Pick an app where you want the translations to be, for example `myapp` +1. Make a new folder where you want to store the translated errors. Add this +path to your [`LOCALE_PATHS`][django-locale-paths] setting. + + --- + + **Note:** For the rest of +this document we will assume the path you created was +`/home/www/project/conf/locale/`, and that you have updated your `settings.py` to include the setting: -2. Add a symlink from that app to the installed `rest_framework` ``` - ln -s /home/user/.virtualenvs/myproject/lib/python2.7/site-packages/rest_framework/ rest_framework + LOCALE_PATHS = ( + '/home/www/project/conf/locale/', + ) ``` + + --- + +2. Now create a subfolder for the language you want to translate. The folder should be named using [locale +name][django-locale-name] notation. E.g. `de`, `pt_BR`, `es_AR`, etc. + + ``` + mkdir /home/www/project/conf/locale/pt_BR/LC_MESSAGES + ``` + +3. Now copy the base translations file from the REST framework source code +into your translations folder + + ``` + cp /home/user/.virtualenvs/myproject/lib/python2.7/site-packages/rest_framework/locale/en_US/LC_MESSAGES/django.po + /home/www/project/conf/locale/pt_BR/LC_MESSAGES + ``` + + This should create the file + `/home/www/project/conf/locale/pt_BR/LC_MESSAGES/django.po` --- @@ -27,17 +55,17 @@ This guide assumes you are already familiar with how to translate a Django app. --- +4. Edit `/home/www/project/conf/locale/pt_BR/LC_MESSAGES/django.po` and +translate all the error messages. -3. Run Django's `makemessages` command in the normal way, but add the `--symlink` option. For example, if you want to translate into Brazilian Portuguese you would run - ``` - manage.py makemessages --symlink -l pt_BR - ``` - -4. Translate the `django.po` file which is created as normal. This will be in the folder `myapp/locale/pt_BR/LC_MESSAGES`. +5. Run `manage.py compilemessages -l pt_BR` to make the translations +available for Django to use. You should see a message -5. Run `manage.py compilemessages` as normal + ``` + processing file django.po in /home/www/project/conf/locale/pt_BR/LC_MESSAGES + ``` -6. Restart your server +6. Restart your server. @@ -59,3 +87,5 @@ REST framework will use the same preferences to select which language to display [django-translation]: https://docs.djangoproject.com/en/1.7/topics/i18n/translation [django-language-preference]: https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#how-django-discovers-language-preference +[django-locale-paths]: https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-LOCALE_PATHS +[django-locale-name]: https://docs.djangoproject.com/en/1.7/topics/i18n/#term-locale-name \ No newline at end of file diff --git a/rest_framework/locale/en_US/LC_MESSAGES/django.po b/rest_framework/locale/en_US/LC_MESSAGES/django.po new file mode 100644 index 000000000..510ce0aad --- /dev/null +++ b/rest_framework/locale/en_US/LC_MESSAGES/django.po @@ -0,0 +1,277 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-12-28 17:49+0000\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: rest_framework/authtoken/serializers.py:20 +msgid "User account is disabled." +msgstr "" + +#: rest_framework/authtoken/serializers.py:23 +msgid "Unable to log in with provided credentials." +msgstr "" + +#: rest_framework/authtoken/serializers.py:26 +msgid "Must include \"username\" and \"password\"" +msgstr "" + +#: rest_framework/exceptions.py:39 +msgid "A server error occured" +msgstr "" + +#: rest_framework/exceptions.py:74 +msgid "Malformed request." +msgstr "" + +#: rest_framework/exceptions.py:79 +msgid "Incorrect authentication credentials." +msgstr "" + +#: rest_framework/exceptions.py:84 +msgid "Authentication credentials were not provided." +msgstr "" + +#: rest_framework/exceptions.py:89 +msgid "You do not have permission to perform this action." +msgstr "" + +#: rest_framework/exceptions.py:94 +#, python-format +msgid "Method '%s' not allowed." +msgstr "" + +#: rest_framework/exceptions.py:105 +msgid "Could not satisfy the request Accept header" +msgstr "" + +#: rest_framework/exceptions.py:117 +#, python-format +msgid "Unsupported media type '%s' in request." +msgstr "" + +#: rest_framework/exceptions.py:128 +msgid "Request was throttled." +msgstr "" + +#: rest_framework/exceptions.py:130 +#, python-format +msgid "Expected available in %(wait)d second." +msgid_plural "Expected available in %(wait)d seconds." +msgstr[0] "" +msgstr[1] "" + +#: rest_framework/fields.py:152 rest_framework/relations.py:131 +#: rest_framework/relations.py:155 rest_framework/validators.py:77 +#: rest_framework/validators.py:155 +msgid "This field is required." +msgstr "" + +#: rest_framework/fields.py:153 +msgid "This field may not be null." +msgstr "" + +#: rest_framework/fields.py:484 rest_framework/fields.py:512 +msgid "`{input}` is not a valid boolean." +msgstr "" + +#: rest_framework/fields.py:547 +msgid "This field may not be blank." +msgstr "" + +#: rest_framework/fields.py:548 rest_framework/fields.py:1250 +msgid "Ensure this field has no more than {max_length} characters." +msgstr "" + +#: rest_framework/fields.py:549 +msgid "Ensure this field has at least {min_length} characters." +msgstr "" + +#: rest_framework/fields.py:584 +msgid "Enter a valid email address." +msgstr "" + +#: rest_framework/fields.py:601 +msgid "This value does not match the required pattern." +msgstr "" + +#: rest_framework/fields.py:612 +msgid "" +"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens." +msgstr "" + +#: rest_framework/fields.py:624 +msgid "Enter a valid URL." +msgstr "" + +#: rest_framework/fields.py:637 +msgid "A valid integer is required." +msgstr "" + +#: rest_framework/fields.py:638 rest_framework/fields.py:672 +#: rest_framework/fields.py:705 +msgid "Ensure this value is less than or equal to {max_value}." +msgstr "" + +#: rest_framework/fields.py:639 rest_framework/fields.py:673 +#: rest_framework/fields.py:706 +msgid "Ensure this value is greater than or equal to {min_value}." +msgstr "" + +#: rest_framework/fields.py:640 rest_framework/fields.py:674 +#: rest_framework/fields.py:710 +msgid "String value too large" +msgstr "" + +#: rest_framework/fields.py:671 rest_framework/fields.py:704 +msgid "A valid number is required." +msgstr "" + +#: rest_framework/fields.py:707 +msgid "Ensure that there are no more than {max_digits} digits in total." +msgstr "" + +#: rest_framework/fields.py:708 +msgid "Ensure that there are no more than {max_decimal_places} decimal places." +msgstr "" + +#: rest_framework/fields.py:709 +msgid "" +"Ensure that there are no more than {max_whole_digits} digits before the " +"decimal point." +msgstr "" + +#: rest_framework/fields.py:793 +msgid "Datetime has wrong format. Use one of these formats instead: {format}" +msgstr "" + +#: rest_framework/fields.py:794 +msgid "Expected a datetime but got a date." +msgstr "" + +#: rest_framework/fields.py:858 +msgid "Date has wrong format. Use one of these formats instead: {format}" +msgstr "" + +#: rest_framework/fields.py:859 +msgid "Expected a date but got a datetime." +msgstr "" + +#: rest_framework/fields.py:916 +msgid "Time has wrong format. Use one of these formats instead: {format}" +msgstr "" + +#: rest_framework/fields.py:972 rest_framework/fields.py:1016 +msgid "`{input}` is not a valid choice." +msgstr "" + +#: rest_framework/fields.py:1017 rest_framework/serializers.py:474 +msgid "Expected a list of items but got type `{input_type}`." +msgstr "" + +#: rest_framework/fields.py:1047 +msgid "No file was submitted." +msgstr "" + +#: rest_framework/fields.py:1048 +msgid "The submitted data was not a file. Check the encoding type on the form." +msgstr "" + +#: rest_framework/fields.py:1049 +msgid "No filename could be determined." +msgstr "" + +#: rest_framework/fields.py:1050 +msgid "The submitted file is empty." +msgstr "" + +#: rest_framework/fields.py:1051 +msgid "" +"Ensure this filename has at most {max_length} characters (it has {length})." +msgstr "" + +#: rest_framework/fields.py:1093 +msgid "Upload a valid image. The file you uploaded was either not an " +msgstr "" + +#: rest_framework/fields.py:1119 +msgid "Expected a list of items but got type `{input_type}`" +msgstr "" + +#: rest_framework/generics.py:122 +msgid "Page is not 'last', nor can it be converted to an int." +msgstr "" + +#: rest_framework/generics.py:126 +#, python-format +msgid "Invalid page (%(page_number)s): %(message)s" +msgstr "" + +#: rest_framework/relations.py:132 +msgid "Invalid pk '{pk_value}' - object does not exist." +msgstr "" + +#: rest_framework/relations.py:133 +msgid "Incorrect type. Expected pk value, received {data_type}." +msgstr "" + +#: rest_framework/relations.py:156 +msgid "Invalid hyperlink - No URL match" +msgstr "" + +#: rest_framework/relations.py:157 +msgid "Invalid hyperlink - Incorrect URL match." +msgstr "" + +#: rest_framework/relations.py:158 +msgid "Invalid hyperlink - Object does not exist." +msgstr "" + +#: rest_framework/relations.py:159 +msgid "Incorrect type. Expected URL string, received {data_type}." +msgstr "" + +#: rest_framework/relations.py:294 +msgid "Object with {slug_name}={value} does not exist." +msgstr "" + +#: rest_framework/relations.py:295 +msgid "Invalid value." +msgstr "" + +#: rest_framework/serializers.py:299 +msgid "Invalid data. Expected a dictionary, but got {datatype}." +msgstr "" + +#: rest_framework/validators.py:22 +msgid "This field must be unique." +msgstr "" + +#: rest_framework/validators.py:76 +msgid "The fields {field_names} must make a unique set." +msgstr "" + +#: rest_framework/validators.py:219 +msgid "This field must be unique for the \"{date_field}\" date." +msgstr "" + +#: rest_framework/validators.py:234 +msgid "This field must be unique for the \"{date_field}\" month." +msgstr "" + +#: rest_framework/validators.py:247 +msgid "This field must be unique for the \"{date_field}\" year." +msgstr "" From faf76a4b75f12f3fa9de4e3ec455daa239af4d89 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 31 Dec 2014 12:49:20 +0000 Subject: [PATCH 05/21] fix spelling & grammar errors --- rest_framework/exceptions.py | 2 +- rest_framework/generics.py | 2 +- rest_framework/locale/en_US/LC_MESSAGES/django.po | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index bcfd8961b..2586fc332 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -36,7 +36,7 @@ class APIException(Exception): Subclasses should provide `.status_code` and `.default_detail` properties. """ status_code = status.HTTP_500_INTERNAL_SERVER_ERROR - default_detail = _('A server error occured') + default_detail = _('A server error occurred') def __init__(self, detail=None): if detail is not None: diff --git a/rest_framework/generics.py b/rest_framework/generics.py index e6db155e7..bdbc19a75 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -119,7 +119,7 @@ class GenericAPIView(views.APIView): if page == 'last': page_number = paginator.num_pages else: - raise Http404(_("Page is not 'last', nor can it be converted to an int.")) + raise Http404(_("Page is not 'last', and cannot be converted to an int.")) try: page = paginator.page(page_number) except InvalidPage as exc: diff --git a/rest_framework/locale/en_US/LC_MESSAGES/django.po b/rest_framework/locale/en_US/LC_MESSAGES/django.po index 510ce0aad..3bed91430 100644 --- a/rest_framework/locale/en_US/LC_MESSAGES/django.po +++ b/rest_framework/locale/en_US/LC_MESSAGES/django.po @@ -2,13 +2,13 @@ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR , YEAR. -# +# #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2014-12-28 17:49+0000\n" +"POT-Creation-Date: 2014-12-31 12:48+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -30,7 +30,7 @@ msgid "Must include \"username\" and \"password\"" msgstr "" #: rest_framework/exceptions.py:39 -msgid "A server error occured" +msgid "A server error occurred" msgstr "" #: rest_framework/exceptions.py:74 @@ -212,7 +212,7 @@ msgid "Expected a list of items but got type `{input_type}`" msgstr "" #: rest_framework/generics.py:122 -msgid "Page is not 'last', nor can it be converted to an int." +msgid "Page is not 'last', and cannot be converted to an int." msgstr "" #: rest_framework/generics.py:126 From a90ba2bc11de5fb391b95d4fce84f87ae7f88eff Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 31 Dec 2014 13:03:16 +0000 Subject: [PATCH 06/21] update error messages for language and consistency --- rest_framework/exceptions.py | 4 +-- rest_framework/fields.py | 17 +++++----- rest_framework/generics.py | 2 +- .../locale/en_US/LC_MESSAGES/django.po | 33 ++++++++++--------- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 2586fc332..d78b7e975 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -36,7 +36,7 @@ class APIException(Exception): Subclasses should provide `.status_code` and `.default_detail` properties. """ status_code = status.HTTP_500_INTERNAL_SERVER_ERROR - default_detail = _('A server error occurred') + default_detail = _('A server error occurred.') def __init__(self, detail=None): if detail is not None: @@ -107,7 +107,7 @@ class MethodNotAllowed(APIException): class NotAcceptable(APIException): status_code = status.HTTP_406_NOT_ACCEPTABLE - default_detail = _('Could not satisfy the request Accept header') + default_detail = _('Could not satisfy the request Accept header.') def __init__(self, detail=None, available_renderers=None): if detail is not None: diff --git a/rest_framework/fields.py b/rest_framework/fields.py index c40dc3fb3..0ff2b0733 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -640,7 +640,7 @@ class IntegerField(Field): 'invalid': _('A valid integer is required.'), 'max_value': _('Ensure this value is less than or equal to {max_value}.'), 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), - 'max_string_length': _('String value too large') + 'max_string_length': _('String value too large.') } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -674,7 +674,7 @@ class FloatField(Field): 'invalid': _("A valid number is required."), 'max_value': _('Ensure this value is less than or equal to {max_value}.'), 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), - 'max_string_length': _('String value too large') + 'max_string_length': _('String value too large.') } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -710,7 +710,7 @@ class DecimalField(Field): 'max_digits': _('Ensure that there are no more than {max_digits} digits in total.'), 'max_decimal_places': _('Ensure that there are no more than {max_decimal_places} decimal places.'), 'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.'), - 'max_string_length': _('String value too large') + 'max_string_length': _('String value too large.') } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -793,7 +793,7 @@ class DecimalField(Field): class DateTimeField(Field): default_error_messages = { - 'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}'), + 'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}.'), 'date': _('Expected a datetime but got a date.'), } format = api_settings.DATETIME_FORMAT @@ -858,7 +858,7 @@ class DateTimeField(Field): class DateField(Field): default_error_messages = { - 'invalid': _('Date has wrong format. Use one of these formats instead: {format}'), + 'invalid': _('Date has wrong format. Use one of these formats instead: {format}.'), 'datetime': _('Expected a date but got a datetime.'), } format = api_settings.DATE_FORMAT @@ -916,7 +916,7 @@ class DateField(Field): class TimeField(Field): default_error_messages = { - 'invalid': _('Time has wrong format. Use one of these formats instead: {format}'), + 'invalid': _('Time has wrong format. Use one of these formats instead: {format}.'), } format = api_settings.TIME_FORMAT input_formats = api_settings.TIME_INPUT_FORMATS @@ -1093,8 +1093,7 @@ class FileField(Field): class ImageField(FileField): default_error_messages = { 'invalid_image': _( - 'Upload a valid image. The file you uploaded was either not an ' - 'image or a corrupted image.' + 'Upload a valid image. The file you uploaded was either not an image or a corrupted image.' ), } @@ -1119,7 +1118,7 @@ class ListField(Field): child = None initial = [] default_error_messages = { - 'not_a_list': _('Expected a list of items but got type `{input_type}`') + 'not_a_list': _('Expected a list of items but got type `{input_type}`.') } def __init__(self, *args, **kwargs): diff --git a/rest_framework/generics.py b/rest_framework/generics.py index bdbc19a75..680992d75 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -119,7 +119,7 @@ class GenericAPIView(views.APIView): if page == 'last': page_number = paginator.num_pages else: - raise Http404(_("Page is not 'last', and cannot be converted to an int.")) + raise Http404(_("Choose a valid page number. Page numbers must be a whole number, or must be the string 'last'.")) try: page = paginator.page(page_number) except InvalidPage as exc: diff --git a/rest_framework/locale/en_US/LC_MESSAGES/django.po b/rest_framework/locale/en_US/LC_MESSAGES/django.po index 3bed91430..18f5fe18d 100644 --- a/rest_framework/locale/en_US/LC_MESSAGES/django.po +++ b/rest_framework/locale/en_US/LC_MESSAGES/django.po @@ -2,13 +2,13 @@ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR , YEAR. -# +# #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2014-12-31 12:48+0000\n" +"POT-Creation-Date: 2014-12-31 13:02+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -30,7 +30,7 @@ msgid "Must include \"username\" and \"password\"" msgstr "" #: rest_framework/exceptions.py:39 -msgid "A server error occurred" +msgid "A server error occurred." msgstr "" #: rest_framework/exceptions.py:74 @@ -55,7 +55,7 @@ msgid "Method '%s' not allowed." msgstr "" #: rest_framework/exceptions.py:105 -msgid "Could not satisfy the request Accept header" +msgid "Could not satisfy the request Accept header." msgstr "" #: rest_framework/exceptions.py:117 @@ -92,7 +92,7 @@ msgstr "" msgid "This field may not be blank." msgstr "" -#: rest_framework/fields.py:548 rest_framework/fields.py:1250 +#: rest_framework/fields.py:548 rest_framework/fields.py:1249 msgid "Ensure this field has no more than {max_length} characters." msgstr "" @@ -133,7 +133,7 @@ msgstr "" #: rest_framework/fields.py:640 rest_framework/fields.py:674 #: rest_framework/fields.py:710 -msgid "String value too large" +msgid "String value too large." msgstr "" #: rest_framework/fields.py:671 rest_framework/fields.py:704 @@ -155,7 +155,7 @@ msgid "" msgstr "" #: rest_framework/fields.py:793 -msgid "Datetime has wrong format. Use one of these formats instead: {format}" +msgid "Datetime has wrong format. Use one of these formats instead: {format}." msgstr "" #: rest_framework/fields.py:794 @@ -163,7 +163,7 @@ msgid "Expected a datetime but got a date." msgstr "" #: rest_framework/fields.py:858 -msgid "Date has wrong format. Use one of these formats instead: {format}" +msgid "Date has wrong format. Use one of these formats instead: {format}." msgstr "" #: rest_framework/fields.py:859 @@ -171,14 +171,15 @@ msgid "Expected a date but got a datetime." msgstr "" #: rest_framework/fields.py:916 -msgid "Time has wrong format. Use one of these formats instead: {format}" +msgid "Time has wrong format. Use one of these formats instead: {format}." msgstr "" #: rest_framework/fields.py:972 rest_framework/fields.py:1016 msgid "`{input}` is not a valid choice." msgstr "" -#: rest_framework/fields.py:1017 rest_framework/serializers.py:474 +#: rest_framework/fields.py:1017 rest_framework/fields.py:1118 +#: rest_framework/serializers.py:474 msgid "Expected a list of items but got type `{input_type}`." msgstr "" @@ -204,15 +205,15 @@ msgid "" msgstr "" #: rest_framework/fields.py:1093 -msgid "Upload a valid image. The file you uploaded was either not an " -msgstr "" - -#: rest_framework/fields.py:1119 -msgid "Expected a list of items but got type `{input_type}`" +msgid "" +"Upload a valid image. The file you uploaded was either not an image or a " +"corrupted image." msgstr "" #: rest_framework/generics.py:122 -msgid "Page is not 'last', and cannot be converted to an int." +msgid "" +"Choose a valid page number. Page numbers must be a whole number, or must be " +"the string 'last'." msgstr "" #: rest_framework/generics.py:126 From 32506e20756c84677abb5ae49706446a0d250371 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 31 Dec 2014 13:14:09 +0000 Subject: [PATCH 07/21] update expected error messages in tests --- tests/test_fields.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test_fields.py b/tests/test_fields.py index 04c721d36..61d39aff6 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -640,8 +640,8 @@ class TestDateField(FieldValues): datetime.date(2001, 1, 1): datetime.date(2001, 1, 1), } invalid_inputs = { - 'abc': ['Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'], - '2001-99-99': ['Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'], + 'abc': ['Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]].'], + '2001-99-99': ['Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]].'], datetime.datetime(2001, 1, 1, 12, 00): ['Expected a date but got a datetime.'], } outputs = { @@ -658,7 +658,7 @@ class TestCustomInputFormatDateField(FieldValues): '1 Jan 2001': datetime.date(2001, 1, 1), } invalid_inputs = { - '2001-01-01': ['Date has wrong format. Use one of these formats instead: DD [Jan-Dec] YYYY'] + '2001-01-01': ['Date has wrong format. Use one of these formats instead: DD [Jan-Dec] YYYY.'] } outputs = {} field = serializers.DateField(input_formats=['%d %b %Y']) @@ -702,8 +702,8 @@ class TestDateTimeField(FieldValues): '2001-01-01T14:00+01:00' if (django.VERSION > (1, 4)) else '2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()) } invalid_inputs = { - 'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'], - '2001-99-99T99:00': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'], + 'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].'], + '2001-99-99T99:00': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].'], datetime.date(2001, 1, 1): ['Expected a datetime but got a date.'], } outputs = { @@ -721,7 +721,7 @@ class TestCustomInputFormatDateTimeField(FieldValues): '1:35pm, 1 Jan 2001': datetime.datetime(2001, 1, 1, 13, 35, tzinfo=timezone.UTC()), } invalid_inputs = { - '2001-01-01T20:50': ['Datetime has wrong format. Use one of these formats instead: hh:mm[AM|PM], DD [Jan-Dec] YYYY'] + '2001-01-01T20:50': ['Datetime has wrong format. Use one of these formats instead: hh:mm[AM|PM], DD [Jan-Dec] YYYY.'] } outputs = {} field = serializers.DateTimeField(default_timezone=timezone.UTC(), input_formats=['%I:%M%p, %d %b %Y']) @@ -773,8 +773,8 @@ class TestTimeField(FieldValues): datetime.time(13, 00): datetime.time(13, 00), } invalid_inputs = { - 'abc': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'], - '99:99': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'], + 'abc': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]].'], + '99:99': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]].'], } outputs = { datetime.time(13, 00): '13:00:00' @@ -790,7 +790,7 @@ class TestCustomInputFormatTimeField(FieldValues): '1:00pm': datetime.time(13, 00), } invalid_inputs = { - '13:00': ['Time has wrong format. Use one of these formats instead: hh:mm[AM|PM]'], + '13:00': ['Time has wrong format. Use one of these formats instead: hh:mm[AM|PM].'], } outputs = {} field = serializers.TimeField(input_formats=['%I:%M%p']) @@ -1028,7 +1028,7 @@ class TestListField(FieldValues): (['1', '2', '3'], [1, 2, 3]) ] invalid_inputs = [ - ('not a list', ['Expected a list of items but got type `str`']), + ('not a list', ['Expected a list of items but got type `str`.']), ([1, 2, 'error'], ['A valid integer is required.']) ] outputs = [ From 9f169acb62a6223a5add0fee7f6d53108e42f207 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 31 Dec 2014 14:56:23 +0000 Subject: [PATCH 08/21] capitalise Django --- docs/topics/internationalisation.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/topics/internationalisation.md b/docs/topics/internationalisation.md index 6470ee033..fac3bdb7a 100644 --- a/docs/topics/internationalisation.md +++ b/docs/topics/internationalisation.md @@ -70,7 +70,8 @@ available for Django to use. You should see a message ## How Django chooses which language to use -REST framework will use the same preferences to select which language to display as Django does. You can find more info in the [django docs on discovering language preferences][django-language-preference]. For reference, these are +REST framework will use the same preferences to select which language to +display as Django does. You can find more info in the [Django docs on discovering language preferences][django-language-preference]. For reference, these are 1. First, it looks for the language prefix in the requested URL 2. Failing that, it looks for the `LANGUAGE_SESSION_KEY` key in the current user’s session. From 6fb37207d18949031fb7203d6fd67ee503df0a34 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Fri, 2 Jan 2015 11:11:13 +0000 Subject: [PATCH 09/21] add missing period; update generated translations --- rest_framework/exceptions.py | 2 +- .../locale/en_US/LC_MESSAGES/django.po | 96 +++++++++++-------- 2 files changed, 59 insertions(+), 39 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index d78b7e975..c8cedfceb 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -91,7 +91,7 @@ class PermissionDenied(APIException): class NotFound(APIException): status_code = status.HTTP_404_NOT_FOUND - default_detail = _('Not found') + default_detail = _('Not found.') class MethodNotAllowed(APIException): diff --git a/rest_framework/locale/en_US/LC_MESSAGES/django.po b/rest_framework/locale/en_US/LC_MESSAGES/django.po index 18f5fe18d..569020739 100644 --- a/rest_framework/locale/en_US/LC_MESSAGES/django.po +++ b/rest_framework/locale/en_US/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2014-12-31 13:02+0000\n" +"POT-Creation-Date: 2015-01-02 11:10+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -50,24 +50,28 @@ msgid "You do not have permission to perform this action." msgstr "" #: rest_framework/exceptions.py:94 +msgid "Not found." +msgstr "" + +#: rest_framework/exceptions.py:99 #, python-format msgid "Method '%s' not allowed." msgstr "" -#: rest_framework/exceptions.py:105 +#: rest_framework/exceptions.py:110 msgid "Could not satisfy the request Accept header." msgstr "" -#: rest_framework/exceptions.py:117 +#: rest_framework/exceptions.py:122 #, python-format msgid "Unsupported media type '%s' in request." msgstr "" -#: rest_framework/exceptions.py:128 +#: rest_framework/exceptions.py:133 msgid "Request was throttled." msgstr "" -#: rest_framework/exceptions.py:130 +#: rest_framework/exceptions.py:135 #, python-format msgid "Expected available in %(wait)d second." msgid_plural "Expected available in %(wait)d seconds." @@ -84,127 +88,127 @@ msgstr "" msgid "This field may not be null." msgstr "" -#: rest_framework/fields.py:484 rest_framework/fields.py:512 +#: rest_framework/fields.py:480 rest_framework/fields.py:508 msgid "`{input}` is not a valid boolean." msgstr "" -#: rest_framework/fields.py:547 +#: rest_framework/fields.py:543 msgid "This field may not be blank." msgstr "" -#: rest_framework/fields.py:548 rest_framework/fields.py:1249 +#: rest_framework/fields.py:544 rest_framework/fields.py:1252 msgid "Ensure this field has no more than {max_length} characters." msgstr "" -#: rest_framework/fields.py:549 +#: rest_framework/fields.py:545 msgid "Ensure this field has at least {min_length} characters." msgstr "" -#: rest_framework/fields.py:584 +#: rest_framework/fields.py:587 msgid "Enter a valid email address." msgstr "" -#: rest_framework/fields.py:601 +#: rest_framework/fields.py:604 msgid "This value does not match the required pattern." msgstr "" -#: rest_framework/fields.py:612 +#: rest_framework/fields.py:615 msgid "" "Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens." msgstr "" -#: rest_framework/fields.py:624 +#: rest_framework/fields.py:627 msgid "Enter a valid URL." msgstr "" -#: rest_framework/fields.py:637 +#: rest_framework/fields.py:640 msgid "A valid integer is required." msgstr "" -#: rest_framework/fields.py:638 rest_framework/fields.py:672 -#: rest_framework/fields.py:705 +#: rest_framework/fields.py:641 rest_framework/fields.py:675 +#: rest_framework/fields.py:708 msgid "Ensure this value is less than or equal to {max_value}." msgstr "" -#: rest_framework/fields.py:639 rest_framework/fields.py:673 -#: rest_framework/fields.py:706 +#: rest_framework/fields.py:642 rest_framework/fields.py:676 +#: rest_framework/fields.py:709 msgid "Ensure this value is greater than or equal to {min_value}." msgstr "" -#: rest_framework/fields.py:640 rest_framework/fields.py:674 -#: rest_framework/fields.py:710 +#: rest_framework/fields.py:643 rest_framework/fields.py:677 +#: rest_framework/fields.py:713 msgid "String value too large." msgstr "" -#: rest_framework/fields.py:671 rest_framework/fields.py:704 +#: rest_framework/fields.py:674 rest_framework/fields.py:707 msgid "A valid number is required." msgstr "" -#: rest_framework/fields.py:707 +#: rest_framework/fields.py:710 msgid "Ensure that there are no more than {max_digits} digits in total." msgstr "" -#: rest_framework/fields.py:708 +#: rest_framework/fields.py:711 msgid "Ensure that there are no more than {max_decimal_places} decimal places." msgstr "" -#: rest_framework/fields.py:709 +#: rest_framework/fields.py:712 msgid "" "Ensure that there are no more than {max_whole_digits} digits before the " "decimal point." msgstr "" -#: rest_framework/fields.py:793 +#: rest_framework/fields.py:796 msgid "Datetime has wrong format. Use one of these formats instead: {format}." msgstr "" -#: rest_framework/fields.py:794 +#: rest_framework/fields.py:797 msgid "Expected a datetime but got a date." msgstr "" -#: rest_framework/fields.py:858 +#: rest_framework/fields.py:861 msgid "Date has wrong format. Use one of these formats instead: {format}." msgstr "" -#: rest_framework/fields.py:859 +#: rest_framework/fields.py:862 msgid "Expected a date but got a datetime." msgstr "" -#: rest_framework/fields.py:916 +#: rest_framework/fields.py:919 msgid "Time has wrong format. Use one of these formats instead: {format}." msgstr "" -#: rest_framework/fields.py:972 rest_framework/fields.py:1016 +#: rest_framework/fields.py:975 rest_framework/fields.py:1019 msgid "`{input}` is not a valid choice." msgstr "" -#: rest_framework/fields.py:1017 rest_framework/fields.py:1118 -#: rest_framework/serializers.py:474 +#: rest_framework/fields.py:1020 rest_framework/fields.py:1121 +#: rest_framework/serializers.py:476 msgid "Expected a list of items but got type `{input_type}`." msgstr "" -#: rest_framework/fields.py:1047 +#: rest_framework/fields.py:1050 msgid "No file was submitted." msgstr "" -#: rest_framework/fields.py:1048 +#: rest_framework/fields.py:1051 msgid "The submitted data was not a file. Check the encoding type on the form." msgstr "" -#: rest_framework/fields.py:1049 +#: rest_framework/fields.py:1052 msgid "No filename could be determined." msgstr "" -#: rest_framework/fields.py:1050 +#: rest_framework/fields.py:1053 msgid "The submitted file is empty." msgstr "" -#: rest_framework/fields.py:1051 +#: rest_framework/fields.py:1054 msgid "" "Ensure this filename has at most {max_length} characters (it has {length})." msgstr "" -#: rest_framework/fields.py:1093 +#: rest_framework/fields.py:1096 msgid "" "Upload a valid image. The file you uploaded was either not an image or a " "corrupted image." @@ -276,3 +280,19 @@ msgstr "" #: rest_framework/validators.py:247 msgid "This field must be unique for the \"{date_field}\" year." msgstr "" + +#: rest_framework/versioning.py:39 +msgid "Invalid version in 'Accept' header." +msgstr "" + +#: rest_framework/versioning.py:70 rest_framework/versioning.py:112 +msgid "Invalid version in URL path." +msgstr "" + +#: rest_framework/versioning.py:138 +msgid "Invalid version in hostname." +msgstr "" + +#: rest_framework/versioning.py:160 +msgid "Invalid version in query parameter." +msgstr "" From 7913947757b0e6bd1b8828db81933c32c498e20a Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 11:13:03 +0000 Subject: [PATCH 10/21] add config and documentation about uploading/downloading translations from Transifex --- .tx/config | 9 +++++ CONTRIBUTING.md | 54 +++++++++++++++++++++++++++++ docs/topics/internationalisation.md | 11 +++--- 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 .tx/config diff --git a/.tx/config b/.tx/config new file mode 100644 index 000000000..271fa1e35 --- /dev/null +++ b/.tx/config @@ -0,0 +1,9 @@ +[main] +host = https://www.transifex.com + +[django-rest-framework.djangopo] +file_filter = rest_framework/locale//LC_MESSAGES/django.po +source_file = rest_framework/locale/en_US/LC_MESSAGES/django.po +source_lang = en_US +type = PO + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b963a4993..d94eb87e0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -177,6 +177,57 @@ We recommend the [`django-reusable-app`][django-reusable-app] template as a good Once your package is decently documented and available on PyPI open a pull request or issue, and we'll add a link to it from the main REST framework documentation. +# Translations + +If REST framework isn't translated into your language you can request that it is at the [Transifex project][transifex]. + +## Managing Transfiex +The [official Transifex client][transifex-client] is used to upload and download translations to Transifex. The client is installed using pip: + +``` +pip install transifex-client +``` + +To use it you'll need a login to Transifex which has a password, and you'll need to have administrative access to the Transifex project. You'll need to create a `~/.transifexrc` file which contains your authentication information: + +``` +[https://www.transifex.com] +username = user +token = +password = p@ssw0rd +hostname = https://www.transifex.com +``` + +## Upload new source translations +When any user-visible strings are changed, they should be uploaded to Transifex so that the translators can start to translate them. To do this, just run: + +``` +cd rest_framework +django-admin.py makemessages -l en_US +cd .. +tx push -s +``` + +When pushing source files, Transifex will update the source strings of a resource to match those from the new source file. + +Here's how differences between the old and new source files will be handled: + +* New strings will be added. +* Modified strings will be added as well. +* Strings which do not exist in the new source file will be removed from the database, along with their translations. If that source strings gets re-added later then [Transifex Translation Memory][translation-memory] will automatically restore the translated string too. + + +## Get translations +When a translator has finished translating their work needs to be downloaded from Transifex into the source repo. To do this, run: + +``` +tx pull -a +cd rest_framework +django-admin.py compilemessages +``` + +You can then commit as normal. + [cite]: http://www.w3.org/People/Berners-Lee/FAQ.html [code-of-conduct]: https://www.djangoproject.com/conduct/ [google-group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework @@ -190,3 +241,6 @@ Once your package is decently documented and available on PyPI open a pull reque [docs]: https://github.com/tomchristie/django-rest-framework/tree/master/docs [mou]: http://mouapp.com/ [django-reusable-app]: https://github.com/dabapps/django-reusable-app +[transifex]: https://www.transifex.com/projects/p/django-rest-framework/ +[transifex-client]: https://pypi.python.org/pypi/transifex-client +[translation-memory]: http://docs.transifex.com/guides/tm#let-tm-automatically-populate-translations \ No newline at end of file diff --git a/docs/topics/internationalisation.md b/docs/topics/internationalisation.md index fac3bdb7a..2a476c864 100644 --- a/docs/topics/internationalisation.md +++ b/docs/topics/internationalisation.md @@ -3,12 +3,14 @@ REST framework ships with translatable error messages. You can make these appea ## How to translate REST Framework errors +REST framework translations are managed online using [Transifex.com][transifex]. To get started, checkout the guide in the [CONTRIBUTING.md guide][contributing]. + +Sometimes you may want to use REST Framework in a language which has not been translated yet on Transifex. If that is the case then you should translate the error messages locally. + +#### How to translate REST Framework error messages locally: This guide assumes you are already familiar with how to translate a Django app. If you're not, start by reading [Django's translation docs][django-translation]. - -#### To translate REST framework error messages: - 1. Make a new folder where you want to store the translated errors. Add this path to your [`LOCALE_PATHS`][django-locale-paths] setting. @@ -89,4 +91,5 @@ display as Django does. You can find more info in the [Django docs on discoveri [django-translation]: https://docs.djangoproject.com/en/1.7/topics/i18n/translation [django-language-preference]: https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#how-django-discovers-language-preference [django-locale-paths]: https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-LOCALE_PATHS -[django-locale-name]: https://docs.djangoproject.com/en/1.7/topics/i18n/#term-locale-name \ No newline at end of file +[django-locale-name]: https://docs.djangoproject.com/en/1.7/topics/i18n/#term-locale-name +[contributing]: ../../CONTRIBUTING.md From 9b4177b6ea38de6e86b0fe723834b6ef36af15b3 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 11:41:06 +0000 Subject: [PATCH 11/21] switch to using format strings in error messages; raise NotFound when pagination fails to provide a more useful error message --- rest_framework/exceptions.py | 28 ++++++++++++++-------------- rest_framework/generics.py | 14 ++++++++------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index c8cedfceb..dfc57293e 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -7,8 +7,7 @@ In addition Django's built in 403 and 404 exceptions are handled. from __future__ import unicode_literals from django.utils import six from django.utils.encoding import force_text -from django.utils.translation import ugettext_lazy as _ -from django.utils.translation import ungettext_lazy +from django.utils.translation import ugettext_lazy as _, ungettext from rest_framework import status import math @@ -96,13 +95,13 @@ class NotFound(APIException): class MethodNotAllowed(APIException): status_code = status.HTTP_405_METHOD_NOT_ALLOWED - default_detail = _("Method '%s' not allowed.") + default_detail = _("Method {method} not allowed.") def __init__(self, method, detail=None): if detail is not None: self.detail = force_text(detail) else: - self.detail = force_text(self.default_detail) % method + self.detail = force_text(self.default_detail).format(method=method) class NotAcceptable(APIException): @@ -119,23 +118,22 @@ class NotAcceptable(APIException): class UnsupportedMediaType(APIException): status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE - default_detail = _("Unsupported media type '%s' in request.") + default_detail = _("Unsupported media type '{media_type}' in request.") def __init__(self, media_type, detail=None): if detail is not None: self.detail = force_text(detail) else: - self.detail = force_text(self.default_detail) % media_type + self.detail = force_text(self.default_detail).format( + media_type=media_type + ) class Throttled(APIException): status_code = status.HTTP_429_TOO_MANY_REQUESTS default_detail = _('Request was throttled.') - extra_detail = ungettext_lazy( - 'Expected available in %(wait)d second.', - 'Expected available in %(wait)d seconds.', - 'wait' - ) + extra_detail_singular = 'Expected available in {wait} second.' + extra_detail_plural = 'Expected available in {wait} seconds.' def __init__(self, wait=None, detail=None): if detail is not None: @@ -147,6 +145,8 @@ class Throttled(APIException): self.wait = None else: self.wait = math.ceil(wait) - self.detail += ' ' + force_text( - self.extra_detail % {'wait': self.wait} - ) + self.detail += ' ' + force_text(ungettext( + self.extra_detail_singular.format(wait=self.wait), + self.extra_detail_plural.format(wait=self.wait), + self.wait + )) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 680992d75..fe92355d9 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -10,6 +10,7 @@ from django.shortcuts import get_object_or_404 as _get_object_or_404 from django.utils import six from django.utils.translation import ugettext as _ from rest_framework import views, mixins +from rest_framework.exceptions import NotFound from rest_framework.settings import api_settings @@ -119,15 +120,16 @@ class GenericAPIView(views.APIView): if page == 'last': page_number = paginator.num_pages else: - raise Http404(_("Choose a valid page number. Page numbers must be a whole number, or must be the string 'last'.")) + raise NotFound(_("Choose a valid page number. Page numbers must be a whole number, or must be the string 'last'.")) + + page_number = -1 try: page = paginator.page(page_number) except InvalidPage as exc: - error_format = _('Invalid page (%(page_number)s): %(message)s') - raise Http404(error_format % { - 'page_number': page_number, - 'message': six.text_type(exc) - }) + error_format = _('Invalid page ({page_number}): {message}') + raise NotFound(error_format.format( + page_number=page_number, message=six.text_type(exc) + )) return page From 3819ae35ac70ef25804f285b7b59edf2f67ea915 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 11:42:36 +0000 Subject: [PATCH 12/21] recompile pofile with new python format strings --- .../locale/en_US/LC_MESSAGES/django.po | 153 ++++++++---------- 1 file changed, 69 insertions(+), 84 deletions(-) diff --git a/rest_framework/locale/en_US/LC_MESSAGES/django.po b/rest_framework/locale/en_US/LC_MESSAGES/django.po index 569020739..7c5a6c02b 100644 --- a/rest_framework/locale/en_US/LC_MESSAGES/django.po +++ b/rest_framework/locale/en_US/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2015-01-02 11:10+0000\n" +"POT-Creation-Date: 2015-01-07 11:40+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,282 +17,267 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -#: rest_framework/authtoken/serializers.py:20 +#: authtoken/serializers.py:20 msgid "User account is disabled." msgstr "" -#: rest_framework/authtoken/serializers.py:23 +#: authtoken/serializers.py:23 msgid "Unable to log in with provided credentials." msgstr "" -#: rest_framework/authtoken/serializers.py:26 +#: authtoken/serializers.py:26 msgid "Must include \"username\" and \"password\"" msgstr "" -#: rest_framework/exceptions.py:39 +#: exceptions.py:38 msgid "A server error occurred." msgstr "" -#: rest_framework/exceptions.py:74 +#: exceptions.py:73 msgid "Malformed request." msgstr "" -#: rest_framework/exceptions.py:79 +#: exceptions.py:78 msgid "Incorrect authentication credentials." msgstr "" -#: rest_framework/exceptions.py:84 +#: exceptions.py:83 msgid "Authentication credentials were not provided." msgstr "" -#: rest_framework/exceptions.py:89 +#: exceptions.py:88 msgid "You do not have permission to perform this action." msgstr "" -#: rest_framework/exceptions.py:94 +#: exceptions.py:93 msgid "Not found." msgstr "" -#: rest_framework/exceptions.py:99 -#, python-format -msgid "Method '%s' not allowed." +#: exceptions.py:98 +msgid "Method {method} not allowed." msgstr "" -#: rest_framework/exceptions.py:110 +#: exceptions.py:109 msgid "Could not satisfy the request Accept header." msgstr "" -#: rest_framework/exceptions.py:122 -#, python-format -msgid "Unsupported media type '%s' in request." +#: exceptions.py:121 +msgid "Unsupported media type '{media_type}' in request." msgstr "" -#: rest_framework/exceptions.py:133 +#: exceptions.py:134 msgid "Request was throttled." msgstr "" -#: rest_framework/exceptions.py:135 -#, python-format -msgid "Expected available in %(wait)d second." -msgid_plural "Expected available in %(wait)d seconds." -msgstr[0] "" -msgstr[1] "" - -#: rest_framework/fields.py:152 rest_framework/relations.py:131 -#: rest_framework/relations.py:155 rest_framework/validators.py:77 -#: rest_framework/validators.py:155 +#: fields.py:152 relations.py:131 relations.py:155 validators.py:77 +#: validators.py:155 msgid "This field is required." msgstr "" -#: rest_framework/fields.py:153 +#: fields.py:153 msgid "This field may not be null." msgstr "" -#: rest_framework/fields.py:480 rest_framework/fields.py:508 +#: fields.py:480 fields.py:508 msgid "`{input}` is not a valid boolean." msgstr "" -#: rest_framework/fields.py:543 +#: fields.py:543 msgid "This field may not be blank." msgstr "" -#: rest_framework/fields.py:544 rest_framework/fields.py:1252 +#: fields.py:544 fields.py:1252 msgid "Ensure this field has no more than {max_length} characters." msgstr "" -#: rest_framework/fields.py:545 +#: fields.py:545 msgid "Ensure this field has at least {min_length} characters." msgstr "" -#: rest_framework/fields.py:587 +#: fields.py:587 msgid "Enter a valid email address." msgstr "" -#: rest_framework/fields.py:604 +#: fields.py:604 msgid "This value does not match the required pattern." msgstr "" -#: rest_framework/fields.py:615 +#: fields.py:615 msgid "" "Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens." msgstr "" -#: rest_framework/fields.py:627 +#: fields.py:627 msgid "Enter a valid URL." msgstr "" -#: rest_framework/fields.py:640 +#: fields.py:640 msgid "A valid integer is required." msgstr "" -#: rest_framework/fields.py:641 rest_framework/fields.py:675 -#: rest_framework/fields.py:708 +#: fields.py:641 fields.py:675 fields.py:708 msgid "Ensure this value is less than or equal to {max_value}." msgstr "" -#: rest_framework/fields.py:642 rest_framework/fields.py:676 -#: rest_framework/fields.py:709 +#: fields.py:642 fields.py:676 fields.py:709 msgid "Ensure this value is greater than or equal to {min_value}." msgstr "" -#: rest_framework/fields.py:643 rest_framework/fields.py:677 -#: rest_framework/fields.py:713 +#: fields.py:643 fields.py:677 fields.py:713 msgid "String value too large." msgstr "" -#: rest_framework/fields.py:674 rest_framework/fields.py:707 +#: fields.py:674 fields.py:707 msgid "A valid number is required." msgstr "" -#: rest_framework/fields.py:710 +#: fields.py:710 msgid "Ensure that there are no more than {max_digits} digits in total." msgstr "" -#: rest_framework/fields.py:711 +#: fields.py:711 msgid "Ensure that there are no more than {max_decimal_places} decimal places." msgstr "" -#: rest_framework/fields.py:712 +#: fields.py:712 msgid "" "Ensure that there are no more than {max_whole_digits} digits before the " "decimal point." msgstr "" -#: rest_framework/fields.py:796 +#: fields.py:796 msgid "Datetime has wrong format. Use one of these formats instead: {format}." msgstr "" -#: rest_framework/fields.py:797 +#: fields.py:797 msgid "Expected a datetime but got a date." msgstr "" -#: rest_framework/fields.py:861 +#: fields.py:861 msgid "Date has wrong format. Use one of these formats instead: {format}." msgstr "" -#: rest_framework/fields.py:862 +#: fields.py:862 msgid "Expected a date but got a datetime." msgstr "" -#: rest_framework/fields.py:919 +#: fields.py:919 msgid "Time has wrong format. Use one of these formats instead: {format}." msgstr "" -#: rest_framework/fields.py:975 rest_framework/fields.py:1019 +#: fields.py:975 fields.py:1019 msgid "`{input}` is not a valid choice." msgstr "" -#: rest_framework/fields.py:1020 rest_framework/fields.py:1121 -#: rest_framework/serializers.py:476 +#: fields.py:1020 fields.py:1121 serializers.py:476 msgid "Expected a list of items but got type `{input_type}`." msgstr "" -#: rest_framework/fields.py:1050 +#: fields.py:1050 msgid "No file was submitted." msgstr "" -#: rest_framework/fields.py:1051 +#: fields.py:1051 msgid "The submitted data was not a file. Check the encoding type on the form." msgstr "" -#: rest_framework/fields.py:1052 +#: fields.py:1052 msgid "No filename could be determined." msgstr "" -#: rest_framework/fields.py:1053 +#: fields.py:1053 msgid "The submitted file is empty." msgstr "" -#: rest_framework/fields.py:1054 +#: fields.py:1054 msgid "" "Ensure this filename has at most {max_length} characters (it has {length})." msgstr "" -#: rest_framework/fields.py:1096 +#: fields.py:1096 msgid "" "Upload a valid image. The file you uploaded was either not an image or a " "corrupted image." msgstr "" -#: rest_framework/generics.py:122 +#: generics.py:123 msgid "" "Choose a valid page number. Page numbers must be a whole number, or must be " "the string 'last'." msgstr "" -#: rest_framework/generics.py:126 -#, python-format -msgid "Invalid page (%(page_number)s): %(message)s" +#: generics.py:129 +msgid "Invalid page ({page_number}): {message}" msgstr "" -#: rest_framework/relations.py:132 +#: relations.py:132 msgid "Invalid pk '{pk_value}' - object does not exist." msgstr "" -#: rest_framework/relations.py:133 +#: relations.py:133 msgid "Incorrect type. Expected pk value, received {data_type}." msgstr "" -#: rest_framework/relations.py:156 +#: relations.py:156 msgid "Invalid hyperlink - No URL match" msgstr "" -#: rest_framework/relations.py:157 +#: relations.py:157 msgid "Invalid hyperlink - Incorrect URL match." msgstr "" -#: rest_framework/relations.py:158 +#: relations.py:158 msgid "Invalid hyperlink - Object does not exist." msgstr "" -#: rest_framework/relations.py:159 +#: relations.py:159 msgid "Incorrect type. Expected URL string, received {data_type}." msgstr "" -#: rest_framework/relations.py:294 +#: relations.py:294 msgid "Object with {slug_name}={value} does not exist." msgstr "" -#: rest_framework/relations.py:295 +#: relations.py:295 msgid "Invalid value." msgstr "" -#: rest_framework/serializers.py:299 +#: serializers.py:299 msgid "Invalid data. Expected a dictionary, but got {datatype}." msgstr "" -#: rest_framework/validators.py:22 +#: validators.py:22 msgid "This field must be unique." msgstr "" -#: rest_framework/validators.py:76 +#: validators.py:76 msgid "The fields {field_names} must make a unique set." msgstr "" -#: rest_framework/validators.py:219 +#: validators.py:219 msgid "This field must be unique for the \"{date_field}\" date." msgstr "" -#: rest_framework/validators.py:234 +#: validators.py:234 msgid "This field must be unique for the \"{date_field}\" month." msgstr "" -#: rest_framework/validators.py:247 +#: validators.py:247 msgid "This field must be unique for the \"{date_field}\" year." msgstr "" -#: rest_framework/versioning.py:39 +#: versioning.py:39 msgid "Invalid version in 'Accept' header." msgstr "" -#: rest_framework/versioning.py:70 rest_framework/versioning.py:112 +#: versioning.py:70 versioning.py:112 msgid "Invalid version in URL path." msgstr "" -#: rest_framework/versioning.py:138 +#: versioning.py:138 msgid "Invalid version in hostname." msgstr "" -#: rest_framework/versioning.py:160 +#: versioning.py:160 msgid "Invalid version in query parameter." msgstr "" From fe5d93c8cbc5f3a9b1b6715208c70f485be68bdf Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 11:44:18 +0000 Subject: [PATCH 13/21] remove hardcoded page number --- rest_framework/generics.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index fe92355d9..7c4d5e958 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -122,7 +122,6 @@ class GenericAPIView(views.APIView): else: raise NotFound(_("Choose a valid page number. Page numbers must be a whole number, or must be the string 'last'.")) - page_number = -1 try: page = paginator.page(page_number) except InvalidPage as exc: From 4c32083b8b59a50877633910055313dad7bb117e Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 12:01:11 +0000 Subject: [PATCH 14/21] use double quotes for user visible strings; end user visible strings in full stops; add some missing translation tags --- rest_framework/authentication.py | 17 ++++--- rest_framework/authtoken/serializers.py | 6 +-- rest_framework/exceptions.py | 24 ++++----- rest_framework/fields.py | 68 ++++++++++++------------- rest_framework/generics.py | 2 +- rest_framework/relations.py | 16 +++--- rest_framework/serializers.py | 4 +- rest_framework/validators.py | 14 ++--- rest_framework/versioning.py | 8 +-- 9 files changed, 80 insertions(+), 79 deletions(-) diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index 124ef68ac..7e86a7b9f 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import base64 from django.contrib.auth import authenticate from django.middleware.csrf import CsrfViewMiddleware +from django.utils.translation import ugettext_lazy as _ from rest_framework import exceptions, HTTP_HEADER_ENCODING from rest_framework.authtoken.models import Token @@ -65,16 +66,16 @@ class BasicAuthentication(BaseAuthentication): return None if len(auth) == 1: - msg = 'Invalid basic header. No credentials provided.' + msg = _("Invalid basic header. No credentials provided.") raise exceptions.AuthenticationFailed(msg) elif len(auth) > 2: - msg = 'Invalid basic header. Credentials string should not contain spaces.' + msg = _("Invalid basic header. Credentials string should not contain spaces.") raise exceptions.AuthenticationFailed(msg) try: auth_parts = base64.b64decode(auth[1]).decode(HTTP_HEADER_ENCODING).partition(':') except (TypeError, UnicodeDecodeError): - msg = 'Invalid basic header. Credentials not correctly base64 encoded' + msg = _("Invalid basic header. Credentials not correctly base64 encoded.") raise exceptions.AuthenticationFailed(msg) userid, password = auth_parts[0], auth_parts[2] @@ -86,7 +87,7 @@ class BasicAuthentication(BaseAuthentication): """ user = authenticate(username=userid, password=password) if user is None or not user.is_active: - raise exceptions.AuthenticationFailed('Invalid username/password') + raise exceptions.AuthenticationFailed(_("Invalid username/password.")) return (user, None) def authenticate_header(self, request): @@ -152,10 +153,10 @@ class TokenAuthentication(BaseAuthentication): return None if len(auth) == 1: - msg = 'Invalid token header. No credentials provided.' + msg = _("Invalid token header. No credentials provided.") raise exceptions.AuthenticationFailed(msg) elif len(auth) > 2: - msg = 'Invalid token header. Token string should not contain spaces.' + msg = _("Invalid token header. Token string should not contain spaces.") raise exceptions.AuthenticationFailed(msg) return self.authenticate_credentials(auth[1]) @@ -164,10 +165,10 @@ class TokenAuthentication(BaseAuthentication): try: token = self.model.objects.get(key=key) except self.model.DoesNotExist: - raise exceptions.AuthenticationFailed('Invalid token') + raise exceptions.AuthenticationFailed(_("Invalid token")) if not token.user.is_active: - raise exceptions.AuthenticationFailed('User inactive or deleted') + raise exceptions.AuthenticationFailed(_("User inactive or deleted")) return (token.user, token) diff --git a/rest_framework/authtoken/serializers.py b/rest_framework/authtoken/serializers.py index f31dded17..78fe6a117 100644 --- a/rest_framework/authtoken/serializers.py +++ b/rest_framework/authtoken/serializers.py @@ -17,13 +17,13 @@ class AuthTokenSerializer(serializers.Serializer): if user: if not user.is_active: - msg = _('User account is disabled.') + msg = _("User account is disabled.") raise exceptions.ValidationError(msg) else: - msg = _('Unable to log in with provided credentials.') + msg = _("Unable to log in with provided credentials.") raise exceptions.ValidationError(msg) else: - msg = _('Must include "username" and "password"') + msg = _("Must include \"username\" and \"password\"") raise exceptions.ValidationError(msg) attrs['user'] = user diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index dfc57293e..3ca8e5380 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -35,7 +35,7 @@ class APIException(Exception): Subclasses should provide `.status_code` and `.default_detail` properties. """ status_code = status.HTTP_500_INTERNAL_SERVER_ERROR - default_detail = _('A server error occurred.') + default_detail = _("A server error occurred.") def __init__(self, detail=None): if detail is not None: @@ -52,7 +52,7 @@ class APIException(Exception): # built in `ValidationError`. For example: # # from rest_framework import serializers -# raise serializers.ValidationError('Value was invalid') +# raise serializers.ValidationError("Value was invalid") class ValidationError(APIException): status_code = status.HTTP_400_BAD_REQUEST @@ -70,32 +70,32 @@ class ValidationError(APIException): class ParseError(APIException): status_code = status.HTTP_400_BAD_REQUEST - default_detail = _('Malformed request.') + default_detail = _("Malformed request.") class AuthenticationFailed(APIException): status_code = status.HTTP_401_UNAUTHORIZED - default_detail = _('Incorrect authentication credentials.') + default_detail = _("Incorrect authentication credentials.") class NotAuthenticated(APIException): status_code = status.HTTP_401_UNAUTHORIZED - default_detail = _('Authentication credentials were not provided.') + default_detail = _("Authentication credentials were not provided.") class PermissionDenied(APIException): status_code = status.HTTP_403_FORBIDDEN - default_detail = _('You do not have permission to perform this action.') + default_detail = _("You do not have permission to perform this action.") class NotFound(APIException): status_code = status.HTTP_404_NOT_FOUND - default_detail = _('Not found.') + default_detail = _("Not found.") class MethodNotAllowed(APIException): status_code = status.HTTP_405_METHOD_NOT_ALLOWED - default_detail = _("Method {method} not allowed.") + default_detail = _("Method '{method}' not allowed.") def __init__(self, method, detail=None): if detail is not None: @@ -106,7 +106,7 @@ class MethodNotAllowed(APIException): class NotAcceptable(APIException): status_code = status.HTTP_406_NOT_ACCEPTABLE - default_detail = _('Could not satisfy the request Accept header.') + default_detail = _("Could not satisfy the request Accept header.") def __init__(self, detail=None, available_renderers=None): if detail is not None: @@ -131,9 +131,9 @@ class UnsupportedMediaType(APIException): class Throttled(APIException): status_code = status.HTTP_429_TOO_MANY_REQUESTS - default_detail = _('Request was throttled.') - extra_detail_singular = 'Expected available in {wait} second.' - extra_detail_plural = 'Expected available in {wait} seconds.' + default_detail = _("Request was throttled.") + extra_detail_singular = "Expected available in {wait} second." + extra_detail_plural = "Expected available in {wait} seconds." def __init__(self, wait=None, detail=None): if detail is not None: diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 0ff2b0733..8a781b356 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -149,8 +149,8 @@ class Field(object): _creation_counter = 0 default_error_messages = { - 'required': _('This field is required.'), - 'null': _('This field may not be null.') + 'required': _("This field is required."), + 'null': _("This field may not be null.") } default_validators = [] default_empty_html = empty @@ -477,7 +477,7 @@ class Field(object): class BooleanField(Field): default_error_messages = { - 'invalid': _('`{input}` is not a valid boolean.') + 'invalid': _("`{input}` is not a valid boolean.") } default_empty_html = False initial = False @@ -505,7 +505,7 @@ class BooleanField(Field): class NullBooleanField(Field): default_error_messages = { - 'invalid': _('`{input}` is not a valid boolean.') + 'invalid': _("`{input}` is not a valid boolean.") } initial = None TRUE_VALUES = set(('t', 'T', 'true', 'True', 'TRUE', '1', 1, True)) @@ -540,9 +540,9 @@ class NullBooleanField(Field): class CharField(Field): default_error_messages = { - 'blank': _('This field may not be blank.'), - 'max_length': _('Ensure this field has no more than {max_length} characters.'), - 'min_length': _('Ensure this field has at least {min_length} characters.') + 'blank': _("This field may not be blank."), + 'max_length': _("Ensure this field has no more than {max_length} characters."), + 'min_length': _("Ensure this field has at least {min_length} characters.") } initial = '' coerce_blank_to_null = False @@ -584,7 +584,7 @@ class CharField(Field): class EmailField(CharField): default_error_messages = { - 'invalid': _('Enter a valid email address.') + 'invalid': _("Enter a valid email address.") } def __init__(self, **kwargs): @@ -601,7 +601,7 @@ class EmailField(CharField): class RegexField(CharField): default_error_messages = { - 'invalid': _('This value does not match the required pattern.') + 'invalid': _("This value does not match the required pattern.") } def __init__(self, regex, **kwargs): @@ -637,10 +637,10 @@ class URLField(CharField): class IntegerField(Field): default_error_messages = { - 'invalid': _('A valid integer is required.'), - 'max_value': _('Ensure this value is less than or equal to {max_value}.'), - 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), - 'max_string_length': _('String value too large.') + 'invalid': _("A valid integer is required."), + 'max_value': _("Ensure this value is less than or equal to {max_value}."), + 'min_value': _("Ensure this value is greater than or equal to {min_value}."), + 'max_string_length': _("String value too large.") } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -672,9 +672,9 @@ class IntegerField(Field): class FloatField(Field): default_error_messages = { 'invalid': _("A valid number is required."), - 'max_value': _('Ensure this value is less than or equal to {max_value}.'), - 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), - 'max_string_length': _('String value too large.') + 'max_value': _("Ensure this value is less than or equal to {max_value}."), + 'min_value': _("Ensure this value is greater than or equal to {min_value}."), + 'max_string_length': _("String value too large.") } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -704,13 +704,13 @@ class FloatField(Field): class DecimalField(Field): default_error_messages = { - 'invalid': _('A valid number is required.'), - 'max_value': _('Ensure this value is less than or equal to {max_value}.'), - 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), - 'max_digits': _('Ensure that there are no more than {max_digits} digits in total.'), - 'max_decimal_places': _('Ensure that there are no more than {max_decimal_places} decimal places.'), - 'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.'), - 'max_string_length': _('String value too large.') + 'invalid': _("A valid number is required."), + 'max_value': _("Ensure this value is less than or equal to {max_value}."), + 'min_value': _("Ensure this value is greater than or equal to {min_value}."), + 'max_digits': _("Ensure that there are no more than {max_digits} digits in total."), + 'max_decimal_places': _("Ensure that there are no more than {max_decimal_places} decimal places."), + 'max_whole_digits': _("Ensure that there are no more than {max_whole_digits} digits before the decimal point."), + 'max_string_length': _("String value too large.") } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -793,8 +793,8 @@ class DecimalField(Field): class DateTimeField(Field): default_error_messages = { - 'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}.'), - 'date': _('Expected a datetime but got a date.'), + 'invalid': _("Datetime has wrong format. Use one of these formats instead: {format}."), + 'date': _("Expected a datetime but got a date."), } format = api_settings.DATETIME_FORMAT input_formats = api_settings.DATETIME_INPUT_FORMATS @@ -858,8 +858,8 @@ class DateTimeField(Field): class DateField(Field): default_error_messages = { - 'invalid': _('Date has wrong format. Use one of these formats instead: {format}.'), - 'datetime': _('Expected a date but got a datetime.'), + 'invalid': _("Date has wrong format. Use one of these formats instead: {format}."), + 'datetime': _("Expected a date but got a datetime."), } format = api_settings.DATE_FORMAT input_formats = api_settings.DATE_INPUT_FORMATS @@ -916,7 +916,7 @@ class DateField(Field): class TimeField(Field): default_error_messages = { - 'invalid': _('Time has wrong format. Use one of these formats instead: {format}.'), + 'invalid': _("Time has wrong format. Use one of these formats instead: {format}."), } format = api_settings.TIME_FORMAT input_formats = api_settings.TIME_INPUT_FORMATS @@ -972,7 +972,7 @@ class TimeField(Field): class ChoiceField(Field): default_error_messages = { - 'invalid_choice': _('`{input}` is not a valid choice.') + 'invalid_choice': _("`{input}` is not a valid choice.") } def __init__(self, choices, **kwargs): @@ -1016,8 +1016,8 @@ class ChoiceField(Field): class MultipleChoiceField(ChoiceField): default_error_messages = { - 'invalid_choice': _('`{input}` is not a valid choice.'), - 'not_a_list': _('Expected a list of items but got type `{input_type}`.') + 'invalid_choice': _("`{input}` is not a valid choice."), + 'not_a_list': _("Expected a list of items but got type `{input_type}`.") } default_empty_html = [] @@ -1051,7 +1051,7 @@ class FileField(Field): 'invalid': _("The submitted data was not a file. Check the encoding type on the form."), 'no_name': _("No filename could be determined."), 'empty': _("The submitted file is empty."), - 'max_length': _('Ensure this filename has at most {max_length} characters (it has {length}).'), + 'max_length': _("Ensure this filename has at most {max_length} characters (it has {length})."), } use_url = api_settings.UPLOADED_FILES_USE_URL @@ -1118,7 +1118,7 @@ class ListField(Field): child = None initial = [] default_error_messages = { - 'not_a_list': _('Expected a list of items but got type `{input_type}`.') + 'not_a_list': _("Expected a list of items but got type `{input_type}`.") } def __init__(self, *args, **kwargs): @@ -1249,7 +1249,7 @@ class ModelField(Field): that do not have a serializer field to be mapped to. """ default_error_messages = { - 'max_length': _('Ensure this field has no more than {max_length} characters.'), + 'max_length': _("Ensure this field has no more than {max_length} characters."), } def __init__(self, model_field, **kwargs): diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 7c4d5e958..c7053d8f3 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -125,7 +125,7 @@ class GenericAPIView(views.APIView): try: page = paginator.page(page_number) except InvalidPage as exc: - error_format = _('Invalid page ({page_number}): {message}') + error_format = _("Invalid page ({page_number}): {message}.") raise NotFound(error_format.format( page_number=page_number, message=six.text_type(exc) )) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 7b119291d..3737b21fa 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -128,9 +128,9 @@ class StringRelatedField(RelatedField): class PrimaryKeyRelatedField(RelatedField): default_error_messages = { - 'required': _('This field is required.'), + 'required': _("This field is required."), 'does_not_exist': _("Invalid pk '{pk_value}' - object does not exist."), - 'incorrect_type': _('Incorrect type. Expected pk value, received {data_type}.'), + 'incorrect_type': _("Incorrect type. Expected pk value, received {data_type}."), } def use_pk_only_optimization(self): @@ -152,11 +152,11 @@ class HyperlinkedRelatedField(RelatedField): lookup_field = 'pk' default_error_messages = { - 'required': _('This field is required.'), - 'no_match': _('Invalid hyperlink - No URL match'), - 'incorrect_match': _('Invalid hyperlink - Incorrect URL match.'), - 'does_not_exist': _('Invalid hyperlink - Object does not exist.'), - 'incorrect_type': _('Incorrect type. Expected URL string, received {data_type}.'), + 'required': _("This field is required."), + 'no_match': _("Invalid hyperlink - No URL match."), + 'incorrect_match': _("Invalid hyperlink - Incorrect URL match."), + 'does_not_exist': _("Invalid hyperlink - Object does not exist."), + 'incorrect_type': _("Incorrect type. Expected URL string, received {data_type}."), } def __init__(self, view_name=None, **kwargs): @@ -292,7 +292,7 @@ class SlugRelatedField(RelatedField): default_error_messages = { 'does_not_exist': _("Object with {slug_name}={value} does not exist."), - 'invalid': _('Invalid value.'), + 'invalid': _("Invalid value."), } def __init__(self, slug_field=None, **kwargs): diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 623ed5865..9d7c8884a 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -296,7 +296,7 @@ def get_validation_error_detail(exc): @six.add_metaclass(SerializerMetaclass) class Serializer(BaseSerializer): default_error_messages = { - 'invalid': _('Invalid data. Expected a dictionary, but got {datatype}.') + 'invalid': _("Invalid data. Expected a dictionary, but got {datatype}.") } @property @@ -473,7 +473,7 @@ class ListSerializer(BaseSerializer): many = True default_error_messages = { - 'not_a_list': _('Expected a list of items but got type `{input_type}`.') + 'not_a_list': _("Expected a list of items but got type `{input_type}`.") } def __init__(self, *args, **kwargs): diff --git a/rest_framework/validators.py b/rest_framework/validators.py index e3719b8d5..cf6f07180 100644 --- a/rest_framework/validators.py +++ b/rest_framework/validators.py @@ -19,7 +19,7 @@ class UniqueValidator: Should be applied to an individual field on the serializer. """ - message = _('This field must be unique.') + message = _("This field must be unique.") def __init__(self, queryset, message=None): self.queryset = queryset @@ -73,8 +73,8 @@ class UniqueTogetherValidator: Should be applied to the serializer class, not to an individual field. """ - message = _('The fields {field_names} must make a unique set.') - missing_message = _('This field is required.') + message = _("The fields {field_names} must make a unique set.") + missing_message = _("This field is required.") def __init__(self, queryset, fields, message=None): self.queryset = queryset @@ -152,7 +152,7 @@ class UniqueTogetherValidator: class BaseUniqueForValidator: message = None - missing_message = _('This field is required.') + missing_message = _("This field is required.") def __init__(self, queryset, field, date_field, message=None): self.queryset = queryset @@ -216,7 +216,7 @@ class BaseUniqueForValidator: class UniqueForDateValidator(BaseUniqueForValidator): - message = _('This field must be unique for the "{date_field}" date.') + message = _("This field must be unique for the \"{date_field}\" date.") def filter_queryset(self, attrs, queryset): value = attrs[self.field] @@ -231,7 +231,7 @@ class UniqueForDateValidator(BaseUniqueForValidator): class UniqueForMonthValidator(BaseUniqueForValidator): - message = _('This field must be unique for the "{date_field}" month.') + message = _("This field must be unique for the \"{date_field}\" month.") def filter_queryset(self, attrs, queryset): value = attrs[self.field] @@ -244,7 +244,7 @@ class UniqueForMonthValidator(BaseUniqueForValidator): class UniqueForYearValidator(BaseUniqueForValidator): - message = _('This field must be unique for the "{date_field}" year.') + message = _("This field must be unique for the \"{date_field}\" year.") def filter_queryset(self, attrs, queryset): value = attrs[self.field] diff --git a/rest_framework/versioning.py b/rest_framework/versioning.py index 440efd139..587ba9f13 100644 --- a/rest_framework/versioning.py +++ b/rest_framework/versioning.py @@ -67,7 +67,7 @@ class URLPathVersioning(BaseVersioning): Host: example.com Accept: application/json """ - invalid_version_message = _('Invalid version in URL path.') + invalid_version_message = _("Invalid version in URL path.") def determine_version(self, request, *args, **kwargs): version = kwargs.get(self.version_param, self.default_version) @@ -109,7 +109,7 @@ class NamespaceVersioning(BaseVersioning): Host: example.com Accept: application/json """ - invalid_version_message = _('Invalid version in URL path.') + invalid_version_message = _("Invalid version in URL path.") def determine_version(self, request, *args, **kwargs): resolver_match = getattr(request, 'resolver_match', None) @@ -135,7 +135,7 @@ class HostNameVersioning(BaseVersioning): Accept: application/json """ hostname_regex = re.compile(r'^([a-zA-Z0-9]+)\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$') - invalid_version_message = _('Invalid version in hostname.') + invalid_version_message = _("Invalid version in hostname.") def determine_version(self, request, *args, **kwargs): hostname, seperator, port = request.get_host().partition(':') @@ -157,7 +157,7 @@ class QueryParameterVersioning(BaseVersioning): Host: example.com Accept: application/json """ - invalid_version_message = _('Invalid version in query parameter.') + invalid_version_message = _("Invalid version in query parameter.") def determine_version(self, request, *args, **kwargs): version = request.query_params.get(self.version_param) From 662a907bdf821c29b42b60ce2b44eb8149a85bd7 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 12:02:04 +0000 Subject: [PATCH 15/21] update source strings --- .../locale/en_US/LC_MESSAGES/django.po | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/rest_framework/locale/en_US/LC_MESSAGES/django.po b/rest_framework/locale/en_US/LC_MESSAGES/django.po index 7c5a6c02b..5d0d3a045 100644 --- a/rest_framework/locale/en_US/LC_MESSAGES/django.po +++ b/rest_framework/locale/en_US/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2015-01-07 11:40+0000\n" +"POT-Creation-Date: 2015-01-07 11:58+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,6 +17,38 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" +#: authentication.py:69 +msgid "Invalid basic header. No credentials provided." +msgstr "" + +#: authentication.py:72 +msgid "Invalid basic header. Credentials string should not contain spaces." +msgstr "" + +#: authentication.py:78 +msgid "Invalid basic header. Credentials not correctly base64 encoded." +msgstr "" + +#: authentication.py:90 +msgid "Invalid username/password." +msgstr "" + +#: authentication.py:156 +msgid "Invalid token header. No credentials provided." +msgstr "" + +#: authentication.py:159 +msgid "Invalid token header. Token string should not contain spaces." +msgstr "" + +#: authentication.py:168 +msgid "Invalid token" +msgstr "" + +#: authentication.py:171 +msgid "User inactive or deleted" +msgstr "" + #: authtoken/serializers.py:20 msgid "User account is disabled." msgstr "" @@ -54,7 +86,7 @@ msgid "Not found." msgstr "" #: exceptions.py:98 -msgid "Method {method} not allowed." +msgid "Method '{method}' not allowed." msgstr "" #: exceptions.py:109 @@ -206,8 +238,8 @@ msgid "" "the string 'last'." msgstr "" -#: generics.py:129 -msgid "Invalid page ({page_number}): {message}" +#: generics.py:128 +msgid "Invalid page ({page_number}): {message}." msgstr "" #: relations.py:132 @@ -219,7 +251,7 @@ msgid "Incorrect type. Expected pk value, received {data_type}." msgstr "" #: relations.py:156 -msgid "Invalid hyperlink - No URL match" +msgid "Invalid hyperlink - No URL match." msgstr "" #: relations.py:157 From 9a4267049ba37883e3e0c21b5d453b9551343b8d Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 12:33:37 +0000 Subject: [PATCH 16/21] use double quotes in user messages --- rest_framework/exceptions.py | 4 ++-- rest_framework/fields.py | 2 +- rest_framework/generics.py | 4 ++-- rest_framework/relations.py | 2 +- rest_framework/versioning.py | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 3ca8e5380..f8a43871b 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -95,7 +95,7 @@ class NotFound(APIException): class MethodNotAllowed(APIException): status_code = status.HTTP_405_METHOD_NOT_ALLOWED - default_detail = _("Method '{method}' not allowed.") + default_detail = _("Method \"{method}\" not allowed.") def __init__(self, method, detail=None): if detail is not None: @@ -118,7 +118,7 @@ class NotAcceptable(APIException): class UnsupportedMediaType(APIException): status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE - default_detail = _("Unsupported media type '{media_type}' in request.") + default_detail = _("Unsupported media type \"{media_type}\" in request.") def __init__(self, media_type, detail=None): if detail is not None: diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 8a781b356..279446088 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -612,7 +612,7 @@ class RegexField(CharField): class SlugField(CharField): default_error_messages = { - 'invalid': _("Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.") + 'invalid': _("Enter a valid \"slug\" consisting of letters, numbers, underscores or hyphens.") } def __init__(self, **kwargs): diff --git a/rest_framework/generics.py b/rest_framework/generics.py index c7053d8f3..738ba544a 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -120,12 +120,12 @@ class GenericAPIView(views.APIView): if page == 'last': page_number = paginator.num_pages else: - raise NotFound(_("Choose a valid page number. Page numbers must be a whole number, or must be the string 'last'.")) + raise NotFound(_("Choose a valid page number. Page numbers must be a whole number, or must be the string \"last\".")) try: page = paginator.page(page_number) except InvalidPage as exc: - error_format = _("Invalid page ({page_number}): {message}.") + error_format = _("Invalid page \"{page_number}\": {message}.") raise NotFound(error_format.format( page_number=page_number, message=six.text_type(exc) )) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 3737b21fa..42b624e7d 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -129,7 +129,7 @@ class StringRelatedField(RelatedField): class PrimaryKeyRelatedField(RelatedField): default_error_messages = { 'required': _("This field is required."), - 'does_not_exist': _("Invalid pk '{pk_value}' - object does not exist."), + 'does_not_exist': _("Invalid pk \"{pk_value}\" - object does not exist."), 'incorrect_type': _("Incorrect type. Expected pk value, received {data_type}."), } diff --git a/rest_framework/versioning.py b/rest_framework/versioning.py index 587ba9f13..819c32df8 100644 --- a/rest_framework/versioning.py +++ b/rest_framework/versioning.py @@ -36,7 +36,7 @@ class AcceptHeaderVersioning(BaseVersioning): Host: example.com Accept: application/json; version=1.0 """ - invalid_version_message = _("Invalid version in 'Accept' header.") + invalid_version_message = _("Invalid version in \"Accept\" header.") def determine_version(self, request, *args, **kwargs): media_type = _MediaType(request.accepted_media_type) From 91e316f7810157474d6246cd0024bd7f7cc31ff7 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 12:46:23 +0000 Subject: [PATCH 17/21] prefer single quotes in source and double quotes in user visible strings; add some missing full stops to user visible strings --- rest_framework/authentication.py | 16 ++-- rest_framework/authtoken/serializers.py | 6 +- rest_framework/exceptions.py | 24 +++--- rest_framework/fields.py | 82 +++++++++---------- rest_framework/generics.py | 4 +- .../locale/en_US/LC_MESSAGES/django.po | 23 +++--- rest_framework/relations.py | 20 ++--- rest_framework/serializers.py | 4 +- rest_framework/validators.py | 14 ++-- rest_framework/versioning.py | 10 +-- tests/test_fields.py | 2 +- tests/test_generics.py | 6 +- tests/test_relations.py | 2 +- 13 files changed, 107 insertions(+), 106 deletions(-) diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index 7e86a7b9f..11db05855 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -66,16 +66,16 @@ class BasicAuthentication(BaseAuthentication): return None if len(auth) == 1: - msg = _("Invalid basic header. No credentials provided.") + msg = _('Invalid basic header. No credentials provided.') raise exceptions.AuthenticationFailed(msg) elif len(auth) > 2: - msg = _("Invalid basic header. Credentials string should not contain spaces.") + msg = _('Invalid basic header. Credentials string should not contain spaces.') raise exceptions.AuthenticationFailed(msg) try: auth_parts = base64.b64decode(auth[1]).decode(HTTP_HEADER_ENCODING).partition(':') except (TypeError, UnicodeDecodeError): - msg = _("Invalid basic header. Credentials not correctly base64 encoded.") + msg = _('Invalid basic header. Credentials not correctly base64 encoded.') raise exceptions.AuthenticationFailed(msg) userid, password = auth_parts[0], auth_parts[2] @@ -87,7 +87,7 @@ class BasicAuthentication(BaseAuthentication): """ user = authenticate(username=userid, password=password) if user is None or not user.is_active: - raise exceptions.AuthenticationFailed(_("Invalid username/password.")) + raise exceptions.AuthenticationFailed(_('Invalid username/password.')) return (user, None) def authenticate_header(self, request): @@ -153,10 +153,10 @@ class TokenAuthentication(BaseAuthentication): return None if len(auth) == 1: - msg = _("Invalid token header. No credentials provided.") + msg = _('Invalid token header. No credentials provided.') raise exceptions.AuthenticationFailed(msg) elif len(auth) > 2: - msg = _("Invalid token header. Token string should not contain spaces.") + msg = _('Invalid token header. Token string should not contain spaces.') raise exceptions.AuthenticationFailed(msg) return self.authenticate_credentials(auth[1]) @@ -165,10 +165,10 @@ class TokenAuthentication(BaseAuthentication): try: token = self.model.objects.get(key=key) except self.model.DoesNotExist: - raise exceptions.AuthenticationFailed(_("Invalid token")) + raise exceptions.AuthenticationFailed(_('Invalid token.')) if not token.user.is_active: - raise exceptions.AuthenticationFailed(_("User inactive or deleted")) + raise exceptions.AuthenticationFailed(_('User inactive or deleted.')) return (token.user, token) diff --git a/rest_framework/authtoken/serializers.py b/rest_framework/authtoken/serializers.py index 78fe6a117..37ade255d 100644 --- a/rest_framework/authtoken/serializers.py +++ b/rest_framework/authtoken/serializers.py @@ -17,13 +17,13 @@ class AuthTokenSerializer(serializers.Serializer): if user: if not user.is_active: - msg = _("User account is disabled.") + msg = _('User account is disabled.') raise exceptions.ValidationError(msg) else: - msg = _("Unable to log in with provided credentials.") + msg = _('Unable to log in with provided credentials.') raise exceptions.ValidationError(msg) else: - msg = _("Must include \"username\" and \"password\"") + msg = _('Must include "username" and "password".') raise exceptions.ValidationError(msg) attrs['user'] = user diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index f8a43871b..f62c9fe39 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -35,7 +35,7 @@ class APIException(Exception): Subclasses should provide `.status_code` and `.default_detail` properties. """ status_code = status.HTTP_500_INTERNAL_SERVER_ERROR - default_detail = _("A server error occurred.") + default_detail = _('A server error occurred.') def __init__(self, detail=None): if detail is not None: @@ -70,32 +70,32 @@ class ValidationError(APIException): class ParseError(APIException): status_code = status.HTTP_400_BAD_REQUEST - default_detail = _("Malformed request.") + default_detail = _('Malformed request.') class AuthenticationFailed(APIException): status_code = status.HTTP_401_UNAUTHORIZED - default_detail = _("Incorrect authentication credentials.") + default_detail = _('Incorrect authentication credentials.') class NotAuthenticated(APIException): status_code = status.HTTP_401_UNAUTHORIZED - default_detail = _("Authentication credentials were not provided.") + default_detail = _('Authentication credentials were not provided.') class PermissionDenied(APIException): status_code = status.HTTP_403_FORBIDDEN - default_detail = _("You do not have permission to perform this action.") + default_detail = _('You do not have permission to perform this action.') class NotFound(APIException): status_code = status.HTTP_404_NOT_FOUND - default_detail = _("Not found.") + default_detail = _('Not found.') class MethodNotAllowed(APIException): status_code = status.HTTP_405_METHOD_NOT_ALLOWED - default_detail = _("Method \"{method}\" not allowed.") + default_detail = _('Method "{method}" not allowed.') def __init__(self, method, detail=None): if detail is not None: @@ -106,7 +106,7 @@ class MethodNotAllowed(APIException): class NotAcceptable(APIException): status_code = status.HTTP_406_NOT_ACCEPTABLE - default_detail = _("Could not satisfy the request Accept header.") + default_detail = _('Could not satisfy the request Accept header.') def __init__(self, detail=None, available_renderers=None): if detail is not None: @@ -118,7 +118,7 @@ class NotAcceptable(APIException): class UnsupportedMediaType(APIException): status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE - default_detail = _("Unsupported media type \"{media_type}\" in request.") + default_detail = _('Unsupported media type "{media_type}" in request.') def __init__(self, media_type, detail=None): if detail is not None: @@ -131,9 +131,9 @@ class UnsupportedMediaType(APIException): class Throttled(APIException): status_code = status.HTTP_429_TOO_MANY_REQUESTS - default_detail = _("Request was throttled.") - extra_detail_singular = "Expected available in {wait} second." - extra_detail_plural = "Expected available in {wait} seconds." + default_detail = _('Request was throttled.') + extra_detail_singular = 'Expected available in {wait} second.' + extra_detail_plural = 'Expected available in {wait} seconds.' def __init__(self, wait=None, detail=None): if detail is not None: diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 279446088..76101608e 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -149,8 +149,8 @@ class Field(object): _creation_counter = 0 default_error_messages = { - 'required': _("This field is required."), - 'null': _("This field may not be null.") + 'required': _('This field is required.'), + 'null': _('This field may not be null.') } default_validators = [] default_empty_html = empty @@ -477,7 +477,7 @@ class Field(object): class BooleanField(Field): default_error_messages = { - 'invalid': _("`{input}` is not a valid boolean.") + 'invalid': _('`{input}` is not a valid boolean.') } default_empty_html = False initial = False @@ -505,7 +505,7 @@ class BooleanField(Field): class NullBooleanField(Field): default_error_messages = { - 'invalid': _("`{input}` is not a valid boolean.") + 'invalid': _('`{input}` is not a valid boolean.') } initial = None TRUE_VALUES = set(('t', 'T', 'true', 'True', 'TRUE', '1', 1, True)) @@ -540,9 +540,9 @@ class NullBooleanField(Field): class CharField(Field): default_error_messages = { - 'blank': _("This field may not be blank."), - 'max_length': _("Ensure this field has no more than {max_length} characters."), - 'min_length': _("Ensure this field has at least {min_length} characters.") + 'blank': _('This field may not be blank.'), + 'max_length': _('Ensure this field has no more than {max_length} characters.'), + 'min_length': _('Ensure this field has at least {min_length} characters.') } initial = '' coerce_blank_to_null = False @@ -584,7 +584,7 @@ class CharField(Field): class EmailField(CharField): default_error_messages = { - 'invalid': _("Enter a valid email address.") + 'invalid': _('Enter a valid email address.') } def __init__(self, **kwargs): @@ -601,7 +601,7 @@ class EmailField(CharField): class RegexField(CharField): default_error_messages = { - 'invalid': _("This value does not match the required pattern.") + 'invalid': _('This value does not match the required pattern.') } def __init__(self, regex, **kwargs): @@ -612,7 +612,7 @@ class RegexField(CharField): class SlugField(CharField): default_error_messages = { - 'invalid': _("Enter a valid \"slug\" consisting of letters, numbers, underscores or hyphens.") + 'invalid': _('Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.') } def __init__(self, **kwargs): @@ -624,7 +624,7 @@ class SlugField(CharField): class URLField(CharField): default_error_messages = { - 'invalid': _("Enter a valid URL.") + 'invalid': _('Enter a valid URL.') } def __init__(self, **kwargs): @@ -637,10 +637,10 @@ class URLField(CharField): class IntegerField(Field): default_error_messages = { - 'invalid': _("A valid integer is required."), - 'max_value': _("Ensure this value is less than or equal to {max_value}."), - 'min_value': _("Ensure this value is greater than or equal to {min_value}."), - 'max_string_length': _("String value too large.") + 'invalid': _('A valid integer is required.'), + 'max_value': _('Ensure this value is less than or equal to {max_value}.'), + 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), + 'max_string_length': _('String value too large.') } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -671,10 +671,10 @@ class IntegerField(Field): class FloatField(Field): default_error_messages = { - 'invalid': _("A valid number is required."), - 'max_value': _("Ensure this value is less than or equal to {max_value}."), - 'min_value': _("Ensure this value is greater than or equal to {min_value}."), - 'max_string_length': _("String value too large.") + 'invalid': _('A valid number is required.'), + 'max_value': _('Ensure this value is less than or equal to {max_value}.'), + 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), + 'max_string_length': _('String value too large.') } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -704,13 +704,13 @@ class FloatField(Field): class DecimalField(Field): default_error_messages = { - 'invalid': _("A valid number is required."), - 'max_value': _("Ensure this value is less than or equal to {max_value}."), - 'min_value': _("Ensure this value is greater than or equal to {min_value}."), - 'max_digits': _("Ensure that there are no more than {max_digits} digits in total."), - 'max_decimal_places': _("Ensure that there are no more than {max_decimal_places} decimal places."), - 'max_whole_digits': _("Ensure that there are no more than {max_whole_digits} digits before the decimal point."), - 'max_string_length': _("String value too large.") + 'invalid': _('A valid number is required.'), + 'max_value': _('Ensure this value is less than or equal to {max_value}.'), + 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), + 'max_digits': _('Ensure that there are no more than {max_digits} digits in total.'), + 'max_decimal_places': _('Ensure that there are no more than {max_decimal_places} decimal places.'), + 'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.'), + 'max_string_length': _('String value too large.') } MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. @@ -793,8 +793,8 @@ class DecimalField(Field): class DateTimeField(Field): default_error_messages = { - 'invalid': _("Datetime has wrong format. Use one of these formats instead: {format}."), - 'date': _("Expected a datetime but got a date."), + 'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}.'), + 'date': _('Expected a datetime but got a date.'), } format = api_settings.DATETIME_FORMAT input_formats = api_settings.DATETIME_INPUT_FORMATS @@ -858,8 +858,8 @@ class DateTimeField(Field): class DateField(Field): default_error_messages = { - 'invalid': _("Date has wrong format. Use one of these formats instead: {format}."), - 'datetime': _("Expected a date but got a datetime."), + 'invalid': _('Date has wrong format. Use one of these formats instead: {format}.'), + 'datetime': _('Expected a date but got a datetime.'), } format = api_settings.DATE_FORMAT input_formats = api_settings.DATE_INPUT_FORMATS @@ -916,7 +916,7 @@ class DateField(Field): class TimeField(Field): default_error_messages = { - 'invalid': _("Time has wrong format. Use one of these formats instead: {format}."), + 'invalid': _('Time has wrong format. Use one of these formats instead: {format}.'), } format = api_settings.TIME_FORMAT input_formats = api_settings.TIME_INPUT_FORMATS @@ -972,7 +972,7 @@ class TimeField(Field): class ChoiceField(Field): default_error_messages = { - 'invalid_choice': _("`{input}` is not a valid choice.") + 'invalid_choice': _('`{input}` is not a valid choice.') } def __init__(self, choices, **kwargs): @@ -1016,8 +1016,8 @@ class ChoiceField(Field): class MultipleChoiceField(ChoiceField): default_error_messages = { - 'invalid_choice': _("`{input}` is not a valid choice."), - 'not_a_list': _("Expected a list of items but got type `{input_type}`.") + 'invalid_choice': _('`{input}` is not a valid choice.'), + 'not_a_list': _('Expected a list of items but got type `{input_type}`.') } default_empty_html = [] @@ -1047,11 +1047,11 @@ class MultipleChoiceField(ChoiceField): class FileField(Field): default_error_messages = { - 'required': _("No file was submitted."), - 'invalid': _("The submitted data was not a file. Check the encoding type on the form."), - 'no_name': _("No filename could be determined."), - 'empty': _("The submitted file is empty."), - 'max_length': _("Ensure this filename has at most {max_length} characters (it has {length})."), + 'required': _('No file was submitted.'), + 'invalid': _('The submitted data was not a file. Check the encoding type on the form.'), + 'no_name': _('No filename could be determined.'), + 'empty': _('The submitted file is empty.'), + 'max_length': _('Ensure this filename has at most {max_length} characters (it has {length}).'), } use_url = api_settings.UPLOADED_FILES_USE_URL @@ -1118,7 +1118,7 @@ class ListField(Field): child = None initial = [] default_error_messages = { - 'not_a_list': _("Expected a list of items but got type `{input_type}`.") + 'not_a_list': _('Expected a list of items but got type `{input_type}`.') } def __init__(self, *args, **kwargs): @@ -1249,7 +1249,7 @@ class ModelField(Field): that do not have a serializer field to be mapped to. """ default_error_messages = { - 'max_length': _("Ensure this field has no more than {max_length} characters."), + 'max_length': _('Ensure this field has no more than {max_length} characters.'), } def __init__(self, model_field, **kwargs): diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 738ba544a..7ebed0327 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -120,12 +120,12 @@ class GenericAPIView(views.APIView): if page == 'last': page_number = paginator.num_pages else: - raise NotFound(_("Choose a valid page number. Page numbers must be a whole number, or must be the string \"last\".")) + raise NotFound(_('Choose a valid page number. Page numbers must be a whole number, or must be the string "last".')) try: page = paginator.page(page_number) except InvalidPage as exc: - error_format = _("Invalid page \"{page_number}\": {message}.") + error_format = _('Invalid page "{page_number}": {message}.') raise NotFound(error_format.format( page_number=page_number, message=six.text_type(exc) )) diff --git a/rest_framework/locale/en_US/LC_MESSAGES/django.po b/rest_framework/locale/en_US/LC_MESSAGES/django.po index 5d0d3a045..c8fc7f4d7 100644 --- a/rest_framework/locale/en_US/LC_MESSAGES/django.po +++ b/rest_framework/locale/en_US/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2015-01-07 11:58+0000\n" +"POT-Creation-Date: 2015-01-07 12:28+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -42,11 +42,11 @@ msgid "Invalid token header. Token string should not contain spaces." msgstr "" #: authentication.py:168 -msgid "Invalid token" +msgid "Invalid token." msgstr "" #: authentication.py:171 -msgid "User inactive or deleted" +msgid "User inactive or deleted." msgstr "" #: authtoken/serializers.py:20 @@ -58,7 +58,7 @@ msgid "Unable to log in with provided credentials." msgstr "" #: authtoken/serializers.py:26 -msgid "Must include \"username\" and \"password\"" +msgid "Must include \"username\" and \"password\"." msgstr "" #: exceptions.py:38 @@ -86,7 +86,7 @@ msgid "Not found." msgstr "" #: exceptions.py:98 -msgid "Method '{method}' not allowed." +msgid "Method \"{method}\" not allowed." msgstr "" #: exceptions.py:109 @@ -94,7 +94,7 @@ msgid "Could not satisfy the request Accept header." msgstr "" #: exceptions.py:121 -msgid "Unsupported media type '{media_type}' in request." +msgid "Unsupported media type \"{media_type}\" in request." msgstr "" #: exceptions.py:134 @@ -136,7 +136,8 @@ msgstr "" #: fields.py:615 msgid "" -"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens." +"Enter a valid \"slug\" consisting of letters, numbers, underscores or " +"hyphens." msgstr "" #: fields.py:627 @@ -235,15 +236,15 @@ msgstr "" #: generics.py:123 msgid "" "Choose a valid page number. Page numbers must be a whole number, or must be " -"the string 'last'." +"the string \"last\"." msgstr "" #: generics.py:128 -msgid "Invalid page ({page_number}): {message}." +msgid "Invalid page \"{page_number}\": {message}." msgstr "" #: relations.py:132 -msgid "Invalid pk '{pk_value}' - object does not exist." +msgid "Invalid pk \"{pk_value}\" - object does not exist." msgstr "" #: relations.py:133 @@ -299,7 +300,7 @@ msgid "This field must be unique for the \"{date_field}\" year." msgstr "" #: versioning.py:39 -msgid "Invalid version in 'Accept' header." +msgid "Invalid version in \"Accept\" header." msgstr "" #: versioning.py:70 versioning.py:112 diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 42b624e7d..05ac3d1c6 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -128,9 +128,9 @@ class StringRelatedField(RelatedField): class PrimaryKeyRelatedField(RelatedField): default_error_messages = { - 'required': _("This field is required."), - 'does_not_exist': _("Invalid pk \"{pk_value}\" - object does not exist."), - 'incorrect_type': _("Incorrect type. Expected pk value, received {data_type}."), + 'required': _('This field is required.'), + 'does_not_exist': _('Invalid pk "{pk_value}" - object does not exist.'), + 'incorrect_type': _('Incorrect type. Expected pk value, received {data_type}.'), } def use_pk_only_optimization(self): @@ -152,11 +152,11 @@ class HyperlinkedRelatedField(RelatedField): lookup_field = 'pk' default_error_messages = { - 'required': _("This field is required."), - 'no_match': _("Invalid hyperlink - No URL match."), - 'incorrect_match': _("Invalid hyperlink - Incorrect URL match."), - 'does_not_exist': _("Invalid hyperlink - Object does not exist."), - 'incorrect_type': _("Incorrect type. Expected URL string, received {data_type}."), + 'required': _('This field is required.'), + 'no_match': _('Invalid hyperlink - No URL match.'), + 'incorrect_match': _('Invalid hyperlink - Incorrect URL match.'), + 'does_not_exist': _('Invalid hyperlink - Object does not exist.'), + 'incorrect_type': _('Incorrect type. Expected URL string, received {data_type}.'), } def __init__(self, view_name=None, **kwargs): @@ -291,8 +291,8 @@ class SlugRelatedField(RelatedField): """ default_error_messages = { - 'does_not_exist': _("Object with {slug_name}={value} does not exist."), - 'invalid': _("Invalid value."), + 'does_not_exist': _('Object with {slug_name}={value} does not exist.'), + 'invalid': _('Invalid value.'), } def __init__(self, slug_field=None, **kwargs): diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 9d7c8884a..623ed5865 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -296,7 +296,7 @@ def get_validation_error_detail(exc): @six.add_metaclass(SerializerMetaclass) class Serializer(BaseSerializer): default_error_messages = { - 'invalid': _("Invalid data. Expected a dictionary, but got {datatype}.") + 'invalid': _('Invalid data. Expected a dictionary, but got {datatype}.') } @property @@ -473,7 +473,7 @@ class ListSerializer(BaseSerializer): many = True default_error_messages = { - 'not_a_list': _("Expected a list of items but got type `{input_type}`.") + 'not_a_list': _('Expected a list of items but got type `{input_type}`.') } def __init__(self, *args, **kwargs): diff --git a/rest_framework/validators.py b/rest_framework/validators.py index cf6f07180..e3719b8d5 100644 --- a/rest_framework/validators.py +++ b/rest_framework/validators.py @@ -19,7 +19,7 @@ class UniqueValidator: Should be applied to an individual field on the serializer. """ - message = _("This field must be unique.") + message = _('This field must be unique.') def __init__(self, queryset, message=None): self.queryset = queryset @@ -73,8 +73,8 @@ class UniqueTogetherValidator: Should be applied to the serializer class, not to an individual field. """ - message = _("The fields {field_names} must make a unique set.") - missing_message = _("This field is required.") + message = _('The fields {field_names} must make a unique set.') + missing_message = _('This field is required.') def __init__(self, queryset, fields, message=None): self.queryset = queryset @@ -152,7 +152,7 @@ class UniqueTogetherValidator: class BaseUniqueForValidator: message = None - missing_message = _("This field is required.") + missing_message = _('This field is required.') def __init__(self, queryset, field, date_field, message=None): self.queryset = queryset @@ -216,7 +216,7 @@ class BaseUniqueForValidator: class UniqueForDateValidator(BaseUniqueForValidator): - message = _("This field must be unique for the \"{date_field}\" date.") + message = _('This field must be unique for the "{date_field}" date.') def filter_queryset(self, attrs, queryset): value = attrs[self.field] @@ -231,7 +231,7 @@ class UniqueForDateValidator(BaseUniqueForValidator): class UniqueForMonthValidator(BaseUniqueForValidator): - message = _("This field must be unique for the \"{date_field}\" month.") + message = _('This field must be unique for the "{date_field}" month.') def filter_queryset(self, attrs, queryset): value = attrs[self.field] @@ -244,7 +244,7 @@ class UniqueForMonthValidator(BaseUniqueForValidator): class UniqueForYearValidator(BaseUniqueForValidator): - message = _("This field must be unique for the \"{date_field}\" year.") + message = _('This field must be unique for the "{date_field}" year.') def filter_queryset(self, attrs, queryset): value = attrs[self.field] diff --git a/rest_framework/versioning.py b/rest_framework/versioning.py index 819c32df8..e31c71e9b 100644 --- a/rest_framework/versioning.py +++ b/rest_framework/versioning.py @@ -36,7 +36,7 @@ class AcceptHeaderVersioning(BaseVersioning): Host: example.com Accept: application/json; version=1.0 """ - invalid_version_message = _("Invalid version in \"Accept\" header.") + invalid_version_message = _('Invalid version in "Accept" header.') def determine_version(self, request, *args, **kwargs): media_type = _MediaType(request.accepted_media_type) @@ -67,7 +67,7 @@ class URLPathVersioning(BaseVersioning): Host: example.com Accept: application/json """ - invalid_version_message = _("Invalid version in URL path.") + invalid_version_message = _('Invalid version in URL path.') def determine_version(self, request, *args, **kwargs): version = kwargs.get(self.version_param, self.default_version) @@ -109,7 +109,7 @@ class NamespaceVersioning(BaseVersioning): Host: example.com Accept: application/json """ - invalid_version_message = _("Invalid version in URL path.") + invalid_version_message = _('Invalid version in URL path.') def determine_version(self, request, *args, **kwargs): resolver_match = getattr(request, 'resolver_match', None) @@ -135,7 +135,7 @@ class HostNameVersioning(BaseVersioning): Accept: application/json """ hostname_regex = re.compile(r'^([a-zA-Z0-9]+)\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$') - invalid_version_message = _("Invalid version in hostname.") + invalid_version_message = _('Invalid version in hostname.') def determine_version(self, request, *args, **kwargs): hostname, seperator, port = request.get_host().partition(':') @@ -157,7 +157,7 @@ class QueryParameterVersioning(BaseVersioning): Host: example.com Accept: application/json """ - invalid_version_message = _("Invalid version in query parameter.") + invalid_version_message = _('Invalid version in query parameter.') def determine_version(self, request, *args, **kwargs): version = request.query_params.get(self.version_param) diff --git a/tests/test_fields.py b/tests/test_fields.py index 61d39aff6..5ecb98573 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -439,7 +439,7 @@ class TestSlugField(FieldValues): 'slug-99': 'slug-99', } invalid_inputs = { - 'slug 99': ["Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."] + 'slug 99': ['Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.'] } outputs = {} field = serializers.SlugField() diff --git a/tests/test_generics.py b/tests/test_generics.py index 94023c30a..fba8718f5 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -117,7 +117,7 @@ class TestRootView(TestCase): with self.assertNumQueries(0): response = self.view(request).render() self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) - self.assertEqual(response.data, {"detail": "Method 'PUT' not allowed."}) + self.assertEqual(response.data, {"detail": 'Method "PUT" not allowed.'}) def test_delete_root_view(self): """ @@ -127,7 +127,7 @@ class TestRootView(TestCase): with self.assertNumQueries(0): response = self.view(request).render() self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) - self.assertEqual(response.data, {"detail": "Method 'DELETE' not allowed."}) + self.assertEqual(response.data, {"detail": 'Method "DELETE" not allowed.'}) def test_post_cannot_set_id(self): """ @@ -181,7 +181,7 @@ class TestInstanceView(TestCase): with self.assertNumQueries(0): response = self.view(request).render() self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) - self.assertEqual(response.data, {"detail": "Method 'POST' not allowed."}) + self.assertEqual(response.data, {"detail": 'Method "POST" not allowed.'}) def test_put_instance_view(self): """ diff --git a/tests/test_relations.py b/tests/test_relations.py index 62353dc25..08c92242b 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -33,7 +33,7 @@ class TestPrimaryKeyRelatedField(APISimpleTestCase): with pytest.raises(serializers.ValidationError) as excinfo: self.field.to_internal_value(4) msg = excinfo.value.detail[0] - assert msg == "Invalid pk '4' - object does not exist." + assert msg == 'Invalid pk "4" - object does not exist.' def test_pk_related_lookup_invalid_type(self): with pytest.raises(serializers.ValidationError) as excinfo: From 58ec7669aed9ebd58fd6095c6a6437bf9f3cf7f1 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 18:22:30 +0000 Subject: [PATCH 18/21] swap backticks for double quotes --- rest_framework/exceptions.py | 2 +- rest_framework/fields.py | 12 ++++++------ rest_framework/locale/en_US/LC_MESSAGES/django.po | 8 ++++---- rest_framework/serializers.py | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index f62c9fe39..f954c13e5 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -52,7 +52,7 @@ class APIException(Exception): # built in `ValidationError`. For example: # # from rest_framework import serializers -# raise serializers.ValidationError("Value was invalid") +# raise serializers.ValidationError('Value was invalid') class ValidationError(APIException): status_code = status.HTTP_400_BAD_REQUEST diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 76101608e..b80dea603 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -477,7 +477,7 @@ class Field(object): class BooleanField(Field): default_error_messages = { - 'invalid': _('`{input}` is not a valid boolean.') + 'invalid': _('"{input}" is not a valid boolean.') } default_empty_html = False initial = False @@ -505,7 +505,7 @@ class BooleanField(Field): class NullBooleanField(Field): default_error_messages = { - 'invalid': _('`{input}` is not a valid boolean.') + 'invalid': _('"{input}" is not a valid boolean.') } initial = None TRUE_VALUES = set(('t', 'T', 'true', 'True', 'TRUE', '1', 1, True)) @@ -972,7 +972,7 @@ class TimeField(Field): class ChoiceField(Field): default_error_messages = { - 'invalid_choice': _('`{input}` is not a valid choice.') + 'invalid_choice': _('"{input}" is not a valid choice.') } def __init__(self, choices, **kwargs): @@ -1016,8 +1016,8 @@ class ChoiceField(Field): class MultipleChoiceField(ChoiceField): default_error_messages = { - 'invalid_choice': _('`{input}` is not a valid choice.'), - 'not_a_list': _('Expected a list of items but got type `{input_type}`.') + 'invalid_choice': _('"{input}" is not a valid choice.'), + 'not_a_list': _('Expected a list of items but got type "{input_type}".') } default_empty_html = [] @@ -1118,7 +1118,7 @@ class ListField(Field): child = None initial = [] default_error_messages = { - 'not_a_list': _('Expected a list of items but got type `{input_type}`.') + 'not_a_list': _('Expected a list of items but got type "{input_type}".') } def __init__(self, *args, **kwargs): diff --git a/rest_framework/locale/en_US/LC_MESSAGES/django.po b/rest_framework/locale/en_US/LC_MESSAGES/django.po index c8fc7f4d7..d98225ce9 100644 --- a/rest_framework/locale/en_US/LC_MESSAGES/django.po +++ b/rest_framework/locale/en_US/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2015-01-07 12:28+0000\n" +"POT-Creation-Date: 2015-01-07 18:21+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -111,7 +111,7 @@ msgid "This field may not be null." msgstr "" #: fields.py:480 fields.py:508 -msgid "`{input}` is not a valid boolean." +msgid "\"{input}\" is not a valid boolean." msgstr "" #: fields.py:543 @@ -199,11 +199,11 @@ msgid "Time has wrong format. Use one of these formats instead: {format}." msgstr "" #: fields.py:975 fields.py:1019 -msgid "`{input}` is not a valid choice." +msgid "\"{input}\" is not a valid choice." msgstr "" #: fields.py:1020 fields.py:1121 serializers.py:476 -msgid "Expected a list of items but got type `{input_type}`." +msgid "Expected a list of items but got type \"{input_type}\"." msgstr "" #: fields.py:1050 diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 623ed5865..5bfbd2351 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -473,7 +473,7 @@ class ListSerializer(BaseSerializer): many = True default_error_messages = { - 'not_a_list': _('Expected a list of items but got type `{input_type}`.') + 'not_a_list': _('Expected a list of items but got type "{input_type}".') } def __init__(self, *args, **kwargs): From 734f8f26678d3bd28f04bc44b0fabd146b97ddb0 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Wed, 7 Jan 2015 18:22:40 +0000 Subject: [PATCH 19/21] restore Django 404 --- rest_framework/generics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 7ebed0327..d52f2b6c0 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -120,13 +120,13 @@ class GenericAPIView(views.APIView): if page == 'last': page_number = paginator.num_pages else: - raise NotFound(_('Choose a valid page number. Page numbers must be a whole number, or must be the string "last".')) + raise Http404(_('Choose a valid page number. Page numbers must be a whole number, or must be the string "last".')) try: page = paginator.page(page_number) except InvalidPage as exc: error_format = _('Invalid page "{page_number}": {message}.') - raise NotFound(error_format.format( + raise Http404(error_format.format( page_number=page_number, message=six.text_type(exc) )) From 1368c31a705a4892995f42cf5e0dcdcbfa13a1ce Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Thu, 8 Jan 2015 17:16:15 +0000 Subject: [PATCH 20/21] remove unused import --- rest_framework/generics.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index d52f2b6c0..0d709c37a 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -10,7 +10,6 @@ from django.shortcuts import get_object_or_404 as _get_object_or_404 from django.utils import six from django.utils.translation import ugettext as _ from rest_framework import views, mixins -from rest_framework.exceptions import NotFound from rest_framework.settings import api_settings From 7f8d314101c4e6e059b00ac12658f0e1055da8f7 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Thu, 8 Jan 2015 17:16:47 +0000 Subject: [PATCH 21/21] update tests to expect new error messages --- tests/test_fields.py | 18 +++++++++--------- tests/test_serializer_bulk_update.py | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/test_fields.py b/tests/test_fields.py index 5ecb98573..240827eea 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -338,7 +338,7 @@ class TestBooleanField(FieldValues): False: False, } invalid_inputs = { - 'foo': ['`foo` is not a valid boolean.'], + 'foo': ['"foo" is not a valid boolean.'], None: ['This field may not be null.'] } outputs = { @@ -368,7 +368,7 @@ class TestNullBooleanField(FieldValues): None: None } invalid_inputs = { - 'foo': ['`foo` is not a valid boolean.'], + 'foo': ['"foo" is not a valid boolean.'], } outputs = { 'true': True, @@ -832,7 +832,7 @@ class TestChoiceField(FieldValues): 'good': 'good', } invalid_inputs = { - 'amazing': ['`amazing` is not a valid choice.'] + 'amazing': ['"amazing" is not a valid choice.'] } outputs = { 'good': 'good', @@ -872,8 +872,8 @@ class TestChoiceFieldWithType(FieldValues): 3: 3, } invalid_inputs = { - 5: ['`5` is not a valid choice.'], - 'abc': ['`abc` is not a valid choice.'] + 5: ['"5" is not a valid choice.'], + 'abc': ['"abc" is not a valid choice.'] } outputs = { '1': 1, @@ -899,7 +899,7 @@ class TestChoiceFieldWithListChoices(FieldValues): 'good': 'good', } invalid_inputs = { - 'awful': ['`awful` is not a valid choice.'] + 'awful': ['"awful" is not a valid choice.'] } outputs = { 'good': 'good' @@ -917,8 +917,8 @@ class TestMultipleChoiceField(FieldValues): ('aircon', 'manual'): set(['aircon', 'manual']), } invalid_inputs = { - 'abc': ['Expected a list of items but got type `str`.'], - ('aircon', 'incorrect'): ['`incorrect` is not a valid choice.'] + 'abc': ['Expected a list of items but got type "str".'], + ('aircon', 'incorrect'): ['"incorrect" is not a valid choice.'] } outputs = [ (['aircon', 'manual'], set(['aircon', 'manual'])) @@ -1028,7 +1028,7 @@ class TestListField(FieldValues): (['1', '2', '3'], [1, 2, 3]) ] invalid_inputs = [ - ('not a list', ['Expected a list of items but got type `str`.']), + ('not a list', ['Expected a list of items but got type "str".']), ([1, 2, 'error'], ['A valid integer is required.']) ] outputs = [ diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index fb881a755..bc955b2ef 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -101,7 +101,7 @@ class BulkCreateSerializerTests(TestCase): serializer = self.BookSerializer(data=data, many=True) self.assertEqual(serializer.is_valid(), False) - expected_errors = {'non_field_errors': ['Expected a list of items but got type `int`.']} + expected_errors = {'non_field_errors': ['Expected a list of items but got type "int".']} self.assertEqual(serializer.errors, expected_errors) @@ -118,6 +118,6 @@ class BulkCreateSerializerTests(TestCase): serializer = self.BookSerializer(data=data, many=True) self.assertEqual(serializer.is_valid(), False) - expected_errors = {'non_field_errors': ['Expected a list of items but got type `dict`.']} + expected_errors = {'non_field_errors': ['Expected a list of items but got type "dict".']} self.assertEqual(serializer.errors, expected_errors)