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.
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
|
|
| "trailingComma": "all", | ||
| "printWidth": 100 | ||
| "printWidth": 100, | ||
| "proseWrap": "never" |
There was a problem hiding this comment.
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.
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.
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.
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.
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_ymltakes a batch of yml files and converts them to txtI18nFlatYmlBackendimplements loading these files for the I18n gemNext Steps
If we wanted to land this, we'd need to:
normalize-yamlJS 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
.txtfiles:Format specification
Uses JSON to encode one-line forms of strings. If we need newlines inside strings, we'll use
\nTo 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: