-
Notifications
You must be signed in to change notification settings - Fork 3k
Ensure translations work as expected #11049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🪼 branch checks and previews
Install Gradio from this PR pip install https://gradio-pypi-previews.s3.amazonaws.com/fa5a3bd25d9fb96d7d9158cd82936d362773e618/gradio-5.25.2-py3-none-any.whl Install Gradio Python Client from this PR pip install "gradio-client @ git+https://github.com/gradio-app/gradio@fa5a3bd25d9fb96d7d9158cd82936d362773e618#subdirectory=client/python" Install Gradio JS Client from this PR npm install https://gradio-npm-previews.s3.amazonaws.com/fa5a3bd25d9fb96d7d9158cd82936d362773e618/gradio-client-1.14.2.tgz Use Lite from this PR <script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/fa5a3bd25d9fb96d7d9158cd82936d362773e618/dist/lite.js""></script> |
🦄 change detectedThis Pull Request includes changes to the following packages.
With the following changelog entry.
Maintainers or the PR author can modify the PR title to modify this entry.
|
Just want to clarify one thing: I thought only strings that are hardcoded into the UI are translated? Is it the case that other strings are automatically translated as well if they happen to match one of the UI strings? E.g. let's say I have
|
@abidlabs I understand the confusion! Let me clarify.
Yes, it would be - this was actually the behaviour prior to our translation change, so it's technically a regression fix. I see what you're saying about contextual translation, that could be an edge case.
The custom i18n should always take precedence over our core translations, though i haven't tested this in the PR for that
Great point! I'm pretty sure the value is never directly affected by translation, it's purely visual - but I'd like to double check that See this demo (in french)
This is what it looks like in main vs this PR
There are still some translation gaps in this demo, but my thoughts are that using those components without any configuration (so gradio's default values used instead) should mean that it will also handle translation for users in my app as well for a seamless user experience. right now we have a lot of inconsistencies which feels janky and broken. This all being said, it's a relatively niche problem and custom i18n will fix any annoyances people may have :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good @hannahblair !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me, I tested and although I am far from and expert, it looked good!
Thanks for tackling this one @hannahblair!
Amazing will merge this in so that we can review the other one! |
Description
There are two key i18n fixes in this PR:
getLocaleFromNavigator()
returns BCP 47 language tags (like "en-US", "de-DE") while our translation files use simple language codes ("en", "de"). I've added a normalisation func to handle this.Submit
as avalue
ingr.Button
used to get translated without an issue. Now, we haveSubmit
under the common namespace like"common": { "submit": "Submit" }
. Sogradio.i18n("Submit")
will just return Submit because it can't find it inen.json
without thecommon
prefix. Strings that are hardcoded and don't come from the backend are obviously unaffected and work fine.To fix this, during i18n setup, we now iterate through all language files and compile a
Set
(all_common_keys
) containing every key defined within any common namespace (e.g., "common.submit", "common.flag").Fallback flow:
all_common_keys
set. For each key (like "common.flag"), it extracts the base name ("flag"). If the normalised input value matches the extracted base key name, it uses the full common key ("common.flag") to attempt translation in the current locale. If a successful translation is found via this key-name matching, it's returnedIf the user uses their own value which isn't in the namespace then this obviously wont get translated, unless they use the up and coming
gr.i18n()
func!We do need to check that we have all our default values in the common namespace now.
🎯 PRs Should Target Issues
Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.
Not adhering to this guideline will result in the PR being closed.
Testing and Formatting Your Code
PRs will only be merged if tests pass on CI. We recommend at least running the backend tests locally, please set up your Gradio environment locally and run the backed tests:
bash scripts/run_backend_tests.sh
Please run these bash scripts to automatically format your code:
bash scripts/format_backend.sh
, and (if you made any changes to non-Python files)bash scripts/format_frontend.sh