Xamarin looking for resources at the wrong place - xamarin

Environment: Visual Studio 2015, Xamarin 4.7.9.45, Xamarin Android 7.4.5.1
My development environment used to work fine until yesterday and now I am not able to debug properly. While loading string resources, the following code from AppResources.designer.cs throws a "file-not-found" exception:
public static string Button_Username {
get {
return ResourceManager.GetString("Button_Username", resourceCulture);
}
}
The resource culture is "en-US." The string resource Button_Username is indeed defined. Otherwise, the above lines of code wouldn't even be there in the auto-generated AppResources.designer.cs file.
Upon inspecting the exception in the debugger, it appears Xamarin is trying to load the following file:
/storage/emulated/0/Android/data/myapp.mycomp.com/
files/.__override__/en-US/MyProject.resources.dll
We generate MyProject.resources.dll for many other languages but not en-US as it is the default language. Why is Xamarin trying to load this dll? Shouldn't it be looking into resources.arsc file? Regards.

We generate MyProject.resources.dll for many other languages but not en-US as it is the default language. Why is Xamarin trying to load this dll? Shouldn't it be looking into resources.arsc file?
resources.arsc works for Android Native localization. But you are currently developing a Xamarin.Forms App and using its localization, where the related .resw files will be complied into MyProject.resources.dll and as your project sets en-US as the default culture.
If you have created AppResources.en-US.resw file and current culture is en-US. Your app will try to look for this file, which is contained in .../en-US/MyProject.resources.dll.
If you didn't created AppResources.en-US.resw your app will search the string in AppResources.resw file.

Related

Xamarin Forms creating a Folder in Local

Creating folder by following code
var documents=Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
var directoryname = Path.Combine(documents, "XX");
Directory.CreateDirectory(directoryname);
but the folder does not exists in the specific path..May i know whats the reason.
regards..
if you need the main picture folder on the android then your code should look like this
string AndroidDcimFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath;
From Xamarin Official Site:
Not all directories in this enumeration will be available on all
platforms for all users. When running on the Windows operating system,
the operating system determines the paths to these directories.
Enumeration means Environment.SpecialFolder.
You need to implement a DependencyService and need to call System.Environment.GetFolderPath from within the platform-specific implementation.
Check this sample.
As per my knowledge, it (your code) will not work in Xamarin.Form PCL project.
You can do another thing is, In your Android and iOS Project you can put this code to get the platform specific path:
using Xamarin Forms;
//Get current Path.
Config.PathApp = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);

How to add Windows Phone project with resx localization to Xamarin PCL solution?

I have a working PCL project for iOS and I'm trying to create the Windows Phone 8.1 version.
I'm following this tutorial: https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/
And checking the app at: https://github.com/xamarin/xamarin-forms-samples/tree/master/UsingResxLocalization
But it is too deprecated. Even the git project is different from the tutorial, and none of them works.
The ILocalize interface for Windows should look like:
[assembly: Dependency(typeof(UsingResxLocalization.WinPhone.Localize))]
namespace UsingResxLocalization.WinPhone
{
public class Localize : UsingResxLocalization.ILocalize
{
public System.Globalization.CultureInfo GetCurrentCultureInfo ()
{
return System.Threading.Thread.CurrentThread.CurrentUICulture;
}
}
}
But System.Threading.Thread.CurrentThread.CurrentUICulture simply doesn't exist. I found out that I can use Windows.System.UserProfile.GlobalizationPreferences.Languages[0].ToString() instead.
It works for the localized language resources but the default Resource is not working either for the default language "en" or any other non localized language like "ru". I get another error:
In the TranslateExtention class ProvideValue() method I get:
Key 'Start' was not found in resources 'AppNameSpace.AppResources' for
culture 'en'
Being "Start" the first key it tries to get from the resource. It happens for all the other keys on the project.
AppNameSpace.AppResources would be the right file, and "en" is the region I set, so it should work. But it's not.
I'm also getting the following warning when compiling:
The assembly "MyApp.dll"
does not have a NeutralResourcesLanguageAttribute on it. To be used in
an app package, portable libraries must define a
NeutralResourcesLanguageAttribute on their main assembly (ie, the one
containing code, not a satellite assembly). 4>C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v14.0\AppxPackage\Microsoft.AppXPackage.Targets(1216,5):
warning APPX2002: Cannot find the neutral resource language for the
resources for portable library 'MyApp'. Verify that
the portable library defines a NeutralResourcesLanguageAttribute. The
build is continuing assuming the project's default culture of 'en-US'
correctly identifies the portable library's neutral resources.
4>MakePRI : warning 0xdef00522: Resources found for language(s) 'de,
es, fr, pt' but no resources found for default language(s): 'en-US'.
Change the default language or qualify resources with the default
language. http://go.microsoft.com/fwlink/?LinkId=231899
But I have no idea how to fix it.
On the tutorial it also says:
Windows Phone projects must be properly configured for localized text
to be displayed. Supported languages must be selected in the Project
Options and the WMAppManifest.xml files. If these settings are not
updated the localized RESX resources will not be loaded.
Fine, but those options doesn't exist anymore. At least where they should be. I even found a Package.appxmanifest file in my project, but it doesn't have those regional options.
So, I need help with an updated way to do it.
Thanks
So I found out that when you add a Windows Phone project to a solution with no Windows Phone projects, it doesn't add everything it needs.
Also the tutorials don't show everything that is necessary (no big news there).
All my project was missing was [assembly: NeutralResourcesLanguage("en-US")] in my PCL AssemblyInfo.cs file.
The RESX tutorial also says that you should use:
if (Device.OS == TargetPlatform.iOS || Device.OS == TargetPlatform.Android)
{
ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
}
In the TranslateExtention.cs file because Windows Phones don't need it. Well, that's false. At least for the emulator to get the right language, it needs to use the DependencyService and get the CultureInfo this way:
System.Globalization.CultureInfo ci = null;
ci = new System.Globalization.CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0].ToString());
return ci;

