Dynamically add files for localization C# - c#

I need a help in localizing my Wpf .net 3.5 application. I am going to give only the exe file to my cient with which he will create setup for his users. My client wants to add files for localization by himself.
The general method of localization is creating .resx files for each languages and set the language in CurrentUICulture. but since, he cant use resgen tool to create resource files, I want to know the best method to let him add files in txt,excel,xml format to make the localization.
PS: reading through stream and assigning the key is the only way?

If you cannot use the resgen Tool a simple textfile wit Key-Value Pairs would be the simplest way. And if you just staying with simple strings you should use just this. Name them after the default convention with the language in the name to identify to correct file to load and write yourself a custom resourceManager that load this files at startup or on demand into a Dictionary to access them.
Edit:
I go a bit more in details about this...
I would create a resource manager that
has static accessors for all used
resourcefiles where just the fallback
resource is static typed. I guess your
language is defined at startup so just
load the fallback and the defined
language files each into a dictionary
and both dictionaries in a static
typed for this resource type. Use a
GetString("name", Culture) to load a
string. You can do the lookups for the
culture in the resource dictionary and
if one key is missing do a fallback to
the static one.
Edit II:
You can ZIP the text-files using one
of the .Net libraries out there to
prevent anyone from open and change
the content. For additional security
you can also add a password to the
zip. So you end up with smaller files
plus the ability to modify the files
with any ZIP program. For sure you
should also change the file
extension.
Hope this helps

Related

Can you add a language without forcing a rebuild?

My app has been designed to be able to run on two different languages, english and czech. In order to accomplish this, I've created 2 resource files:
If an end-user would like to add another language, for example GlobalStrings.fr-FR.resx, is it possible to allow for this functionality without rebuilding the application?
If we look at the properties of these resource files:
I'm not understanding what embedded resource means. Does this mean that in order for the app to consume this file, the application must be rebuilt?
How do we create a resource file, that is open to be extended/changed by the end user, without having to rebuild the entire application
?
Regular .Net resources are compiled into assembly with particular name and loaded by matching that name. So if "end-user" is ok to translate strings in resx file and compile resources into assembly with particular name (like "MyResources.cs-cz.dll") you can do that with default .Net behavior without recompiling main code. See MSDN:Packing and Deploying resources and related links for more information.
Note that you don't need Visual Studio for it and can use csc command line compiler to embed resources on user's machine - so if your really want you can provide simple script that compiles corresponding resx locally. Note that editing XML (resx) as text is generally not possible by regular person due to required encoding of some characters - consider technical level of your "end-users" before going that route. Plain text version of source for resource may work in more cases.
Usually this is not the case - if end-user localization is requirement you would create some sort of custom resource string management by loading strings from plain text files or database that users can update locally.

How to fetch resource value from .resx file stored in my own custom folder location?

I want to get a resource value from my .resx file that is NOT stored in the local resource folder.
How can I do this?
I am currently using the strongly typed file that is generated, but in this case I can't use the strongly typed as I am building the key (which is a string) in a loop dynamically.
GetLocalResourceObject doesn't seem to work as it only seems to look for the resource file in a specific location.
You can pass a path to when using GetLocalResourceObject. Bare in mind you may hit problems if the external source is not get compiled.
HttpContext.GetLocalResourceObject("~/Views/SomeView/Index.cshtml", "Prop");
This expects a resx file at the following location.
~/Views/SomeView/App_LocalResources/Index.cshtml.resx
You can also precompile resx resources and create a custom provider to locate them.
You can extend the ResourceManager like the StringsResourceManager used in the article .NET String Resources.

loading resource file in class library and know which resource file to use

I have a class library and was to add a resource file to it to support both English and Spanish.
Any tips as how I can do this? The language will be dictated by the user visiting the site. Would like to have to only load each resource file once and cache or set in static variable and avoid any overheads.
It should be already handled by .net engine, and it would choose to use the one according to currentUICulture or currentCulture. BTW, the resource is supposed to be compiled into DLL so it is not possible to "Choose" since the file is not existed in the final compilation.
Since you mentioned a user visiting your site, I'm assuming you're using ASP.NET. As such, you should read these MSDN articles for information about localization of resources in your ASP.NET application. If you're simply developing an offline application (without ASP.NET), read these articles. In fact, it wouldn't hurt to read all of them.

