How to use HTML inside of translatable strings in Qt? - qt

The Qt tutorial for translatable texts does not mention whether it is possible to use HTML inside of a translatable string.
However, some examples I found use it, so it seems to be possible.
However, if I so much as use "<B>text</B>" in my .ts file, Qt Linguist does not open it. It displays the error message "unexpected tag" instead.

The ts file is XML. If you want to use HTML tags you will need to escape them. For example: <source><b>text</b></source>
Note that the recommended way of managing translations in Qt is to run the lupdate tool on your source code and have Qt generate the .ts file, in which case this will be done automatically.

Related

How can I make VSCode syntax-highlight and give suggestions for CSS written inside tagged template literals in TypeScript files?

When using Angular with VSCode I have seen in tutorials how people have been able to write the "styles" part of "#Component" in Strings using backticks (`) to allow multiline Strings to be directly written as CSS code inside an "app.component.ts" file. The tutorials show that as when written, all expressions typed in allows faster autocompletion with suggestions for values presented after an element has been added.
Like for example if "color:" is used suggested avaliable colors whose values can be used appear.
For me however no such suggestions ever appear when writting within multiline Strings inside a TypeScript file for CSS, although they do appear when using a regular CSS file, as well as when being written within a "style" element inside a HTML file. Still even though no autocomplete suggestions appear withing my TypeScript file, the CSS text code changes color depending on the used elements and values exactly as they do in CSS, so the TypeScript file does indeed recognize the CSS code as CSS, and the results when the server is run looks exactly as they should.
But how do I make it so that I still can get autocomplete suggestions when writting CSS code inside TypeScript to make things easier though?
import { Component } from '#angular/core';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styles: [
`
h3 {
color: /*<--Expects automatic suggestions for auto completion, but I never get any.*/
}
`,
],
})
export class AppComponent {
title = 'my-dream-app';
}
This is related to this github issue: Extension providing language support in ES6 template strings #5961, which was resolved by this comment by one of the maintainers, Matt Bierner:
VS Code Extensions can now bundle ts server plugins. This allows extension authors to write a vscode extension that adds syntax highlighting + intellisense for template strings.
Two extensions are already making use of this:
vscode-styled-components — Syntax highlighting, IntelliSense, and error reporting for styled-component CSS strings.
lit-html — Syntax highlighting, IntelliSense, and formatting for lit-html template strings
If you are interested in writing an extension for a template string embedded language, take a look at how the implementation of these extensions. And let me know if you run into any problems or have any questions about the new contribution point (please open a new issue or post a question to stack overflow)
Note: The link to the Visual Studio Marketplace page for vscode-styled-components in the above quote is now dead. I think it has moved to here (but don't quote me on that. I'm not 100% sure what the history is here.)
I'm not aware of CSS currently having such a ts server plugin, so what you can do in the meantime before one is created is:
You can use the Template Literal Editor extension.
Quoting from its readme:
Open ES6 template literals and other configurable multi-line strings or heredocs in any language in a synced editor, with language support (HTML, CSS, SQL, shell, markdown etc).
Instructions:
Install extension to VS Code.
Open a JavaScript or TypeScript file, or a file in some other language if customized via "templateLiteralEditor.regexes" configuration. Many languages have a starter configuration included.
Place cursor inside any template literal string and press Ctrl+Enter.
Select language (defaults to html). Remembers the last selection as default.
Outermost template literal range opens in the selected language in a side-by-side view, synced with the original. Multiple cursors and undo work as usual, and saving the template results in saving of the original document.
Enjoy syntax highlighting, completions, formatting, commenting, snippets, your preferred editing extensions, etc!
When you edit the original document the template editor is kept in sync. If template boundaries are modified or a sync error happens, then the template literal editor is closed for safety.
Ctrl+Enter in the literal editor closes it and keeps the cursor position. Also ordinary close or "Revert And Close Editor" action should work without unnecessary save dialogs. There's also a Ctrl+Shift+Backspace shortcut to close all literal editors quickly, from any editor.
Other related tools:
This won't add suggestions, but for syntax highlighting in tagged template literal strings, there is the comment-tagged-templates extension by Matt Bierner. If you're already using styled-components, you can use vscode-styled-components

Embed file content in ARM template

I have an ARM template, which I use to deploy a resource which has a property that contains a Python script as a string. Instead of having the script in the template file, I would prefer to have it in a .py file, and embed that in the template on deployment.
Is there a way to achieve this?
The simplest way to do this is using loadTextContent() in bicep: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-files#loadtextcontent
If you're not using bicep (and can't) there's no good substitution for it in JSON, you'd have to fashion something to parse the json, figure out where that goes and stringify yourself.
That help?

How to use .ui.qml files from Qt creator version 6

I've been some reading/watching some tutorials here and there about qt quick and qml from last few days. But the user interface of qt creator, the boilerplate that it produces on a new qt quick project in each of the tutorial had been different. there's so much confusion.
I want to make a simple Hello World Qt Quick project, I followed this book from packt. (It has changed from the last time I saw it, but still doesn't work)
I added some a UI file, which added *.ui.qml and *form.qml files. Now if I edit .ui.qml in design, No change is reflected on any other file, but if I open some other file in design, it prompts me to use the .ui.qml file instead. The examples give an error "No Cmake config found". I'm using Qt6.2 on Arch/wayland, downloaded via installer.
How is it supposed to work? I have read this answer, and few others but I'm still confused.
Could you link to a proper tutorial, or a simple example?
The best way to think about it is that *.ui.qml should be used for UI elements and their settings. They should be about the structure, layout and styling of your UI. Doing that also happens to make the files presentable and editable in Creator's Design Mode since it simplifies the structure of the QML to the point where it can reliable present it and modify it.
*.qml files on the other shouldn't have UI elements and should instead primarily contain functions, properties and signal handers.
This pattern is sometimes referred to as "code behind". It sort of follows the same philosophy with HTML and CSS. Modern HTML documents primarily represent the structure of a page while CSS is placed in separate files to govern how they should be styled.
In this case *.ui.qml files contain the structure and styling while *.qml files containing the behavior. Creator will create a pair of them while adding a QML UI resource to your project where the .qml file inherits from the .ui.qml file.
Other than this inheritance and to encourage the code behind pattern, there's no real difference between a .qml and .ui.qml file. Creator will also try to steer you to the .ui.qml version of the pair when you go into designer to help encourage this pattern.
I will note that Qt violates this pattern a bunch all over their documentation and examples....

Qt Linguist: translation of text changed dynamically in tr()

I am using qt linguist and qt 5.2. I have a problem that when I add something to .ui files I can see it after lupdate in ts file but when I change name of this button dynamically in my code like:
ui->label->setText(tr("foo"));
I can't see it in ts file, is it possible to make translation in that case? How I can do this?
Basically, tr() can be detected anywhere in the codes.For ui files, linguist would detect the changes immediately, however in codes I encounterd the same problem.
Based on my experience, try to reBuilt/qmake it.
(I am using Qt4.8 & 5.1.)

Help context tagging Qt with slickedit

I am trying to get Slickedit working with the Qt framework so that I can navigate the codebase easily and because of the other features it provides. To context tag the library I go to Tools > Tag Files, then I right click on C/C++ to add tag file. I name it Qt.vtg and then select the following trees:
C:\Qt\2010.05\qt\src
C:\Qt\2010.05\qt\include
I get context tagging for something, but I can't get context tagging for some scenarios where I normally get it:
When I try to get the parameters for a second pointer, it shows nothing:
oldAddress = ui->addressText->
When I try to complete specific headers it also gives no help
#include <QtCore/ >
I don't know if I am missing a step for context tagging QT, or the additional steps for context tagging QT.
Ted.
You have to provide the include directories of QT for your project, either in your project's properties (Menu Project, Project properties, directories' tab), or in your compiler's properties (Menu Tools, Options, search for "Compiler properties"). This will enable your project to find the include files; the tag file alone doesn't really help, since you may have conflicting tags in the many tag files you could have, so the include files are necessary.
Make sure you have activated all the macros QT provides to your build system. It's been ten years since I used Qt, but IIRC there were several macro defines optionally specified on a compiler command line; make sure SlickEdit knows about them. (Menu Project, Project properties, compile/link tab).
If that's not sufficient, please give more details (where the definition is contained, settings for autocomplete, for context tagging, etc).

Resources