[GH-ISSUE #463] Translation strategy / demo #332

Closed
opened 2026-03-04 01:04:05 +03:00 by kerem · 3 comments
Owner

Originally created by @mpictor on GitHub (Jun 24, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/463

This is the first time I've dealt with translations, and I'm not certain of the best approach. I'm writing an app which needs to ask the user some configuration questions; currently I'm using tview.Pages, each with a simple form. The first question (first page) asks the user to select a language. When they do, I want translatable strings on all pages to update.

Currently I am defining all pages in the UI before the language question is asked, but it seems there's no easy way to enumerate the elements and change their text. I'm also not seeing a way to define elements that only load their text when they become visible; it'd be nice for my use case but I suppose this use case is an exception.

My current thinking is that it'll be best to delete and re-define pages in the ui when the language changes - or to only allow the language to be set once, and only define the remaining pages after the language is set.

Does this approach make sense to you? Is it how you'd do things?

I was also wondering if you'd create a demo showing how you'd approach the problem.


If it helps, this is what I'm doing for translations:

I'm using a to-be-written package xlate. In that package, func T(string)string takes a string in the default language and looks it up in the translation resource matching the string var Language, returning the appropriate string for that language.

	xlate.Language = "Ελληνικά" //set from dropdown
	//...
	tview.NewForm().
		/*...*/.
		AddButton(xlate.T(BtnNext), func() { /*...*/ })

Thanks for writing tview, I'm finding it quite useful!

Originally created by @mpictor on GitHub (Jun 24, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/463 This is the first time I've dealt with translations, and I'm not certain of the best approach. I'm writing an app which needs to ask the user some configuration questions; currently I'm using tview.Pages, each with a simple form. The first question (first page) asks the user to select a language. When they do, I want translatable strings on all pages to update. Currently I am defining all pages in the UI before the language question is asked, but it seems there's no easy way to enumerate the elements and change their text. I'm also not seeing a way to define elements that only load their text when they become visible; it'd be nice for my use case but I suppose this use case is an exception. My current thinking is that it'll be best to delete and re-define pages in the ui when the language changes - or to only allow the language to be set once, and only define the remaining pages after the language is set. Does this approach make sense to you? Is it how you'd do things? I was also wondering if you'd create a demo showing how you'd approach the problem. ---- If it helps, this is what I'm doing for translations: I'm using a to-be-written package `xlate`. In that package, `func T(string)string` takes a string in the default language and looks it up in the translation resource matching the string var `Language`, returning the appropriate string for that language. ```golang xlate.Language = "Ελληνικά" //set from dropdown //... tview.NewForm(). /*...*/. AddButton(xlate.T(BtnNext), func() { /*...*/ }) ``` ---- Thanks for writing tview, I'm finding it quite useful!
kerem closed this issue 2026-03-04 01:04:05 +03:00
Author
Owner

@rivo commented on GitHub (Jun 28, 2020):

The question is whether there are any users at all who will change the language mid-way through such a configuration walkthrough. Personally, I've never done that. I would think it's ok to create all pages, translated, after the first form which is "Select your language".

Of course, if your forms are complex, it can be a bit difficult to change all labels and texts. There is Form.GetFormItem() but you'll still need to cast to the actual Primitive type (e.g. using switch). I would think it's possible. Let me know if there is a specific function that you're missing.

Another alternative is to create pages for all languages but only show the ones for the language that was picked. Since you can name pages, you could append language codes, e.g. "config1_en" and "config1_el".

<!-- gh-comment-id:650713172 --> @rivo commented on GitHub (Jun 28, 2020): The question is whether there are any users at all who will change the language mid-way through such a configuration walkthrough. Personally, I've never done that. I would think it's ok to create all pages, translated, after the first form which is "Select your language". Of course, if your forms are complex, it can be a bit difficult to change all labels and texts. There is [`Form.GetFormItem()`](https://pkg.go.dev/github.com/rivo/tview?tab=doc#Form.GetFormItem) but you'll still need to cast to the actual Primitive type (e.g. using `switch`). I would think it's possible. Let me know if there is a specific function that you're missing. Another alternative is to create pages for all languages but only show the ones for the language that was picked. Since you can name pages, you could append language codes, e.g. "config1_en" and "config1_el".
Author
Owner

@rivo commented on GitHub (Jul 12, 2020):

If there are no follow-up questions, I'll close this issue soon.

<!-- gh-comment-id:657210178 --> @rivo commented on GitHub (Jul 12, 2020): If there are no follow-up questions, I'll close this issue soon.
Author
Owner

@mpictor commented on GitHub (Jul 13, 2020):

Thanks. What I ended up doing was re-creating all pages whenever the language changes. This means the user can go back and switch languages, if desired - and I don't have to iterate through the items/determine type/etc.

<!-- gh-comment-id:657768950 --> @mpictor commented on GitHub (Jul 13, 2020): Thanks. What I ended up doing was re-creating all pages whenever the language changes. This means the user can go back and switch languages, if desired - and I don't have to iterate through the items/determine type/etc.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#332
No description provided.