Access process i18n property files before compilation - grails

I have a following situation: in some of my i18n property files there are properties containing a special word:
prop.example=specialword just for example
prop.test=just for test specialword
I want to have a possibility of having a property somewhere in my Config.groovy that would contain a specific value for this specialword so that if I specify:
specialword=Value of special word
in a Config.groovy then I want my i18n properties to be resolved like:
prop.example=Value of special word just for example
prop.test=just for test Value of special word
for that purpose, when building the project, I want to access property files in order to look for occurences of specialword and to replace them with value of specialwordvalue from Config.groovy.
Is that possible somehow? Perhaps, someone faced similar situation? I would really appreciate any help.
Thanks, Cheers

Instead of trying to change the way the properties are compiled, you would be better off passing the special value as an argument to your message code (as discussed in the comments to your question).
For instance:
<g:message code="my.key.code" args="[someVariableWithAValueFromConfig]" />
If your message code doesn't use the argument it will simply be ignored. This seems like the best approach to the problem you are trying to solve.

Related

Use plugin config as paramater in services.yml

The documentation only shows that you can pass the SystemConfigService as a parameter to another service.
Is there also the possibility to pass directly the value from the plugin configuration?
Background of the question: I would like to initialize directly an instance of an external component. But this expects fixed arguments as strings. Alternatively, one would otherwise have to write some kind of factory.
Hm, it's possible to do. I have not done this directly myself, but 99% confident that it will work. You may need to play around with it a little.
In the services.xml you can use symfony expressions.
<argument type="expression">service('Shopware\Core\System\SystemConfig\SystemConfigService').get('SwagBasicExample.config.example')</argument>
You may need to find the alias name for the Shopware\Core\System\SystemConfig\SystemConfigService class instead. Also check the Symfony documentation, you can do a lot more with this!
I myself passed an array as an argument, but used a custom class as a config getter like so:
<argument type="expression">
{
"shop_is_active": service('config_bridge').get('isActive'),
"customer_number": service('config_bridge').get('customerNumber'),
"shop_number": service('config_bridge').get('shopNumber'),
"apikey": service('config_bridge').get('apiKey')
}
</argument>
Not strictly necessary as Shopware already requires it, but always a good practice to add the requirement to your plugin composer file:
"require": {
...,
"symfony/expression-language": "~5.3.0|~5.4.0"
},
As of today it's not possible to inject specific system_config values in services.

Easiest way to render an Integer without thousands separator when using Fields plugin 2.0.2

I'm building a very basic app using Grails 3.0.2.
I have a domain class called Unit which contains, among others, a field called season, whose type is Integer and represents a year.
I have used the command generate-views to generate the scaffolded views.
Once running the application, when an existing instance is shown, the season is displayed using "," as thousands separator, and I want to remove it.
What is the easiest way to override (only) the format of the season?
For testing purposes, I have modified the show.gsp of the Unit class in the following manner:
<f:with bean="unit">
<f:display />
<f:display property="season" />
</f:with>
The <f:display property="season" /> displays simply "1,975", but ignores the label.
I've tried to understand the documentation of the Fields plugin, but I do not achieve what I want so it's obvious that I do not understand it.
I have added _displayWidget.gsp under views/_fields/unit/season (I have also tried under views/unit/season), but the outcome is exactly the same than before, so I assume the plugin is not taking them into account.
<g:formatNumber groupingUsed="false" number="${value}" />
I am not familiar with the _displayWidget.gsp convention, but a simpler approach might be to override the display of the unit.season property by adding a _display.gsp under views/_fields/unit/season containing just the following:
${value}
Diego, you can format any given number using the taglib formatNumber:
https://grails.github.io/grails-doc/latest/ref/Tags/formatNumber.html
Use the param 'format' and check the DecimalFormat patterns to find the one that suits fine for you.
Hope it helps!
I was able to get this working in Grails 3.1.14 by creating views/_fields/myDomainClass/myFieldName/_displayWrapper.gsp and containing a single line of ${value}

Add many attributes to my xdatamodel (iOS Core Data)

