Changing between two .properties with a same template in FreeMarker - templates

I am new to FreeMarker, and what I would like to accomplish is switch between different properties using the same template file, in order to send e-mails in different languages.
My default properties file is messages.properties, and the second file (non-default) I would like to use is messages_fr.properties.
A template file is template.ftl
The code I have is:
Map<String, Object> content = new HashMap<String, Object>();
content.put(I18N, i18nService);
content.put("someText", "a text";
content.put("language", language);
String setBodyTextMessage = commonService.getProcessedTemplate("template.ftl", content);
What I am struggling to find out is how to do the same thing as in the code I provided, but by using the messages_fr.properties without changing the configuration file (I would like to make the switch within the Java code). Furthermore, setting a different locale will not work either.

You have a data-model there, called content. You put into that whatever you want, with Java code. Each template processing can use its own data-model. So I'm not sure where do you stuck.
Or, you can create a ResourceBundle and put that into the data-model (even as shared variable on the Configuration-level), and then set the locale setting for that single request (not in the shared Configuration object). To do that, instead of Configuration.getTemplate use Template.createProcessingEnvironment. In the returned freemarker.core.Environment you can set the locale (and other settings), and then call Environment.process to generate the output.

Related

Boost.Locale translation - Preventing user modifications to dictionaries / embed dictionaries in executable

I use Boost.Locale with ICU backend (internally using GNU gettext) to do the translations. This translation uses dictionaries stored on disk. Search paths are provided through boost's generator class like so:
boost::locale:generator gen;
gen.add_messages_path("path");
By inspecting the boost's source code, this internally later passes the paths to localzation backend through localization_backend::set_option. In case of ICU localization backend implementation that I use, this finally makes the paths to be set in gnu_gettext::messages_info (paths field).
Now as for my question - my requirement is to make sure that the user will not change the texts e.g. by modifying the .mo dictionary file on disk. The reason I use Boost.Locale is its codepage translations support, multiple languages support etc. and I want to keep that, but I don't want the ability for the user to freely define the texts later used in the application. My initial thought was to use the dictionaries "in memory" in some way, e.g. by storing .mo file contents inside executable and pass already read data into the localization_backend somehow. However, after checking how it works internally (described above) it seems that the only supported option is to have the dictionaries read in "real time" as I do the translations, which may include any changes to those files done by the user. It's either that or maybe I'm missing something?
What are my options?
You can use the callback field on gnu_gettext::messages_info to provide a function that will be called instead of loading messages files from disk. From Custom Filesystem Support:
namespace blg = boost::locale::gnu_gettext;
blg::messages_info info;
info.language = "he";
info.country = "IL";
info.encoding="UTF-8";
info.paths.push_back(""); // You need some even empty path
info.domains.push_back(blg::messages_info::domain("my_app"));
info.callback = some_file_loader; // Provide a callback
The callback signature is std::vector<char>(std::string const &file_name, std::string const &encoding). There's an example in the tests; this actually loads from disk, but you can adapt it to return hard-coded data instead.

Can I use a variable as a text property when calling class window?

I have a child object class window with a parameter regexptitle, so text being changed dynamically depending on the regular expression. I need to check if this window is opened and active using variable. I tried to put there string but it didn't help. Please help me find a solution.
Example of code repeated:
If Window("Excel").Window("Prompts for Project Analysis"). Exist Then ...
If Window("Excel").Window("Prompts for Engagements"). Exist Then ...
Assuming I read your question correctly, you should be able to achieve the goal using descriptive programming with this:
If Window("Excel").Window("regexptitle:=Prompts for.*"). Exist Then
Or if you have at least one version of the window learned in the object repository, add the regexptitle property to the test object details under Description properties, set it to a regular expression and set its value to be "Prompts for.*" - this will cause UFT to recognise all windows of this class with a regexptitle beginning "Prompts for" as this object (assuming the other recognition properties match up as well, and you get to use the OR-friendly object name in your code.
Let me know if that works for you, or if you need further help.

How do I provide default YAML configuration values in a static library?

I have a configuration file system written in C++ which uses the yaml-cpp library to parse and write to YAML files. I have this as part of my static library.
I would like the ability to return a default value for a field that is requested by a user of the library (calling from their code), but which has not been defined in the user's YAML file.
For example say the user wants to use the field foo from their custom config.yaml file:
int bar = config_reader.read<int>( "config.yaml", "foo" );
If they have foo: 10 in their config.yaml then bar will be set to 10. However I would also like to provide a default value (for example 4) in the case where foo is omitted from config.yaml.
There are two possibilities I have thought of:
Have a set of static maps between field names and default values in a cpp file which gets compiled into the static library, however I will need to have different maps for different types and I feel this could get messy with type checking and maybe requiring template specialization methods.
Have a YAML file which contains all of the default values for expected fields, which the configuration system falls back on if it cannot find the field in the user's config file. I think this would be the preferred solution for me, but I cannot think of a neat way of packaging this YAML file. I would rather the user didn't have to copy or point to this file each time they set up a new project linking the static library.
I would provide the defaults in a YAML file in a global (i.e. non-user specific place) and allow to override the values with user-specific ones.
Consider just throwing an error if the global defaults are missing an entry, this will not happen by accident.
The global defaults you can put in /etc/default/YOUBLIBNAME.yaml. The user's configuration nowadays mostly follows the XDG base directory specification. For that use $XDG_CONFIG_HOME/YOURLIBNAME/config.yaml if XDG_CONFIG_HOME is set in the environment, if not set use $HOME/.config/YOURLIBNAME/config.yaml.
If your library has to work under Windows, I would put the user specific data under %APPDATA% in a subdir YOURBLINAME.

Possible to update an existing file when using file templates?

I have a multi-file template that generates an interface and an implementation. I would like to update the IoC mappings in my solution, but this doesn't require a new file; it requires a snippet of code added to an existing file.
Is it possible to do a partial update of this kind?
So, for example, I'd reference the existing file, which contains the IoC mappings, and a code region to append some code to.
No, ReSharper's live templates don't work like this - they generate code rather than modify it. If the whole file is generated, delete it and regenerate it.

How to use the original filename in a multi file template in resharper?

I have a multi file template in resharper and I can use $NAME$ macro to get the name of the original file to use to name the other files in the template. But I also want to use the $NAME$ of the original file in the content of the other file template.
Is this possible? I can't see a macro which seems suitable for the internal variables as onlt the Current File Name seems available.
Anyone know if this is possible or how I might workaround this?
As a workaround, you may create a parameter $FILENAME$ (macro "Current file name without extension") in the first file e.g. in the comments, like:
class Foo
{
//$FILENAME$
}
Then you may call this parameter in other files of the multifile template - this parameter will contain the name of the first file since the first file will be generated before other ones.
Unfortunately, there isn't a macro that will give you this. I've added a feature request that you can vote on and track (and more specific detail as to what your requirements are would be useful) - http://youtrack.jetbrains.com/issue/RSRP-415055
It is possible to write your own macros as part of a plugin, but there isn't a sure-fire way of getting the name of the first document in the created file set. The IHotspotSessionContext instance that is passed to the macro via IHotspotSession.Context property includes an enumerable of IDocument, from which you can get IDocument.Moniker, which will be the full path for file based documents. However, there's no guarantee of the order of the enumerable - it's backed by a hashset. You might be able to rely on implementation details (small set, no removes) to be able to use the first document as the original, but there is really no guarantee of this.