-
Notifications
You must be signed in to change notification settings - Fork 162
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
Flattens .yml files for i18n #10503
Flattens .yml files for i18n #10503
Conversation
- scripts/yml_to_txt takes a batch of yml files and converts them to txt - I18nTxtBackend implements loading these files for the I18n gem
lib/i18n_txt_backend.rb
Outdated
# @param filename [String] filename, assumed to have the locale slug in the filename such as "en.txt" | ||
# @return [Array(Hash, Boolean)] tuple of a hash and keys_symbolized | ||
def load_txt(filename) | ||
locale = File.basename(filename, '.txt') |
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.
The locale is guessed from the filename because it lets us drop the locale from the key in the file. Without this, the files would be like:
en.foo.bar.baz: "test"
and that would mean keys would be harder to copy-paste across files.
I expect the |
- Disable proseWrap - Use double quotes for config/locales/*.yml files
It seems like we don't have that many strings that include newlines. I wonder if the "values" here could just be raw text to avoid having to deal with JSON encoding and make them easier to edit by hand. This would encourage splitting large blocks up into multiple strings and handling formatting concerns in the views instead. Alternately, raw text with |
After some messing around, it turns out that this format is valid YAML, so I switched it around and tools like |
changelog: Internal, Source code, Reformat i18n files to simplify merges
|
@@ -1,5 +1,6 @@ | |||
{ | |||
"singleQuote": true, | |||
"trailingComma": "all", | |||
"printWidth": 100 | |||
"printWidth": 100, | |||
"proseWrap": "never" |
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.
This will affect all Markdown formatted by Prettier. I don't have a strong feeling one way or the other, just noting it has a more widespread impact than just i18n strings.
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.
I'd like to keep these .yml
as un-wrapped as possible, so I could back this change out but add it to normalize-yaml
script as an override?
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.
I think it's fine if we acknowledge this as a project-wide default, which seems reasonable enough?
But yeah, if needed, I think this could also go back to being able to configure normalize-yaml
with a specific Prettier configuration if we wanted to behave differently specific for normalize-yaml
/ specific for specific sets of files normalized by normalize-yaml
.
app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js
Outdated
Show resolved
Hide resolved
app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js
Show resolved
Hide resolved
Co-authored-by: Andrew Duthie <1779930+aduth@users.noreply.github.com>
|
||
# Custom i18n backend that parse our "flat_yml" files into the nested | ||
# hash structure that i18n works with | ||
class I18nFlatYmlBackend < I18n::Backend::Simple |
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.
Is it possible to keep the locale at the top of the file and avoid the separate backend?
en:
key: value
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.
I'd be in favor of keeping the backend if it saves a huge amount of unnecessary indentation in those files.
Also based on comment in #10503 (comment), I think we could take this even further to customize the backend to consume the keys more directly (avoid backend structured as a nested hash)
What's in the Branch
Based on the challenges of merging multi-line, nested YML files for translation, this PR proposes flattening them to files with flat keys, no indentation needed.
scripts/yml_to_flat_yml
takes a batch of yml files and converts them to txtI18nFlatYmlBackend
implements loading these files for the I18n gemNext Steps
If we wanted to land this, we'd need to:
normalize-yaml
JS script to have an option to parse/fix these files as welli18n-tasks
(need to make a compatible parser for it, it doesn't reuse i18n ones 🙄 )Example
Example of how to generate one of these
.txt
files:Format specification
Uses JSON to encode one-line forms of strings. If we need newlines inside strings, we'll use
\n
To support arrays, I went with "if all keys of a hash are numeric, it should be an array" (we have one case of a hash with mixed numbers and strings)
Alternative to handle arrays
Another approach could be a special symbol for arrays like: