Flutter intl access string dependent on variables - flutter

I use intl in my Flutter app.
I need a string resource that depends on two different user selections.
The name of the resource looks like resoucename_<selection1>_<selection2>.
The user selections are taken from a dropdown field.
To prevent checking a lot of cases, on native Android I got the string using Java Reflection:
String resName = "resource_" + userSelection1 + "_" + userSelection2;
Field idField = R.string.class.getDeclaredField(resName);
String text = getActivity().getResources().getText(elementart_resource_id)
Is there a way to implement this in dart or is there even a more elegant solution to getting a string dependent on the user's selection?

Related

passing dynamic strings to AppLocalizations.of(context)! in Flutter

right now Im working in App using Flutter and I have 4 different languages, I use json (arb files) for localization (translation)
I need to pass different string values which app fetch them using API's as shown in example below
AppLocalizations.of(context)!snapshot.data![index].state_pickup[0]
however "AppLocalizations.of(context)!" doesn't fetch the return data from snapshot.data![index].state_pickup[0] and instead it looks for it as string and tries to search for match string name in AppLocalization.dart class?
Any idea how I can pass dynamic string arguments to AppLocalizations.of(context)!?
What you are trying to do, invoking a method by its name at runtime, is called reflection, and this is not supported by Flutter natively (though there are packages that try to emulate this, but I have no experience with them).
What will work for you, even though it might be tedious, is manually mapping your value from the API to the corresponding method from AppLocalizations.of(context).
String localizedString = getLocalizedString(snapshot.data![index].state_pickup[0], context);
String getLocalizedString(String key, BuildContext context) {
switch (key) {
case "possible_api_value_1":
return AppLocalizations.of(context)!.possibleApiValue1;
case "possible_api_value_2":
return AppLocalizations.of(context)!.possibleApiValue2;
...
}

How to use Strings to access class members in Flutter

I want to set use Strings from an API as Fontawesome icons. I have added the font_awesome_flutter plugin.
I need to store the icon name in a variable and then create an object from it. I would imagine it to like this:
String iconfromApi = 'suitcase';
Icon(FontAwesomeIcons.iconfromApi);
As described here, you would need have access to the dart:mirrors pacakage, which is not avaible in Flutter.
A solution that would work in Flutter is creating a helper method. This means that you will have to code in cases for all icon names you want to use. If you do not want to write all of that by hand, you can take a look at a package like reflectable as mentioned in the GitHub comment or potentially source_gen or build_runner, however, I am not sure if the latter two are well suited.
Anyways, what you could also write by hand is a helper function like this:
IconData fontAwesomeIconFromString(String name) {
switch (name) {
case 'suitecase':
return FontAwesomeIcons.suitecase;
case 'gamepad':
return FontAwesomeIcons.gamepad;
// ...
}
}
In your code, you can now use it like this:
String iconfromApi = 'suitcase';
Icon(fontAwesomeIconFromString(iconFromApi));

Bukkit - Why is it displaying null (using a config file)

So, I am making a custom plugin for my server, and one of my features requires me to set an integer in a gui that shows how many 'CommonPackages' a user has. The issue that I am having is that when I am getting the String from my config (My config uses a custom file creation/management class that was given to me by a friend) it is saying that it is null in the gui, I do not get any errors in the console, please may someone help me? The item in the gui and the code for setting the item in the gui.
Item in the gui
gui creation code:
public static Inventory WhiteBackpack(Player player) {
UUID uuid = player.getUniqueId();
Inventory inv = Bukkit.createInventory(null, 27, (inventoryname));
ItemStack common = new ItemStack(Material.INK_SACK);
common.setDurability((byte) 8);
ItemMeta commonMeta = common.getItemMeta();
commonMeta.setDisplayName(Utils.chat("&fCommon Packages &8ยป &f&l" + Main.pl.getFileControl().getConfig().getString("players." + uuid + ".Packages.Common"))); //How I access my custom configs.
common.setItemMeta(commonMeta);
inv.setItem(10, common);
return inv;
}
Without the code of your method to get the config I can only say that the string in the actual file is not present.
As the Bukkit documentation states:
If the String does not exist and no default value was specified, this will return null.
So either the key just does not exist in the file or you pointed to the wrong file. The configuration should be well formated, too. (no tabs, only spaces)

How to validate custom ("%CUSTOM%) RulesStringValue in Fiddler?

I have following piece of code in my fiddlerscript:
RulesString("Redirect Foo", true)
RulesStringValue(0, "to the latest version", "latest")
RulesStringValue(1, "to a particular version... (e.g. 1.2.3)", "%CUSTOM%")
public static var foo_redirect: String = null;
It renders as a submenu and clicking Redirect Foo to a particular version... opens a prompt box where you can type something.
Now, I want to validate that the user typed something following a regexp, and not a totally random string, and display a FiddlerObject.alert if he put something non-conformant.
How can I do the validation just after the user inputs the value, but before there's any redirection happening? (I don't want to defer it to OnBeforeRequest since it may display dozens of alerts there, one per each session - Foo is a folder with many JS files).
Fiddler does not offer a mechanism to validate the values of the %CUSTOM% token at the point of entry. If you want to do that, don't use RulesString, instead use FiddlerScript or an extension to add a menu of your own devising.

SSRS CreateSubscription - setting Locale

We have a custom built application, based on silverlight, which manages our report subscriptions. The problem is that whenever you add a new subscription, it sets the Locale value in the Subscriptions table to 'en-US'.
When you create subscriptions directly in Report Manager, the value in the Locale field is determined by your browser language settings (this is exactly what we want to achieve).
We can't find a way to set the Locale field before we call the CreateSubscription method as it doesn't seem to accept the Locale parameter and it defaults to en-US (which I believe is a server setting).
Do you know of any way to set Locale when creating subscriptions in SSRS?
As far as I can see there is no way to do this; I've been using the service via C#.
I decided upon a solution of obtaining the language/locale of the report via the service, and then making the changes directly in the ReportServer database, as an UPDATE statement on the SubscriptionID.
To get the language/locale of the report I use something like:
private static void Main()
{
ReportingService2010 service = new ReportingService2010();
service.Url = "URL of service";
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
string reportItemPath = "Path of report";
string language = GetReportPropertyValue(service, reportItemPath, "Language", "en-GB");
// Create the subscription, then update the database using the returned SubscriptionID.
}
private static string GetReportPropertyValue(ReportingService2010 service, string itemPath, string propertyName, string defaultValue)
{
Property[] properties = service.GetProperties(itemPath, null);
if (properties.Any(p => p.Name == propertyName))
return properties.First(p => p.Name == propertyName).Value;
else
return defaultValue;
}
I haven't tried it via the SOAP API yet, but shouldn't it be possible to set the Accept-Language header?
I'm doing this when rendering reports in different languages. The language of each report is set to =User!Language.
When using WCF to access the reporting service, you can do (example as VB.NET)
Using oReportingService As New ReportingService2010SoapClient
oReportingService.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation
Using oScope As OperationContextScope = New OperationContextScope(oReportingService.InnerChannel)
If i_oSubscription.Language IsNot Nothing Then
' Accept-Language
Dim oHttpRequestProperty As New HttpRequestMessageProperty
oHttpRequestProperty.Headers.Add(HttpRequestHeader.AcceptLanguage, i_oSubscription.Language)
OperationContext.Current.OutgoingMessageProperties(HttpRequestMessageProperty.Name) = oHttpRequestProperty
End If
oReportingService.CreateSubscription(New ReportingService2010.TrustedUserHeader(), i_oSubscription.Path, oExtensionSettings, i_oSubscription.Description, "TimedSubscription", sMatchData, lstParameters.ToArray(), sSubscriptionId)
Return sSubscriptionId
End Using
End Using
Edit: This is the way I'm doing it myself and it seems to work :)
i_oSubscription is just a simple container with properties like Description
Note: this seems to work for the most part. However, the "Days" field in a MonthlyRecurrence is to be formatted locale-dependent (see: https://stackoverflow.com/questions/16011008/ssrs-monthlyrecurrence-formatting-for-different-languages)