alex-poor commented on code in PR #40679:
URL: https://github.com/apache/superset/pull/40679#discussion_r3918212660


##########
superset/jinja_context.py:
##########
@@ -1251,6 +1252,24 @@ def get_template_processor(
     return template_processor(database=database, table=table, query=query, 
**kwargs)
 
 
+def i18n_macro(default_text: str) -> str:
+    """Jinja macro for translating asset metadata into the viewer's locale.
+
+    Usage in a templated field (e.g. a chart axis label or native filter 
name)::
+
+        {{ i18n('Sales') }}
+
+    Resolution is delegated to the configured ``TRANSLATION_HOOK`` and gated by
+    the ``ENABLE_I18N_ASSET_TRANSLATIONS`` feature flag; when the feature is 
off
+    or no translation exists, the original text is returned unchanged. See
+    ``superset.utils.i18n`` and SIP-161.
+    """
+    # pylint: disable=import-outside-toplevel
+    from superset.utils.i18n import translate
+
+    return translate(default_text, model_name="template", field_name="i18n") 
or ""

Review Comment:
   Fixed in 97e318e — good catch, this was the most serious thing left in the 
PR. The macro now registers its resolved value through `cache_key_wrapper`, and 
`i18n(...)` is in the `ExtraCache` regex so a query using it collects extra 
cache keys at all; without that the wrapper would never have run.
   
   Keyed on the resolved text rather than the locale, so two locales that 
resolve to the same string still share a cache entry, and toggling the feature 
flag naturally invalidates. Covered by a test asserting the extra cache key is 
collected, plus one on the regex.



##########
superset/jinja_context.py:
##########
@@ -1105,6 +1105,7 @@ def set_context(self, **kwargs: Any) -> None:
                 "get_guest_user_attribute": partial(
                     safe_proxy, extra_cache.get_guest_user_attribute
                 ),
+                "i18n": partial(safe_proxy, i18n_macro),

Review Comment:
   Fixed in 97e318e — the built-in now registers only when the name is 
unclaimed, so an existing addon wins.
   
   For the record on the general shape: every built-in in that block shadows 
addons, since `BaseTemplateProcessor.set_context` installs `context_addons()` 
before the subclass update. The difference you are pointing at is that those 
names are long-standing while `i18n` is introduced here, so an addon of that 
name necessarily predates the built-in and silently losing it would be an 
upgrade regression rather than existing behaviour. Left the other built-ins 
alone on that basis. There is a test that an addon-provided `i18n` still wins.



##########
examples/asset_metadata_translation/README.md:
##########
@@ -0,0 +1,86 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+# Reference: database-backed asset metadata translation
+
+This is a **self-contained reference**, not part of Superset core. It shows one
+concrete way to implement end-to-end authoring on top of the
+`TRANSLATION_HOOK` read path documented at
+[Asset Metadata 
Translation](https://superset.apache.org/docs/configuration/asset-metadata-translation).

Review Comment:
   Fixed in 97e318e — now points at 
`/admin-docs/configuration/asset-metadata-translation`. Confirmed against 
`routeBasePath: 'admin-docs'` in the Docusaurus config, and it matches the 
page's own internal links, which already use the `/admin-docs/` prefix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to