Customize Calendar Winrt Xaml Toolkit

I'm developing an Universal App for Win8.1, i've added the Winrt Xaml Toolkit dll but when i use Calendar control the strings of months appear only in English.
Is there any way to change them? I'm going crazy :D
PS: I've downloaded the source code of dll but there is no track of these strings
If you look at the DateTimeHelper class, you can see the method GetCurrentDateFormat which uses the en-US format. This method is used in many places for the Calendar and CalendarItem controls from the toolkit.
You can try to import the project from the site instead of using the NuGet package and then change this method to use your device locale. You can maybe change it to something like this:
public static DateTimeFormatInfo GetCurrentDateFormat()
{
return CultureInfo.CurrentCulture.DateTimeFormat;
}
I haven't tested this code so this is just a hint where you should look for your solution.

WP7 Localization issues from C#

I'm having an issue with the localization in my converters. So, I tried a new project and still have problems.
So, I created a fresh new Silverlight for Windows Phone application (7.0, but 7.1 don't resolve my problem).
I added two resources files: Strings.resx and Strings.fr.resx. This two files have two string property: HelloString and ByeString, all initiated correctly.
Then, I go to the Mainpage.xaml.cs, in the ctor and add the following simple line.
PageTitle.Text = Strings.HelloString;
Whenever I have the emulator or my phone set to English or French, I always got the same strings (Hello).
I tried to rename the file Strings.fr-FR.resx. Still the same issue.
The CurrentUICulture returned from the current thread is correct.
I also tried to force culture on the resource:
Strings.Culture = new System.Globalization.CultureInfo("fr-FR");
Does anyone have an idea?
You don't say that you've set the SupportedCultures for the project. Without this the additional language resource files will never be used.
See http://msdn.microsoft.com/en-us/library/dd941931(v=vs.95).aspx

Localizing Windows Phone 7 App

I'm having a little trouble getting localized resources files to work on Windows Phone 7. Here's what I'm doing:
Create a resource file, say "Strings.resx" (Build Action: Compile)
Create a key, say "TestKey" with a default value of empty string
Add a English resource file in the same folder with a value of "some English string": Strings.en-us.resx (Build Action: Embedded Resource)
Add a Japanese resource file in the same folder with a value of "some Japanese string": Strings.ja-jp.resx (Build Action: Embedded Resource)
In my PC Silverlight, WPF Apps that works fine when I change the Thread.CurrentThread.CurrentCulture. But in the phone I always seem to be getting the value that's in the Strings.resx file - an empty string.
I have tried using the designer generated code and wiring up the resource manager by hand and it does not seem to matter. Here's my code:
Type t = typeof(Strings);
_resourceManager = new ResourceManager(
t.Namespace + "." + t.Name,
t.Assembly);
_resourceManager.GetString("TestKey");
Tell me localized resources are supported on the phone... ;> What am I doing wrong? Thanks!
Update: Thanks Olivier for forwarding the link. I saw that as well but missed an important step. I didn't add the "SupportedCultures" node to my csproj. Made all the difference - hoping someone else doesn't loose two hours trying to figure this out like I did.
<SupportedCultures>de-DE;es-ES;</SupportedCultures>
Of course, localized resources are supported on the phone:
How to: Build a Localized Application for Windows Phone
I wrote a blog post that provides links to a bunch of Globalization / Localization guides for WP7. There is a Windows Phone 7 in 7 Training video that helped me understand the basics. After that it was simply a matter of learning how to do databinding:
The MSDN article shows you how to
setup the files and create the
LocalizedStrings class, but they then
assume that you know how to use that
class for data binding. Visual Studio
2010 and Silverlight handle data
binding differently than Winforms, and
it gets even more confusing since XAML
also has it’s own definition of
Resources that are different then the
.NET resources we just created.
Silverlight also uses the term
Resource to refer to files that use
the the Build Action of "Content”, as
these files get wrapped up into the
.XAP file similar to how files with
Build Action of "Resource” get
embedded into the .Dll assembly (ex:
loading an image from content or
resource files). I found that instead
of using the Text="{Binding
Path=resourceFile.resourceName,
Source={StaticResource
Localizedresources }}" XAML syntax it
was easier to use the following steps:
Open your primary XAML page (usually MainPage.xaml) in the Visual
Studio designer
Open the properties for the PhoneApplicationPage and set the
DataContext to be
Application.Resources –>
LocalizedStrings. NOTE: if you already
are using a DataContext object, then
you should integrate the
LocalizedStrings class into that
object so that it has localization
support.
Once the Page’s DataContext has been set you can change the data
binding for any control on the page by
simply selecting the property (ex:
text, checked, etc), selecting “Apply
Data Binding…”, and setting the Path
to Localizedresources.BtnText or
whatever the name of the desired
resource value is.

Resources