Skip to content

Commit 0988a4a

Browse files
authored
SG-12945 + SG-12581 : Added localized param to Shotgun object (#208)
Adds an optional localized property on the Shotgun object which allows to get localized display names on methods schema_entity_read(), schema_field_read(), and schema_read().
1 parent 52306c1 commit 0988a4a

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Shotgun Python API Changelog
44

55
Here you can see the full list of changes between each Python API release.
66

7+
v3.1.2 (2019 Sept 17)
8+
=====================
9+
- Adds an optional `localized` property on the Shotgun object which allows to retrieve localized display names on
10+
methods ``schema_entity_read()``, ``schema_field_read()``, and ``schema_read()``.
11+
712
v3.1.1 (2019 August 29)
813
=====================
914
- Fixes a regression on Python 2.7.0-2.7.9 on Windows with the mimetypes module.

docs/reference.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,46 @@ Stores the number of milliseconds to wait between request retries. By default,
891891

892892
In the case that both this environment variable and the config's ``rpc_attempt_interval`` property are set, the value in ``rpc_attempt_interal`` will be used.
893893

894+
************
895+
Localization
896+
************
897+
898+
The Shotgun API offers the ability to return localized display names in the current user's language.
899+
Requests made from script/API users are localized in the site settings.
900+
901+
This functionality is currently supported by the methods ``Shotgun.schema_entity_read``, ``Shotgun.schema_field_read``, and ``Shotgun.schema_read``.
902+
903+
Localization is disabled by default. To enable localization, set the ``localized`` property to ``True``.
904+
905+
Example for a user whose language preference is set to Japanese:
906+
907+
.. code-block:: python
908+
:emphasize-lines: 9,20
909+
910+
>>> sg = Shotgun(site_name, script_name, script_key)
911+
>>> sg.config.localized # checking that localization is disabled
912+
False
913+
>>> sg.schema_field_read('Shot')
914+
{
915+
'sg_vendor_groups': {
916+
'mandatory': {'editable': False, 'value': False},
917+
# the value field (display name) is not localized
918+
'name': {'editable': True, 'value': 'Vendor Groups'},
919+
...
920+
},
921+
...
922+
}
923+
>>> sg.config.localized = True # enabling the localization
924+
>>> sg.schema_field_read('Shot')
925+
{
926+
'sg_vendor_groups': {
927+
'mandatory': {'editable': False, 'value': False},
928+
# the value field (display name) is localized
929+
'name': {'editable': True, 'value': '\xe3\x83\x99\xe3\x83\xb3\xe3\x83\x80\xe3\x83\xbc \xe3\x82\xb0\xe3\x83\xab\xe3\x83\xbc\xe3\x83\x97'},
930+
...
931+
},
932+
...
933+
}
934+
935+
.. note::
936+
If needed, the encoding of the returned localized string can be ensured regardless the Python version using shotgun_api3.lib.six.ensure_text().

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
setup(
2929
name='shotgun_api3',
30-
version='3.1.1',
30+
version='3.1.2',
3131
description='Shotgun Python API ',
3232
long_description=readme,
3333
author='Shotgun Software',

shotgun_api3/shotgun.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _is_mimetypes_broken():
117117

118118
# ----------------------------------------------------------------------------
119119
# Version
120-
__version__ = "3.1.1"
120+
__version__ = "3.1.2"
121121

122122
# ----------------------------------------------------------------------------
123123
# Errors
@@ -427,6 +427,7 @@ def __init__(self, sg):
427427
self.session_token = None
428428
self.authorization = None
429429
self.no_ssl_validation = False
430+
self.localized = False
430431

431432
@property
432433
def records_per_page(self):
@@ -1818,6 +1819,9 @@ def schema_entity_read(self, project_entity=None):
18181819
``{'type': 'Project', 'id': 3}``
18191820
:returns: dict of Entity Type to dict containing the display name.
18201821
:rtype: dict
1822+
1823+
.. note::
1824+
The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more information.
18211825
"""
18221826

18231827
params = {}
@@ -1887,6 +1891,9 @@ def schema_read(self, project_entity=None):
18871891
types. Properties that are ``'editable': True``, can be updated using the
18881892
:meth:`~shotgun_api3.Shotgun.schema_field_update` method.
18891893
:rtype: dict
1894+
1895+
.. note::
1896+
The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more information.
18901897
"""
18911898

18921899
params = {}
@@ -1917,6 +1924,9 @@ def schema_field_read(self, entity_type, field_name=None, project_entity=None):
19171924
.. note::
19181925
If you don't specify a ``project_entity``, everything is reported as visible.
19191926
1927+
.. note::
1928+
The returned display names for this method will be localized when the ``localize`` Shotgun config property is set to ``True``. See :ref:`localization` for more information.
1929+
19201930
>>> sg.schema_field_read('Asset', 'shots')
19211931
{'shots': {'data_type': {'editable': False, 'value': 'multi_entity'},
19221932
'description': {'editable': True, 'value': ''},
@@ -3205,6 +3215,10 @@ def _call_rpc(self, method, params, include_auth_params=True, first=False):
32053215
"content-type": "application/json; charset=utf-8",
32063216
"connection": "keep-alive"
32073217
}
3218+
3219+
if self.config.localized is True:
3220+
req_headers["locale"] = "auto"
3221+
32083222
http_status, resp_headers, body = self._make_call("POST", self.config.api_path,
32093223
encoded_payload, req_headers)
32103224
LOG.debug("Completed rpc call to %s" % (method))

tests/example_config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
# Full url to the Shotgun server server
1717
# e.g. http://my-company.shotgunstudio.com
18+
# be careful to not end server_url with a "/", or some tests may fail
1819
server_url : http://0.0.0.0:3000
1920
# script name as key as listed in admin panel for your server
2021
script_name : test script name

tests/test_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ def test_authorization(self):
184184
expected = "Basic " + b64encode(urllib.parse.unquote(login_password)).strip()
185185
self.assertEqual(expected, headers.get("Authorization"))
186186

187+
def test_localization_header_default(self):
188+
"""Localization header not passed to server without explicitly setting SG localization config to True"""
189+
self.sg.info()
190+
191+
args, _ = self.sg._http_request.call_args
192+
(_, _, _, headers) = args
193+
expected_header_value = "auto"
194+
195+
self.assertEqual(None, headers.get("locale"))
196+
197+
def test_localization_header_when_localized(self):
198+
"""Localization header passed to server when setting SG localization config to True"""
199+
self.sg.config.localized = True
200+
201+
self.sg.info()
202+
203+
args, _ = self.sg._http_request.call_args
204+
(_, _, _, headers) = args
205+
expected_header_value = "auto"
206+
207+
self.assertEqual("auto", headers.get("locale"))
208+
187209
def test_user_agent(self):
188210
"""User-Agent passed to server"""
189211
# test default user agent

0 commit comments

Comments
 (0)