How to translate undefined strings with PySide - internationalization

About the process of internationalization with PySide.
The problem is the translation of dynamic variables.
For example:
self.tr("Saved %1 in file %2").arg(datetime).arg(filename)
This can be used because of PyQt.
And QTranslator doesn't keep up with format.
For example:
self.tr("Saved %s in file %s" % (datetime, filename))
They say that this is a bad way for QTranslator.
How to do the same thing with PySide?
Environment
Python3.10.8 PySide6.4

Related

Embeddable Common-Lisp asdf:defsystem returning invalid relative pathname

I'm trying to learn how to use Common-Lisp's asdf, and I have the following code:
(asdf:defsystem example
:serial t
:components ((:file "first")
(:file "second")))
However, I keep getting the error:
Condition of type: SIMPLE-ERROR
Invalid relative pathname #P"first.lisp" for component ("example" "first")
I'm launching the repl in the same directory as these two Lisp files, but I don't understand why there is an error. What am I missing? I'm using ECL on Windows
ASDF uses *load-pathname* or *load-truename* to resolve the full paths to the system's components. If you enter the (asdf:defsystem ...) form on the REPL, these variables are not set.
Write the defsystem form into a file, then load it like (load "example.asd").

Can't use io.open in home directory - Lua

I'm writing a Mac OS program, and I have the following lines:
os.execute("cd ~/testdir")
configfile = io.open("configfile.cfg", "w")
configfile:write("hello")
configfile:close()
The problem is, it only creates the configfile in the scripts current directory instead of the folder I have just cd' into. I realised this is because I'm using a console command to change directory, then direct Lua code to write the file. To combat this I changed the code to this:
configfile = io.open("~/testdir/configfile.cfg", "w")
However I get the following result:
lua: ifontinst.lua:22: attempt to index global 'configfile' (a nil value)
stack traceback:
ifontinst.lua:22: in main chunk
My question is, what's the correct way to use IO.Open to create a file in a folder I have just created in the users home directory?
I appreciate I'm making a rookie mistake here, so I apologise if you waste your time on me.
You have problems with ~ symbol. In your os.execute("cd ~/testdir") is the shell who interprets the symbol and replaces it by your home path. However, in io.open("~/testdir/configfile.cfg", "w") is Lua who receives the string and Lua doesn't interprets this symbol, so your program tries to open a file in the incorrect folder. One simple solution is to call os.getenv("HOME") and concatenate the path string with your file path:
configfile = io.open(os.getenv("HOME").."/testdir/configfile.cfg", "w")
In order to improve error messages I suggests you to wrap io.open() using assert() function:
configfile = assert( io.open(os.getenv("HOME").."/testdir/configfile.cfg", "w") )

Python: Call a shell script which calls a bin. With arguments

The context: There is a map somewhere on the system with bin files which I'd like to call. They are not callable directly though, but through shell scripts which do all kinds of magic and then call the corresponding bin with: "$ENV_VAR/path/to/the/bin" "$#" (the software is non-free, that's probably why this construction is used)
The problem: Calling this from within Python. I tried to use:
from subprocess import call
call(["nameOfBin", "-input somefile"])
But this gave the error ERROR: nameOfBin - Illegal option: input somefile. This means the '-' sign in front of 'input' has disapeared along the way (putting more '-' signs in front doesn't help).
Possible solutions:
1: In some way preserving the '-' sign so the bin at the end actually takes '-input' as an option instead of 'input'.
2: Fix the magic in a dirty way (I will probably manage), and have a way to call a bin at a location defined by a $ENV_VAR (environment variable).
I searched for both methods, but appearantly nobody before me had such a problem (or I didn't see it: Sorry if that's the case).
Each item in the list should be a single argument. Replace "-input somefile" with "-input", "somefile":
from subprocess import call
rc = call(["nameOfBin", "-input", "somefile"])

Write to file as CSV

I'm using this bit of code that I found on stackoverflow to write data to a file.
begin
file = File.open("/tmp/some_file", "w")
file.write("your text")
rescue IOError => e
#some error occur, dir not writable etc.
ensure
file.close unless file == nil
end
where it says your text if I put one of my variable names it will fill in the correct string used as that variable but ideally what I want is to have more than one variable in the output ideally in CSV. Something like:
file.write(var1 ^ var2 ^ var3)
Where each of my variables are seperated by a carrot symbol. How can I do this?
Actually There is a gem for CSV although there is a CSV library in standard ruby library.
Take a look at examples and you will find out that writing CSV files in ruby is much more easier that you thought.
http://fastercsv.rubyforge.org/classes/FasterCSV.html
http://fastercsv.rubyforge.org/
Ruby Stdlib: http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#method-c-generate

Why is LUA_PATH containing a bang (!) mangled on Windows?

I have a file main.lua:
require("hello")
and a file hello.lua in the directory foo bar! baz (with !) in it:
module(...,package.seeall)
print("hello from hello.lua")
when I set (on Windows) the environment variable LUA_PATH to the directory:
set LUA_PATH="C:\Programme\Lua\5.1\foo bar! baz\?.lua"
I get a strange error:
C:\Programme\Lua\5.1>lua main.lua
lua: main.lua:4: module 'hello' not found:
no field package.preload['hello']
no file '"C:\Programme\Lua\5.1\foo barC:\Programme\Lua\5.1 baz\hello.lua
"'
no file '.\hello.dll'
no file '.\hello51.dll'
no file 'C:\Programme\Lua\5.1\hello.dll'
no file 'C:\Programme\Lua\5.1\hello51.dll'
no file 'C:\Programme\Lua\5.1\clibs\hello.dll'
no file 'C:\Programme\Lua\5.1\clibs\hello51.dll'
no file 'C:\Programme\Lua\5.1\loadall.dll'
no file 'C:\Programme\Lua\5.1\clibs\loadall.dll'
stack traceback:
[C]: in function 'require'
main.lua:4: in main chunk
[C]: ?
See the very strange path C:\Programme\Lua\5.1\foo barC:\Programme\Lua\5.1 baz\hello.lua? What is so special about the exclamation mark (bang)? How to get that right in windows? On Mac this seems to be working fine.
"This substitution only happens the first time Lua sets the path
(either from LUA_PATH or from the default from luaconf). If you
set the path by other means, you can avoid the problem."
More info and potential alternatives: http://lua-users.org/lists/lua-l/2012-08/msg00052.html
The explanation is correct (it's documented behavior on Windows), but I can also offer a possible solution (or rather a workaround): since only the first exclamation mark is going to be replaced, instead of using set LUA_PATH="C:\Programme\Lua\5.1\foo bar! baz\?.lua", use:
set LUA_PATH="!\foo bar! baz\?.lua"

Resources