Load all available satellite assemblies for run time language change WPF - c#

I try to get the list of available Culture information, add them to a combobox and change the language on run time. My implementation acts different on different environments. There are some PCs where it works as expected, all the satellite assemblies are recognized, but on other PCs the same application does not recognize the available languages, only the default one (English).
This is my code where I try to find the available resource:
public static IEnumerable<CultureInfo> GetAvailableCultures()
{
List<CultureInfo> result = new List<CultureInfo>();
ResourceManager rm = new ResourceManager(typeof(PresentationResources));
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo culture in cultures)
{
try
{
if (culture.Equals(CultureInfo.InvariantCulture))
{
continue;
}
ResourceSet rs = rm.GetResourceSet(culture, true, false);
if (rs != null)
{
result.Add(culture);
}
}
catch (CultureNotFoundException)
{
//NOP
}
}
return result;
}
Also the code from the xaml from where I try to change the language:
<ComboBox Name="LanguageComboBox"
SelectedIndex="{Binding SelectedLanguageIndex}"
ItemsSource="{Binding AvailableLanguages}"
DisplayMemberPath="NativeName"
SelectedItem="{Binding Source={x:Static lex:LocalizeDictionary.Instance},
Path=Culture}"
SelectionChanged="LanguageComboBox_OnSelectionChanged">
</ComboBox>
I don't really understand why the exactly same application on some environments works as expected and on other ones not.

Related

Check if winforms can be displayed on mono

I am making a console application that displays some data in windows form popups primarily for development or to configure it. However, when deployed it is to be ran in a command line only environment, which will not be able to display the forms. I would like to know if there is a way to detect whether or not it is possible to display these forms so I can display the data in a different format or print an explanation if the data cannot be displayed (one of the forms is an image preview).
Yes sir, i would like to give you two Methods, for the case "Mono":
This Checks, if your in Mono Runtime:
private Boolean IsMonoRuntime()
{
return (Type.GetType("Mono.Runtime") != null);
}
This gives you the Mono-Version (Linux Package) or String.Empty:
private String GetMonoRuntime()
{
Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
if (displayName == null)
return "Unix/Linux + Mono";
else
return "Unix/Linux + Mono " + displayName.Invoke(null, null);
}
return String.Empty;
}
I hope its whay ou needed.

Windows Phone- How to set LocalSettings first time?

In Desktop Application or Web Projects projects there were App.configs and Web.configs files to store settings. These settings were set in development time (or whenever later) but if this occures, it was ALWAYS once action.
In Windows Phone 8.1 XAML there isn't any App.config file, so developers are able to use Windows.Storage.ApplicationData.Current.LocalSettings. Nice.
How can I set these settings first time (this means on first application run ever, so I can later only read them and sometimes update existing values)? Of course I can set settings whenever I run application but this is time wasting. How do you set LocalSettings in you applications first time? I saw this solution Is there a "first run" flag in WP7 but I don't think so, that this is the only possibility.
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
// Create a simple setting
localSettings.Values["exampleSetting"] = "Hello Windows";
// Read data from a simple setting
Object value = localSettings.Values["exampleSetting"];
if (value == null)
{
// No data
}
else
{
// Access data in value
}
// Delete a simple setting
localSettings.Values.Remove("exampleSetting");
Msdn Reference
Persistance of Data
I have written code:
public void Initialize()
{
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (!localSettings.Values.ContainsKey(FirstRunSettingName))
{
localSettings.Values.Add(FirstRunSettingName, false);
}
localSettings.Values.Add(SettingNames.DataFilename, "todo.data.xml");
}
public bool IsFirstRun()
{
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (localSettings.Values.ContainsKey(FirstRunSettingName))
{
return (bool)localSettings.Values[FirstRunSettingName];
}
else
{
return true;
}
}
In the App.xaml.cs file:
public App()
{
this.InitializeComponent();
this.Suspending += this.OnSuspending;
var storageService = Container.Get<ISettingsService>();
if (storageService.IsFirstRun())
{
storageService.Initialize();
}
}
I'm not sure this is proper way to set settings first time, but it is some soultion.

