Using ResourceLoader.GetString method to retrieve resources with dots in the key - c#

I'm using ResourceLoader.GetString to get string resources from my .resw file. I'm able to retrieve resources without a dot in the key, but ones with a dot come back as an empty string. For example:
var rl = new Windows.ApplicationModel.Resources.ResourceLoader();
rl.GetString("HelpText"); // gets the string "Help"
rl.GetString("Forget.Text"); // gets "", even though it's defined in resw file as "Forgotten"
I've tried replacing the dot with various other characters:
rl.GetString("Forget_Text");
rl.GetString("Forget:Text");
rl.GetString("Forget-Text");
No luck. All the examples on MSDN skilfully avoid mentioning this little issue, so I'm a bit stumped. Can anyone help?

It is actually accessed via a forward-slash:
rl.GetString("Forget/Text");

How would you deal with a final dot? For example:
ResourceManager.GetString("My string.")
The forward slash works fine for dots embedded within the string, but not a final dot. Eg, this results in a compile error (error : PRI175: 0x80070057 - Processing Resources failed with error: The parameter is incorrect.):
ResourceManager.GetString("My string/")
The only way I've found around that is to substitute something for the dot before putting it into the resw file. For example, this looks weird but it works:
ResourceManager.GetString("My string0x2e")
Thank you.

Related

Graph filter for a SharePoint list using 'displayName eq' quoting all characters

I'm using the C# Microsoft.Graph API to talk to Microsoft Graph to find out if a specific list exists in a SharePoint Online site. I want this to be robust against all problematic characters in the list name, and use of GraphServiceClient.Sites[siteId].Lists[listName] fails if the listName has a colon in it, so (after advice from StackOverflow) I switched to using a filter.
I've tried all sorts of encoding of problematic characters and can't get it to consistently work - it either returns an error The expression ... is not valid or says that the list does not exist when it does.
Here's one variation of my code:
var name="lis !\"£$%^&*()_+-=[]{};':##~/.,?><`¬";
var encodedName=System.Web.HttpUtility.UrlEncode(name);
existingListRequest.QueryOptions.Add(new QueryOption("filter", $"displayName eq '{encodedName}'"));
The above fails but the error quotes the original 'name' without any encoding, so expect the encoding is being decoded before it gets to the filter. I've tried without using UrlEncode, and using it twice, and using it then replacing all the '%' characters in the result with '%25' but none will successfully find the list.
What's the definitive way to encode problem characters in a Graph filter?
You should try to replace colon with x003a in list name. Hope this will sort your issue.
string listName = ListName.Replace(':', 'x003a');
After a lot of trial and error I worked out the right solution:
name.Replace("'", "''").Replace("#", "%23").Replace("&", "%26").Replace("+", "%2b");
All those steps were included in previous trials but I think perhaps something else I then encoded made it fail to find anything.

how to make string returned by ResourceManager.GetString not verbatim

Okay, I have a string
string textToShow = "this\nrocks"
which when put in label in winforms window will then show
this
rocks
Which is the result I'd like to get. Now, instead of setting the textToShow in the code, I set it in the resource file. When I tried to get the value from resource file using
Properties.Resources.ResourceManager.GetString("textToShow");
the whole string instead will be treated as verbatim, showing
this\nrocks
when put in a label in a winforms window. This is not the result i'm looking for. What's the best way to store strings with special characters in resource file then? I can do string replace for every special characters, like
string.Replace(#"\n", "\n");
but then I need to replace every special characters whenever I call method ResourceManager.GetString, which I think is not the most elegant solution. If there is some ways to make string returned from method ResourceManager.GetString not verbatim, please do tell me.
Thanks
This was already answered here: StackOverflow: How to deal with newline
Basically you have two useful options:
Use shift + enter in the resource manager text editer to add a new line.
Or use String.Format() to replace {0} with \n on read.
The .Net 4.5 framework has the unescape functionality as shown here:
using System.Text.RegularExpressions;
Regex.Unescape(Properties.Resources.ResourceManager.GetString("textToShow"));
solves your issue. Now you can use \n and \u in the resource files.
On the resource editor type "this<shift+enter>rocks" as the resource value.

How to URL encode periods?

I need to URL encode some periods since I have to pass some document path along and it is like this
http://example.com/test.aspx?document=test.docx
So test.docx is causing me an error of an illegal character. So I need to change it to
. --> %2E
I tried to use Server.UrlEncode
string b = Server.UrlEncode("http://example.com/test.aspx?document=test.docx");
but I get
"http%3a%2f%2fexample.com%2ftest.aspx%3fdocument%3dtest.docx"
So do I have to use like a string replace and do it manually and replace all periods with that code?
This is a really old question, but I ran into this searching for a similar problem. I stuck a "/" onto the end of my url's with periods in them and it got around the problem.
The period there isn't he problem (given that %2E doesn't solve the problem). A period is a perfectly valid URL character whatever the problem is it's not the period. Check the stack trace of the error being throw or post the complete error details.
And you shouldn't be URL encoding the entire path. Only the query string parameter value.
string b = "http://example.com/test.aspx?document=" + Server.UrlEncode("test.docx");
Are you still getting the error if you try it that way?
I wouldn't touch SharePoint with a ten foot pole. However, escaping the period wouldn't necessarily stop SharePoint from doing it's shenanigans. But I guess you should at least try it.
Server.UrlEncode("test.docx").Replace(".", "%2E");

