LG-3175: Create localization utilities for React components#3909
LG-3175: Create localization utilities for React components#3909
Conversation
a15bafc to
cb7497b
Compare
|
There's currently an error being reported in CodeClimate, but it's not one I would expect to see: The parentheses are expected:
Unfortunately, CodeClimate's web UI doesn't give much debugging information to work from. I installed and ran their CLI tool and was able to reproduce the error. I observed from their documentation that they run a very outdated version of ESLint by default (3.x, currently 7.x). I suspected it might be something where the ESLint configuration is not able to be loaded, and instead is falling back to some default. Configuring CodeClimate locally to use a newer version of ESLint appears to help make the difference: Looking through the release notes, nothing in particular stands out to me as making the difference here. That being said, it would be wise to bring the CodeClimate ESLint configured version in line with what's installed in the application. #3903 upgraded ESLint from 4.x to the latest 7.x version. ESLint 7 does not appear to be supported yet by CodeClimate. We can configure it as recent as using version 6. I'd recommend giving this a try, and if issues persist, we could consider to downgrade the version used in the application. It's something which should probably be done as its own task, but since it blocks this pull request, I'd propose to commit it here. |
|
5673f9d seemed to work, although it works a little too well 😓 Catching a bunch of preexisting lint errors now. |
2301ec1 to
f6842c9
Compare
**Why**: As a user, I want pages that I'm viewing to be localized to my language, including those page which use React.
f6842c9 to
a1756a1
Compare
New JS code added in #3909 and others require strings configured in assets.js.erb and i18n-strings.js.erb. The added make task, check_asset_strings, assets that for each such string referenced in a Javascript file, there exists a corresponding entry in one of the erb files

Why: As a user, I want pages that I'm viewing to be localized to my language, including those page which use React.
This pull request adds simple localization utilities for React components. It is implemented using React context to establish localization data at the root of the application. A custom React hook is included for the purpose of simplifying usage, modeled after existing
LoginGov.I18n.tand server-sidetRails internationalization.Making this data available via context should hopefully facilitate better unit testing, since it doesn't require mocking of
windowglobals, but instead allows for any (or no) simple JavaScript object to be provided as localization data. The current implementation of the translation utility will return the given key if there is no localized data. While this is not appropriate for the front-end user experience, it could be a fine compromise for tests to be able to find elements by a given string key.The alternative would be to find some way to make the localization data available to the tests. This would have the advantage of bringing tests closer to a "real" user experience of testing components by text visible in the page, but would require additional test bootstrapping logic. The logic would likely involve either loading all YAML files from
config/localesas a plain JavaScript object, or as a prerequisite build artifact generated by Rails (see also:i18n-jsexport,react_on_railsI18n).