how to store settings for deployable c# application?

I want to store settings for my C# application, such that default setttings can be easily shipped with my binaries and the end-user can change them using a simple text editor(or in some other simple way).
I seem to face several alternatives : a .config file, .settings file or a .resx file. What are the pros and cons of these?
Edit1: End-users are computer professionals mainly, so editing these files should not be much of a problem.
Edit2: The settings are something like connection strings, and some other parameters (mostly one-time stuff). Building some kind of GUI/API for changing them is not really an option. Also my application will not edit any of these values, so persistence through code is not required.
Yes, Project + Properties, Settings tab was designed to do this. Add your settings here, change the Scope to Application. That generates a app.exe.config file in your build direcctory, deploy it along with your EXE. Use Properties.Settings.Default.SettingName in your code to obtain the setting value. Your user will normally need admin privileges to edit the .exe.config file on the target machine to change the setting value.
The small print: settings do not work well for DLL assemblies, you have to merge the .config files by hand. When using the debugger, settings are retrieved from the app.vshost.exe.config file.
The .settings file is a helper file used by the IDE, ignore it. .Resx files store resources, they get compiled and embedded in a binary form in an assembly. They are not editable by the user.
I think you can have two ways of doing this.
For regular users, you can make a custom GUI that will make it simple for them to use.
For advanced users, they can edit the configurations using a text editor if it's stored in a text file (ini file, config file, etc..) or you can make an API.
The .settings file is typically used for user-specific preferences and configuration information (whereas the .config file is used for global settings for the application or anything that modifies the .Net runtime. Simply putting parameters in a .config file can alter the behavior of your application even without you writing a single line of code for it).
Check out the Settings article on MSDN for more: http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
Since the file will be modified by the users, I think using app.config is not a good idea. What if they break the file structure? Or set an invalid value? Probably your application will crash directly.
One of the solutions would be to use a custom XML file. You will then validate it when your application starts. XSD will probably be the more elegant way to do it, but you can also parse it directly and validate it in code. If the file is invalid, instead of crashing, you will try to solve the problem, and if impossible, display a pretty error to the user, explaining that there is an error in XML at line n, position n, which is [error description here].
If the end user is really going to be editing them, I'm not sure I would want them editing my app.config file.
You have another couple alternatives that you haven't included. You could use an old-school .INI file that is simpler for an end user to understand. You could also use the registry. I would recommend the INI file, unless your users are very savvy, in which case use the .config file.
The answer depends on the deployment method. For instance, if you are using ClickOnce and offer updates, you might encouter problems using Application Settings.
I believe the best way to go is to create a GUI, something that is most certainly suitable for novice users. Given that you already excluded that option, use John's suggestion (ini files).

Using legacy resource dll's in C#

I'm using a legacy, unmanaged, resource only c++ dll in a new c# app. I've managed to load resources from the dll using hardcoded resource numbers.
Is there any way to do this using the resource id's in C#?
The old C++ code is still being maintained, which means the resource ids/values may change. This may make the resource numbers in the c# code incorrect/invalid.
The best I can think of is to cut/paste the resource.h file into a c# class, then do some manual search/replace to format it into a c# class. This will have to be done after each change to the resource file. Is there a better way?
I'm guessing that you're not asking how to load those resources, but how to keep the ID numbers in synch?
If that's the case, I'd suggest having 2 header files where the resource IDs are defined. Use resource.h just like you do now for things you don't mind the Wizards modifying, but introduce a second header file (eg appresource.h) that you edit manually. That way you can ensure that the IDs in that file will never change. Then the only time you have to change your C# app is when you add new resources you want to use.
If the resource dll isn't maintained by you, I would consider writing a script/tool to do what you said, but automated (to the extent possible).

Categories

Resources