Using a regex to parse $Resource strings

Let´s say I´ve got the following xml string:
<Item Name=\"$Resources:myresource,somestring;\"</Item>
Now I want to pick out all the occurrences of $Resource tags in it (there can be any number of them).
I actually wan´t to replace these resx strings with their corresponding values but that code I´ve already got.
The "problem" is that the code that is supposed to get these values for me requires that I pass in the name of the resource file (e.g. the part after $Resource. In this example it would be myresource) and the actual resource object (e.g. somestring).
Now I have been playing around with regular expression to accomplish this for me and what I really want is to put these two values into two different groups (because sometimes the "resource file" will be a default one, e.g. it can also look like $Resource:somestring).
Anyone got an idea of how to do that or can I perhaps use something in .NET that will do this for me, e.g. give me the classname (I think it´s the appropriate name for a resource file) in one property and the resource object in another one??
The RegEx is actually pretty simple:
(\$Resources\:(?<name>[^,;]+),(?<content>[^;]+);)|(\$Resources\:(?<content>[^;]+);)
For the following string it would return 3 results, where two results have the groups name and content and one result only has the group content.
Sample data:
<Item Name=\"$Resources:somedefaultstring;$Resources:myresource,somestring;$Resources:myresource2,somestring2;$Resources:somedefaultstring;\"</Item>
UPDATE:
Fixed according to the comment.

C#: Issues using dictionary with languages other than english

Ok, so i'm basically trying to load the contents of a .txt file that contains 1 word per line into a dictionary.
I had no problems doing so when the words in that file were in english, but changing the file to a language with accents, i started having problems.
Had to change the encoding while creating the stream reader, also the culture in the ToLower method while adding the word to the dictionary.
Basically i now have something similar to this:
if (!dict.ContainsKey(word.ToLower(culture)))
dict.Add(word.ToLower(culture), true);
The problem is that words like "esta" and "está" are being considered the same. So, is there any way to set the ContainsKey method to a specific language or do we need to implement something in the lines of a comparable? Either way i'm kinda new to c# so i would apreciate an example please.
Another issue submerge with the new file... after like a hundred words it stops adding the rest of the file, leaving a word incomplete... but i cant see any special chars in that word to end the execution of the method, any ideas about this problem?
Many thanks.
EDIT:
1st Problem solved using Jon Skeet sugestion.
In regards of the 2nd problem:
Ok, changed the file format to UTF8 and removed the encoding in the stream reader since it now recognizes the accents just right. Testing some stuff regarding the 2nd issue now.
2nd problem also solved, it was a bug on my part... the shame...
Thnks for the quick answers everyone, and especially Jon Skeet.
I assume you're trying to get case insensitivity for the dictionary. Instead of calling ToLower, use the constructor of Dictionary which takes an equality comparer - and use StringComparer.Create(culture, true) to construct a suitable comparer.
I don't know what your second problem is about - we'd need more detail to diagnose it, including the code you're using, ideally.
EDIT: UTF-7 is almost certainly not the correct encoding. Don't just guess at the encoding; find out what it's really meant to be. Where did this text file come from? What can you open it successfully in?
I suspect that at least some of your problems are due to using UTF-7.
The problem is with the enconding you are using when opening the file to read. Looks like you may be using ASCIIEncoding.
.NET handles strings internally as UTF-8, so this kind of issue would not happen internally.

Categories

Resources