How to call i18next.tr function with different locale? - i18next

I have two folders, fi and sv. Both have their own translation.json files. Calling i18n.tr(text) function works fine, it returns the text from the current language's .json file.
However, sometimes I need to access text from the .json files of different locales. Does i18n allow to pass language as a parameter? Is calling setLocale(language) necessary in order to retrieve text from other language's .json files?

You can pass the {lng: 'sv'} to the t function in order to override the current language.
t('MY.KEY', {lng: 'other-locale'})

Related

How to require multiple modules in a single statement?

I want to require several Lua modules at once, similar the to the asterisk signifier from Java (import java.awt.*). This the structure I organized my modules in subdirectories:
<myapp>
-- calculations
-- calc1
-- calc2
-- calc3
-- helper
-- help1
-- help2
-- print
--graphprinter
--matrixprinter
My client requires each module of a subpath:
local graphprinter = require("myapp.helper.print.graphprinter")
local matrixprinter = require("myapp.helper.print.matrixprinter")
I would prefer an automatic multi-require, which derives the local table names from the module path and requires a whole subpath at once. This could be the format: require("myapp.helper.print.*"). Automatically the local table names should be created for each module of the subdirectory, so that there isn't any difference as I would have required them module by module.
Why don't you just write an init.lua file for each folder that requires all the other libraries?
For example, in calculations you write a file that contains
return {
calc1 = require "calc1";
calc2 = require "calc2";
calc3 = require "calc3";
}
Then you can just write calculations = require "calculations" and it will automatically load calculations.calc<1-3>
This can be done for the entire directory structure, and require "helper" can call require "help1" which in turn calls require "print" and in the end you can find your functions in helper.help1.print.<function>
Short explanation of how this works: When you run require "library" lua will either try to include a file called library.lua or the file init.lua located in a library directory. This is also the reason why you do require "dir.lib" instead of require "dir/lib"; because, if done right, when you just require "dir" it will return a table that contains the field lib, so you would access it as dir.lib.<function>.
The module env partially achieves what you are looking for, though it is far from perfect.
It allows for grouped / named imports, with some caveats - the main one being you must manually manage your environments. In addition, you'll need to write index files (default init.lua, unless you write a custom path set), since it is intended for use with modules that export tables.
Here's a bit of an example. First we need to properly set up our file structure.
-- main.lua
-- calculations /
-- calc1.lua
-- calc2.lua
-- calc3.lua
-- init.lua
-- helper /
-- print /
-- init.lua
-- graphprinter.lua
-- matrixprinter.lua
The index files, which are slightly tedious:
-- calculations/init
return {
calc1 = require 'calculations.calc1',
calc2 = require 'calculations.calc2',
calc3 = require 'calculations.calc3'
}
and
-- helpers/print/init
return {
graphprinter = require 'helper.print.graphprinter',
matrixprinter = require 'helper.print.matrixprinter'
}
Inside your main file. The major caveat shows itself quickly, you must use the function returned by requiring 'env' to override your local environment. Passing no arguments will create a clone of your current environment (preserving require, etc.).
-- main.lua
local _ENV = require 'env' () -- (see notes below)
The new environment will be given an import function, which takes a single argument, a string represent the path or module name to import into the current environment. The return value is a transient table that can be used to further alter the environment state.
import 'helper/print' :use '*'
import 'calculations' :use '*'
One of the functions on the transient table is :use, which either takes a table indicating which values to pull from the required table, or the string '*', which indicates you want all values from the required table placed in your current environment
print(matrixprinter, graphprinter) --> function: 0x{...} function: 0x{...} (or so)
The final caveat is that all the paths you've seen are reliant on the cwd being the same as the one that holds main.lua. lua myapp/main.lua will fail loudly, unless you place your sub modules in a static location, and adjust package.path / import.paths correctly.
Seems like a lot of work to avoid a couple of lines of require statements.
Disclaimer: I wrote env as a bit of an experiment.
Note that import does not currently support the . syntax (you need to use your OS path delimiter), or proper exploding of tables into table chains. I have a bit of a patch in the works that addresses this.
Lua 5.2+ uses _ENV to override local environments. For Lua 5.1, you'll need to use setfenv.
As mentioned above, Lua has no real notion of directories. To really do what you want (with less overhead), you'll need to write your own custom module loader, environment handler, and likely make use of a module like LuaFileSystem to reliably 'automatically' load all files in a directory.
TL;DR:
This is a tricky topic.
There's nothing built into the language to make this easy.
You'll need to write something custom.
There will always be drawbacks.

Load DLL with parameters

I need to delete every single function from my project (VB.Net) and place them in separate DLLs. For example, i have a function that creates a text file and appends text to it. I need a solution that will load my AppendToTextFile.DLL with params just like a function works.
At this time my function accepts two parameters like
AppendToTextFile("C:\test\textFile.txt", "text to be appended")
Does anyone know how to load a custom DLL with params like the function above?
Create your custom DLL.
Then add it as reference to your project.
Call you function and use it like this :
Dim mydll As New MyCustomeDll
mydll.AppendToTextFile("C:\test\textFile.txt", "text to be appended")
That's all.

Can you call "require" on a variable?

I have a lua file that will require another lua file to run but I can't hard code the file name. Can i use the require function on a variable or do i need to figure out an alternate approach to what i am doing?
For example
local path = "mypath.txt"
local level = require path
Yes, you can. require "module" is just a syntactic sugar for require("module") that only works if call a function with single argument that is a string or table constructor. Use proper call in form of require(path) and it will work.

SciLab: where do functions live / need to live?

I'm learning SciLab and I need to figure out the equivalent from MATLAB for running user-defined functions.
I'm used to MATLAB, where when you type foo(27), it looks for the foo.m script in the current directory and then the MATLAB path, and if it finds one, it calls that function with argument 27.
What's the equivalent to SciLab? It doesn't seem to want to look in the current directory for the appropriate .sci file.
In Scilab, you need to explicitly load the script that contains the function. Assuming you've changed your directory to the directory where the function file is loaded, which can be done in Scilab using the menu buttons or the following command:
cd("path/to/working/directory")
Now you load the function file. Assuming the function foo is stored in a file called foo.sci, you need to load this script using the following command:
exec("foo.sci")
Now you should be able to use your function as you would be able in MATLAB.
foo(27)

The right file name for an enum?

I have a DataType (text, numeric, date, boolean, money) enum and I need to put that in a file. How should I call the file and where in my DataAccessLayer.Generic namespace should I put the file into? Right now the file is called enum.vb and is in the DataAccessLayer.Generic.DataTypes namespace. The Result is DataAccessLayer.Generic.DataTypes.DataType, but it is misleading because the file is called enum.vb
Thanks
I would call the file DataType.vb since that is the name of the type within the file.
Without knowing more about your application's architecture it would be difficult to give namespace advice but this should be fine:
DataAccessLayer.Generic.DataTypes.DataType
Especially if the DataAccessLayer.Generic.DataTypes namespace contains other types and the enumeration in question belongs with them.
I have put Enums into a file which I call 'Common', meaning it is shared among the entire namespace. I know that breaks 1 item per file rule, to me Enums are an exception to that rule.