Skip to content

Commit

Permalink
fix: process i18n urls
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Feb 20, 2021
1 parent 4965fac commit a4a40fd
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion teslajsonpy/teslaproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ def __init__(self, proxy_url: URL, host_url: URL) -> None:
}

self.modifiers.update(
{"prepend_url_ajax": partial(self.prepend_relative_urls, self.access_url())}
{
"prepend_url_ajax": partial(
self.prepend_relative_urls, self.access_url()
),
"application/javascript": {
"prepend_url_i18n": partial(
self.prepend_i18n_path, URL(self.access_url().path)
)
},
}
)

async def test_url(
Expand Down Expand Up @@ -121,6 +130,31 @@ async def prepend_relative_urls(self, base_url: URL, html: Text) -> Text:
html=html,
)

async def prepend_i18n_path(self, base_url: URL, html: Text) -> Text:
"""Prepend path for i18n loadPath so it'll reach the proxy.
This is intended to be used for to place the proxy_url path in front of relative urls for loadPath in i18next.
Args:
base_url (URL): Base URL to prepend
html (Text): Text to replace
Returns
Text: Replaced text
"""
if not base_url:
_LOGGER.debug("No base_url specified")
return html

return await find_regex_urls(
partial(prepend_url, base_url, encoded=True),
{
"method_func": r"""(?:loadPath:)\s*?["']([^"']*)[\"\']""",
},
html=html,
)

async def modify_headers(
self, site: URL, request: web.Request
) -> multidict.MultiDict:
Expand Down

0 comments on commit a4a40fd

Please sign in to comment.