Cultures available in C# WPF Application

I am working on localization and pulling all the available cultures based on the folders created in bin. the name of the folders are then converted into culturesInfo.
if (!bFoundInstalledCultures)
{
//determine which cultures are available to this application
Debug.WriteLine("Get Installed cultures:");
CultureInfo tCulture = new CultureInfo("");
foreach (string dir in Directory.GetDirectories(Application.StartupPath))
{
try
{
//see if this directory corresponds to a valid culture name
DirectoryInfo dirinfo = new DirectoryInfo(dir);
tCulture = CultureInfo.GetCultureInfo(dirinfo.Name);
//determine if a resources dll exists in this directory that matches the executable name
if (dirinfo.GetFiles(Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".resources.dll").Length > 0)
{
pSupportedCultures.Add(tCulture);
Debug.WriteLine(string.Format(" Found Culture: {0} [{1}]", tCulture.DisplayName, tCulture.Name));
}
}
catch(ArgumentException e) //ignore exceptions generated for any unrelated directories in the bin folder
{
}
}
bFoundInstalledCultures = true;
Above code is part of one Runtime localization example on Codeproject.com
are there are any methods thru which we can get this Info and can we use reflection.
Thanks in advance
You can use CultureInfo.GetCultures to get all installed cultures:
//create an array of CultureInfo to hold all the cultures found, these include the users local cluture, and all the
//cultures installed with the .Net Framework
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
//loop through all the cultures found
foreach (CultureInfo culture in cultures)
{
// ...
}

Is Microsoft.Web.Administration.dll Redistributable?

We are accessing default port number using ServerManager class from IIS in customer machines. But the Console didn’t run some of the machines and show Microsoft.Web.Administration.dll is required. Is this assembly is Redistributable? Or Is there any other way to get the default Port number?
I am using below code to get the Port Number.
using (ServerManager serverManager = new ServerManager())
{
SiteCollection allsites = serverManager.Sites;
foreach (Site site in allsites)
{
if (site.Name == "Default Web Site")
{
BindingCollection allBindings = site.Bindings;
foreach (Binding bind in allBindings)
{
if (bind.Protocol == "http")
{
PortNo = bind.BindingInformation.ToString();
int portNoLenth = PortNo.Length; PortNo = PortNo.Substring(2, portNoLenth - 3); break;
}
}
}
}
}
Please help me to solve this issue.
No, it is not redistrbutable, it should be installed in the machine with IIS. Even if you did, the likelihood is it will not work since it has dependencies on other libraries (native code DLLs) that need to exist and be registered.
I know this is an older post, but I believe that it is now redistributable as it appears as an assembly under extensions if you try to add a reference to it.

Localize WP7 application

I was looking into ways to localize an application. I saw this example: http://msdn.microsoft.com/en-us/library/ff637520(v=vs.92).aspx but I was wondering if it would be possible to use a different language than the one the user sets on his phone. Let's say that the user sets his language to English, but I want my app to display an interface in spanish if the user chooses so using some listbox.
Do you have any articles regarding this?
Thanks!
It is possible and I do it with my Google Reader client gReadie using the following code, which I call on application start and resume.
public static void SetLanguage() {
CultureInfo c = null;
switch (ViewModel.UserSettings.Language) {
case Language.Default:
break;
case Language.English:
c = new CultureInfo("en-US");
break;
case Language.Chinese:
c = new CultureInfo("zh-CN");
break;
case Language.French:
c = new CultureInfo("fr-FR");
break;
case Language.German:
c = new CultureInfo("de-DE");
break;
}
if (c != null) {
Thread.CurrentThread.CurrentUICulture = c;
ApplicationStrings.Culture = c;
}
}
So basically I have a dropdown in my settings which is bound to a languge enum and allows the user to choose their phone language (default) or one of the supported lanaguages.
Then on app start I set the language of the UI thread and of my ApplicationStrings resource file to match their selection.

Categories

Resources