How to access the localized language resource in another project for WPF?

I have created two projects:

(1) MyProject.Language
      This is a central repository for localized language:
                    MainWindow.resx,
                    MainWindow.en-US.resx,
                    MainWindow.fr-FR.resx,
                    MainWindow.ja-JP.resx.
      Namespace: MyProject.Language.Resources
      Location:  MyProject.Language\Resources\MainWiindow.resx
      BuildAction: Resource  (I have also tried "Embeded Resource"
      Access Modifier= Public

(2) MyProject.ViewModel
      This is a view model, where we access the language resource programmically.

string key = "TestLabel";
ResourceManager rm = new ResourceManager("MyProject.Languages.Resources.MainWindow", Assembly.GetExecutingAssembly());
string sValue = rm.Get(key);

The build is successful. I can see the resource dll in the respective culture folders:
     en-US, fr-FR and ja-JP

However, when I run the application, it threw the exception as follows:

Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "MyProject.Languages.Resources.MainWindow.resources" was correctly embedded or linked into assembly "MyProject.ViewModel" at compile time, or that all the satellite assemblies required are loadable and fully signed.

I have searched the web for possible answers. One of the suggestion is to compile the resource file to satellite assembly and link it to the applicaiton assembly. However, you need to run it by command line. That is not acceptable. So here are my questions:

(1) What is the correct approach to resolve this?
(2) What are the correct steps to handle it in VS2012? No command line.

Your answer will be highly appreciated. I wish I could give more than 500. If you request, I can split the question into few small questions for higher points.

Thank you very much indeed, and in advance.
LVL 1
chuang4630Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

I tried your requirement by working on demo solution. Let me explain how I did ?

PROJECT - MyProject.Language

(1) Created project with namespace MyProject.Langauge.Resources.

(2)  Added resources file in it as shown in snapshot attached. I had not used all resource files for reducing efforts.

Solution View
(3) Now tried to access those resource file in this project's LangWindow.xaml.cs as below:

const string key = "TestLabel";

            var resManager = new ResourceManager("MyProject.Language.MainWindow", Assembly.GetExecutingAssembly());

            var sValue = resManager.GetString(key);
            
            MessageBox.Show(this, sValue); // This is working in this project

Open in new window


PROJECT - MyProject.ViewModel

(1) Created project with namespace MyProject.ViewModel

(2) Added reference of the first project. You can access the demo project from link. The demo had been developed by using VS 2010 IDE and I believe it will work in VS 2012 too.

(3) Now access the value from resource file in another project as:

using System.Resources;
using System.Windows;
using MyProject.Language.Resources; // This is the main point of solution

namespace MyProject.ViewModel
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            var resManager = new ResourceManager("MyProject.Language.Resources.MainWindow", typeof(LangWindow).Assembly);

            var valueFromResourceInOtherProject = resManager.GetString("TestLabel");

            MessageBox.Show(this, valueFromResourceInOtherProject);
        }
    }
}

Open in new window


Let me know if you are unable to understand explanation or project shared with you.
0
chuang4630Author Commented:
var resManager = new ResourceManager("MyProject.Language.Resources.MainWindow", typeof(LangWindow).Assembly);

"typeof(LangWindow).Assembly" is new to me. Since I am not allowed to add any xaml to the MyProject.Language, it seems like a new way is needed.
0
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
typeof(LangWindow).Assembly is new to me. Since I am not allowed to add any xaml to the MyProject.Language, it seems like a new way is needed.

Well! I researched for that part thinking about different aspects and finally reached on accepted solution.

PROJECT - MyProject.Language

(1) See the snapshot attached:

Solution Explorer View
As per the solution, you would need to modify the class and constructor (as shown in below snapshot) modifier to public from internal.

File showing modification done
(2) Clean project and rebuild it.

PROJECT - MyProject.ViewModel

(1) Add reference of above project in this project.

(2) Code modified little bit as: (in file MainWindow.xaml.cs)

using System.Resources;
using System.Windows;

namespace MyProject.ViewModel
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            // Language.Resources.MainWindow is class name from resource file's MainWindow.Designer.cs
            var resManager = new ResourceManager("MyProject.Language.Resources.MainWindow", typeof(Language.Resources.MainWindow).Assembly);

            var valueFromResourceInOtherProject = resManager.GetString("TestLabel");

            MessageBox.Show(this, valueFromResourceInOtherProject);
        }
    }
}

Open in new window


And now run the application. All will work fine.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chuang4630Author Commented:
The key is using

typeof(Language.Resources.MainWindow).Assembly.

It works.

Thanks a lot.
0
chuang4630Author Commented:
Brilliant solution. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.