Repository navigation
Fixes #35: Adds multi-language support to Audimance. - #39
Conversation
… languages. Fixes CyCoreSystems#37: Moves shared stylesheets into audimance. Fixes CyCoreSystems#38: Serves the app directory with ETags and no-cache.
In flake.nix, copies app/ recursively for the new css folder and updates vendorHash.
Ulexus
left a comment
There was a problem hiding this comment.
A number of nits and suggestions. I wouldn't classify anything as a "blocker" per se, but I would entreat you to review my comments. Please feel free to reject as you will. Many are based on opinion, with arguments for and against. While I value my experience and learning, I know I also come at things from a particular perspective which may not align with your own.
| "testing" | ||
| ) | ||
|
|
||
| func load(t *testing.T, yaml string) (*Agenda, error) { |
There was a problem hiding this comment.
This is a bit tortured (not your fault). I propose instead that we alter agenda.New(filename string) (*Agenda, error) to be agenda.New(src io.Reader) (*Agenda, error) and add a new agenda.Load(filename string) (*Agenda, error), which internally calls agenda.New() after opening the source file:
package agenda
// New creates a new agenda from source data.
func New(src io.Reader) (*Agenda, error) {
...
}
// Load loads an agenda from a file.
func Load(srcFilename string) (*Agenda, error) {
data, err := os.ReadFile(srcFilename)
if err != nil {
return nil, fmt.Errorf("failed to read from source file %q: %w", srcFilename, err)
}
return New(data)
}Then here, we can just call agenda.New(strings.NewReader(yamlStringData)) in the tests, avoiding writing out to temp files.
There was a problem hiding this comment.
Much better, thanks for the suggestion. I've implemented it.
| sources: | ||
| - name: Choreography | ||
| `) | ||
| if err != nil { |
There was a problem hiding this comment.
While I generally prefer to avoid unnecessary third-party tools, I think tests are a good example for a situation where it is warranted.
The testify suite of tools a venerable and useful testing suite which does not require much overhead but provides much utility to make tests clearer and more maintainable.
Here, for instance, I would use is thus:
require.NoError(t, err)This provides in a single line, a clear interpretation of the test. The require implies t.FailNow() (which, by the way, is preferable here, since it only fails this test, not others which may also be run within this package, as is more appropriate for this kind of failure).
Other types of tests can be handled by the assert mechanism, such as (for below) assert.NotEmpty(t, lang), which will fail the test but not terminate the propagation of the rest of the current test.
There are many expressive tests which allow testing packages to be clearer and simpler.
There was a problem hiding this comment.
Makes total sense, I should have spent more time looking up Go testing frameworks. I've moved the tests over to testify.
| @@ -0,0 +1,115 @@ | |||
| package agenda | |||
There was a problem hiding this comment.
Unless there is a need to reach in and use private functions and methods, testing packages should use the _test suffix: package agenda_test.
If you do need to reach in to private resources, can they be rewritten to facilitate cleaner testing?
| } | ||
| } | ||
|
|
||
| func TestLabelInJSON(t *testing.T) { |
There was a problem hiding this comment.
Since the parsing of JSON and YAML are outside the scope of the data herein, I don't think there is a need to test both here. Rather, a separate test to validate YAML and JSON unmarshaling should be implemented. By doing this kind of permutation testing, the unit tests explode exponentially, for very little overall benefit.
Permutations features should be tested, but not generally permutations of feature combinations, unless there is a reason to suspect a vulnerability.
aka: more tests are not always better
There was a problem hiding this comment.
I've removed this test. And I agree, I think when we have some time to design the V2 Agenda properly, we should at the very least add a test that validates the serialization to JSON.
| } | ||
|
|
||
| func TestRejectsInvalidLanguage(t *testing.T) { | ||
| _, err := load(t, strings.Replace(bilingual, "languages: [en, zh-TW]", "languages: [en, \"not a tag!\"]", 1)) |
There was a problem hiding this comment.
nit: this is really just testing the upstream language package, which really should be out of scope. These kinds of tests are borderline for me: while they do ensure that the implementation does check the validity of the language:
a) the validity check itself is out of scope of this unit
b) if the validity checking fails, it doesn't really fundamentally break things
The more apropos test is the one above (an unlisted language handling test), which is fundamentally an internal mechanism.
Basically, this verges on being an integration test, not a unit test. It's trivial enough on its own, but these things have a tendency to add up over time.
There was a problem hiding this comment.
Some of these were indeed only testing the language package. I've always had the perspective that you should write just enough tests for your use of a third party library that you can recognize a regression if you were to swap it for another library, but I also understand your point.
A few others tests are testing our own logic—the language "cascade" (i.e. lang query parameter > AcceptsLanguage header > default language). I think they're probably worth keeping? I've cut the list of cases back to only these ones and added a few comments about why they're there.
There was a problem hiding this comment.
This is definitely a limitation in the Go tooling. We have unit tests, but we don't have integration tests. This is a problem I've been meaning to tackle for years, but I haven't done it yet.
It's no problem. This project is not large, and it shouldn't ever grow to the size that we have issues with the tests taking too long.
| {"", "fr-FR", "en"}, | ||
| {"", "", "en"}, | ||
| } { | ||
| if got := r.Resolve(c.query, c.acceptLanguage); got != c.want { |
There was a problem hiding this comment.
This seems to just be testing the upstream package, since the local Resolve() is just a trivial wrapper around it.
| } | ||
| } | ||
|
|
||
| func TestResolveWithoutLanguages(t *testing.T) { |
There was a problem hiding this comment.
This kind of variation is generally better handled in a subtest t.Run("without-languages") inside the base TestResolve(). It is merely a permutation of testing language resolution, not warranting another top-level test.
func TestResolve(t *testing.T) {
// common definitions...
t.Run("empty-languages", func(t *testing.T) {
...
})
t.Run("populated-languages", func(t *testing.T) {
...
})
}This not only keep the code better organised, but the test runs and their results are clearer and better-organised, as well.
|
|
||
| func TestLoadTemplates(t *testing.T) { | ||
| views := t.TempDir() | ||
| write := func(name, content string) { |
There was a problem hiding this comment.
As mentioned in the code itself, by using an fs.FS, you can more easily test without using a real filesystem.
However, another easy option is to use fixed assets. For this, I would populate the assets (along with their expected directory structure) within an assets subdirectory. There's nothing special about the name assets, though; it's just the name I generally use for such things.
Either is better than using temp files:
- there is no write contention (read contention is not an issue)
- eliminates a whole class of transient, external failure causes
- fixed assets are easier and clearer to maintain, requiring intentional, separate changes, which aligns well with the separation of unit tests from code... less opportunity to accidentally "cheat"
- fixed assets can be more freely and naturally formatted, rather than these artificially tight, manual data compressions
- eliminates unimportant formatting code from the tests
| return err | ||
| } | ||
|
|
||
| etags["/"+name] = fmt.Sprintf(`"%x"`, sha256.Sum256(data)) |
There was a problem hiding this comment.
nit: consider using hex.EncodeToString() rather than fmt.Sprintf(). It's clearer to the reader what the intention is, and it is slightly more efficient.
| }() | ||
|
|
||
| // Assemble a copy of the agenda in the show's supported languages | ||
| agendas := map[string]*agenda.Agenda{"": a} |
There was a problem hiding this comment.
This concerns me a little bit. It is clearly a performance optimisation, but it comes at the cost of effectively having two different Agenda structures: one which is fully expressive of the entirety of supported languages and another which represents only a single language.
First, is this a premature optimisation? That is, the agenda is language-processed (ideally, given caches) once per user, and the parsing is a pretty trivial, local operation, not even requiring filesystem access after the initial server load.
Two, having a single-language format distinct from a multi-language format feels... wrong. I can completely understand the desire to keep the majority of the code in Go, rather than JS, but I'm afraid this additional complexity and particularly the external optics and variations from expectation grow too large. We could introduce a LiveAgenda, I suppose, to differentiate (we do already have variations, such as the generated IDs and populated autogenerated fields), but I don't think that this is worth it.
I would consider, for example, a better path forward to be to create a new, v2 agenda which is fully, natively multilingual and simply up-convert a legacy v1 agenda to v2 on server startup. (designate the new agenda with a version: v2 property, to make it easy to differentiate)
There was a problem hiding this comment.
I totally understand your point here. And I agree that we shouldn't have this situation where we've got essentially two different views of the Agenda, one that has all languages and the other that is language specific. The intent for doing is this was really just temporary. Implement something that had the fewest changes to the current architecture and avoid breaking backwards compatibility with existing shows. Your suggestion to introduce a new version of Agenda and up-convert legacy agendas makes sense. It is, ideally, more machinery that I want to write the day before submitting this to Apple and two weeks before premiering a new show.
I actually also have some concerns about the file format I've implemented here, in that it's quite noisy in how it requires language code keys to be repeated for every point of interest and source label. What I do think is good about this design is that the "identifiers" for a particular label (i.e. name) are actually just text in the default language (English in our case). Longer term, I think translations should actually live in separate language bundle files that get merged into the agenda data structure at run time.
Language bundles should be in a form (CSV?) that makes it really easy to share with a translator (in Google Drive?) and have them be do translations quickly using tools they're already familiar with and without risking breaking the rest of the agenda because they didn't format some random bit of YAML correctly. This is how Haowen and I did this round of translations—a shared Google Sheet exported as a CSV file). iOS does something similar for localization, and it was honestly the best experience I've had of retrofitting localization into an app that wasn't designed for it.
We should, at some point, also move more into language bundles, as opposed to what I've done in this PR, which is to introduce copies of every view template for each language. Translation currently involves two steps: 1. putting language-specific labels for POIs and sources in agenda.yaml, and 2. creating language-specific templates.
Longer term, I'd like to introduce a more declarative approach that will allow us to do more rendering in native tools from common representations of a show's UI without duplicating all the core logic that is the same across all our clients. We need to move more rendering to native anyway because the Web version of the spatial room isn't very idiomatically screenreader accessible, and I can make it much more better in SwiftUI with roles that still haven't been added to ARIA (and presumably do the same on Kotlin for Android soon). I think this is consonant with what you're arguing for—data structures that provide comprehensive views of the whole system, and which can be transferred between client and server without loss.
Sorry, this has been a bit of a ramble, but hopefully it makes some vague sense. The short version:
- I totally agree, we do need a V2 agenda format, which is fully inclusive of all localization data
- We need a better, more translator-friendly representation for how localizations are expressed
- More localization logic probably needs to move to the client side
- If you can live with it, let's leave the implementation like this for now, I'll file a ticket about what needs to change, and then we can find a time between Eternal City and the Bell premiere to hash it out?
- If yes to the last question, can you redeploy the demo and then I'll submit the app for review to Apple tomorrow?
There was a problem hiding this comment.
Oh, if you're on a deadline, let's just get it merged. We can always fix "later". Nothing here is blocking.
Yes, I was also thinking about this, too. Having the translations external is both very common and offers a real benefit: translators don't have to know anything about the code.
There was a problem hiding this comment.
I'm really glad you brought the issue up, I honestly didn't think much about it in my hurry to get something working. Let's find some to plan the new architecture out.
|
Hi @Ulexus, thanks, these are very generous review comments. I should be able to address a good number of them tomorrow. |
|
I forced pushed the commits that were missing signatures. Not sure why they're missing, likely an artifact of upgrading my laptop. |
…ch loads from a file.
…public API. Creates YAML test fixture files rather than hardcoding and modifying data structures in the tests directly. Removes tests that only test third party dependencies.
Moves loadTemplates() into a separate file.
Adds test fixture views instead of writing temporary test files.
Ulexus
left a comment
There was a problem hiding this comment.
Given your deadline, I'm going to go ahead and merge this. The remaining items of design and style issues, not functional impediments or dangerous code.
We can always follow up with the open items later.
| return nil, fmt.Errorf("failed to read agenda from file: %w", err) | ||
| } | ||
|
|
||
| return New(bytes.NewReader(data)) |
There was a problem hiding this comment.
If you use os.Open() instead of os.ReadFile(), you get an io.Reader back (a file handle), which can be passed directly to New(). Just make sure you include a defer f.Close() on the returned file handle, so it gets closed after reading. That is handled automatically by os.ReadFile(), but you have to do it yourself for os.Open().
Apologies for the huge pull request, ordinarily I'd have done this in smaller chunks.
In addition to adding i18n support, this PR also includes:
/appand/cssI'll issue a PR for the Audimance demo show repository that includes localizing it into Traditional Chinese as an example of how this new functionality works.
Happy to make any changes you think are needed to improve this (or to remove any of the non-i18n changes if you think they're not worth it), @Ulexus.