I've got a list of 2000+, almost the same, 'NSString' attributes I need to "import" in my .xcdatamodel-file.
If I could open the xcdatamodel-file using an XML-like-text-editor, like a .plist-file, I can add all of my attributes, but I can't find a way how.
I managed to programmaticly create the attributes (using this tutorial), but then I can't set or fetch the attribute's data.
The list should look like:
["str_1_1"],["str_1_2"],["str_1_3"],...,["str_49_4"],["str_49_5"],...
Is there a way to programmaticly add attributes / set and fetch data from attributes?
OR
Is there a way to staticly add all possible attributes without clicking the +button over 2000 times?
You can just open the model file in any text editor. If you have the file compatibility for the file set to Xcode 4 or higher, it's even easy to edit.
The model Foo.xcdatamodel is actually a directory. Inside that is a file named contents, which is nicely formatted, easily readable XML. Edit that. A string attribute will look something like:
<attribute name="stringAttribute" optional="YES" attributeType="String" syncable="YES"/>
Add one or two string attributes in Xcode and then duplicate/edit them as needed.
A couple of notes:
Obviously, it's your job to get the syntax right. This is not documented but also not hard to figure out. If you end up with a broken model file that won't compile, you got something wrong.
It's probably a good idea to quit Xcode first. It might not freak out if you edit the model file while it's running, but you never know.
Having 2000+ string attributes is frankly terrifying and suggests an extremely bad data model. Before editing the model and adding all of these, please carefully consider whether there's a less extreme solution.

Overriding JSF's default I18N handling

Like the asker of the question here
Variable substitution JSF Resource Bundle property file
I'm slightly aghast at the inability to reference the value of other property keys in the message bundle.
Although I see how easy to write my own rubbish handler[0] that can do what I want in a custom component, that would leave expressions in templates calling the message bundle still using the default JSF implementation.
Is it possible to override the default JSF handling of the message bundle?
[0] Or better, to use code referenced in one of the answers to the above question
https://code.google.com/p/reflectiveresourcebundle/
You can provide the fully qualified name of a concrete ResourceBundle implementation as "base name" instead of alone the path and filename of the properties files.
E.g.
public class YourCustomResourceBundle extends ResourceBundle {
// ...
}
which can be registered as follows
<application>
<resource-bundle>
<base-name>com.example.YourCustomResourceBundle</base-name>
<var>text</var>
</resource-bundle>
</application>
or declared on a per-view/template basis as follows
<f:loadBundle baseName="com.example.YourCustomResourceBundle" var="text" />
Here are several related questions/answers which contain some concrete code which you could use as a kickoff example:
How to remove the surrounding ??? when message is not found in bundle
internationalization in JSF with ResourceBundle entries which are loaded from database
i18n with UTF-8 encoded properties files in JSF 2.0 appliaction
Everything is possible for those who try. The question is not whether it is is possible but should you do it. And the answer to that question is: probably not.
Referencing other message in a message bundle means you want to build a compound message. So you can re-use part of the message many times just to save small fraction of the disk space or small fraction of development time.
If that is the case, I have a message for you. What you plan to do is called a concatenation and it is the second most common I18n defect. And its implications are as bad as those of hardcoded strings.
Why? Because target languages do not follow the English grammar rules. First, it is common need to re-order the sentence while translating. This might be easy to fix by using (numbered or named) placeholders. On the other hand though, the translation might differ depending on the context. That is, it might need to be translated in totally other way, or simply the word endings might need to be different depending on a grammar case, mood or gender.
My advice is, do not use such shortcuts, it will create more problems than it fixes.
Now you should know why "those stupid Romans" didn't implement it like this: it is against I18n best practices.

Problems with Netbeans re: Rails erb/rhtml intellisense?

I've been using Netbeans for Rails and like it a lot, considering how little I paid for it. But something that bothers me is that when I'm editing an RHTML or ERB file, it doesn't do the code autocomplete - or at least not reliably. Sometimes it shows the appropriate variables and methods that are available on an object after you type the dot operator. Sometimes it ignores the instance variables. Is there a solution for this? (Please don't say RadRails).
Oh and one more thing in case anyone has solved this: considering how often I have to type <% when I'm in a Rails template, I wish there was some hotkey for autotyping the tag . . . ? I always have to stop and look down at my keyboard to find the < and % keys before I can type the tag so it's not as trivial as it might sound.
I believe you're looking for something like this:
http://ruby.netbeans.org/codetemplates-rhtml.html
Type in one of the triggers, then hit the tab key to expand it to the code as given.
Also, you might want to explore using HAML. It's much easier on the hands.

Resources