Android Question [Solved] Problem with the text engine of BBCODE when using anchors

BlueVision

Active Member
Licensed User
Longtime User
I have tried the BBCode-Anchors-Example from Erel. So far so good. That's exactly what I need. But I have a problem. My chapters do not consist of static text, but come from a localisation table.
This causes a problem with the text engine and I have not yet found a way to solve this problem. Perhaps one of you has an idea.
So far I have stored the text in a webview as an HTML file with integrated links within the document. This worked perfectly, but has the disadvantage that images cannot be integrated so easily. The text itself is available in 15 different languages... So up to now there were 15 different HTML documents, which were then loaded according to the language. A huge administrative effort when making changes...
With BBCode, this effort would be significantly reduced.
Working code with static text based on Erel's example:
    Dim s As String = $"
[a=top]Chapters:[/a]
[url]Chapter 1[/url]
[url]Chapter 2[/url]
[url]Chapter 3[/url]

[a="Chapter 1"][b]Chapter 1[/b][/a] dolor sit amet, consectetur adipiscing elit.
[url=top]Top [FontAwesome=0xF062/][/url]

[a="Chapter 2"][b]Chapter 2[/b][/a] imperdiet pharetra scelerisque. Suspendisse potenti.
[url=top]Top [FontAwesome=0xF062/][/url]

[a="Chapter 3"][b]Chapter 3[/b][/a] pretium commodo mauris quis egestas.
[url=top]Top [FontAwesome=0xF062/][/url]
"$
    BBCodeHelp.Text = s
The problem with using BBCode is as follows:
Failing code with variable text:
    Dim s As String = $"
[a=top]Chapters:[/a]
[url]"$&LOC.Localize("Chapter 1")&$"[/url]
[url]Chapter 2[/url]
[url]Chapter 3[/url]

[a=""$&LOC.Localize("Chapter 1")&$"][b]Chapter 1[/b][/a] dolor sit amet, consectetur adipiscing elit.
[url=top]Top [FontAwesome=0xF062/][/url]

[a="Chapter 2"][b]Chapter 2[/b][/a] imperdiet pharetra scelerisque. Suspendisse potenti.
[url=top]Top [FontAwesome=0xF062/][/url]

[a="Chapter 3"][b]Chapter 3[/b][/a] pretium commodo mauris quis egestas.
[url=top]Top [FontAwesome=0xF062/][/url]
"$
    BBCodeHelp.Text = s
When inserting variable strings in BBCode, as I understand it, an end marker must be set, followed by an ‘&’, then the string from the localisation table, then another ‘&’, followed by the marker for activating the text engine.
Funnily enough, there is no error message when coding, but when executing the code, the text engine gets confused. In my opinion, the reason for this is the two consecutive quotation marks in line 7.
What am I doing wrong or how can I solve the problem? Does anyone have any ideas? Maybe it's just too late again and I should just take a break. But somehow I'm deep in the woods at the moment...

Cheers BV
 
Solution
It should be simple. Smart strings allow you to embed anything you need:

B4X:
    Dim s As String = $"
[a=top]Chapters:[/a]
[url]${LOC.Localize("Chapter 1")}[/url]
[url]Chapter 2[/url]
[url]Chapter 3[/url]"$

teddybear

Well-Known Member
Licensed User
You can assign LOC.Localize("Chapter 1") to a str then use ${str} in the smart string

B4X:
    Dim localstr as string=LOC.Localize("Chapter 1")
    Dim s As String = $"
[a=top]Chapters:[/a]
[url]${localstr}[/url]
[url]Chapter 2[/url]
[url]Chapter 3[/url]

[a=${localstr}][b]Chapter 1[/b][/a] dolor sit amet, consectetur adipiscing elit.
[url=top]Top [FontAwesome=0xF062/][/url]

[a="Chapter 2"][b]Chapter 2[/b][/a] imperdiet pharetra scelerisque. Suspendisse potenti.
[url=top]Top [FontAwesome=0xF062/][/url]

[a="Chapter 3"][b]Chapter 3[/b][/a] pretium commodo mauris quis egestas.
[url=top]Top [FontAwesome=0xF062/][/url]
"$
    BBCodeHelp.Text = s
 
Upvote 0

BlueVision

Active Member
Licensed User
Longtime User
Thank you for your answer Teddybear. I already had this idea, but then found the assignment of an existing variable to a new local variable to be overcomplicated. In the case of anchors, however, the syntax used in BBcode probably leaves me no other option. The problem only occurs with the anchors, i.e. the chapter headings. My text has 15 chapters, which would still be acceptable and would not cause much extra work. The remaining text can be directly filled with the strings from the localisation table. This trick is not necessary there.
The BBCode is not exactly clear compared to the existing HTML document. But it is extremely flexible, because I only have to change the string in the localisation table when making changes, which I see as a great advantage.
I plan to assign a variable in the translation table to each paragraph in the existing text. This usually means several sentences. The existing UTF-8 HTML document created with MS WORD is about 10 pages long, so it is relatively large. I have to test whether the variant with BBCode works in terms of performance anyway, because lazy loading is currently activated in the BBCode view, which could perhaps become a problem if the jumps become too large. Only testing will help.

However, I am of the opinion that the effort for the changeover would be worthwhile, as the web view would no longer be required.
 
Upvote 0

BlueVision

Active Member
Licensed User
Longtime User
Works fantastically. Thanks for your help. Now I just have to work hard on the translation. I'm curious to see how the performance of the entire construct compares to the Webview.
 
Upvote 0

BlueVision

Active Member
Licensed User
Longtime User
After some days of testing I can confirm that the performance of the whole construct is slightly better compared using a webview. It's a very smooth solution.
Only disadvantage when using it in conjunction with localisation is, it's a little bit harder to keep on track. A small mistake within the BBCode breaks everything. So you have to work very carefully and concentrated while writing the code. Debugging it is pure hell. So best practice is to work with predefined modules, pasting and copying them.
Meets my expectations more I thought, it's very flexible.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…