WP7 localization does not work - windows-phone-7

I'm trying to do a simple localization of my app, making it support English (default) and Danish.
I've followed the MS tutorial, and I've looked at some samples but for some reason the simulator does not show the danish version when I choose danish language as the simulator language.
Here's what I've done:
Added supported culture: dk-DK;
Changed assembly info to use "English" as default.
Added the resource to app.xaml:
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:LåneRegnskab" x:Key="LocalizedStrings" />
</Application.Resources>
Added "AppResources.resx" and "AppResources.dk-DK.resx" to project with the strings.
To use the strings I write:
"{Binding Path=LocalizedResources.Title, Source={StaticResource LocalizedStrings}}"
LocalizedStrings class:
public class LocalizedStrings
{
public LocalizedStrings()
{
}
private static AppResources localizedResources = new AppResources();
public AppResources LocalizedResources { get { return localizedResources; } }
}
This all works for the english strings, but they do not change when I'm in danish mode. What am I missing here? :(

Nothing obvious wrong with your code. Try force-chance the culture in App.xaml.cs with the following code in the InitializePhoneApplication method.
private void InitializePhoneApplication()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
Update
Also ensure that SupportedCultures in your ProjectName.csproj file is set to support both languages, like this: <SupportedCultures>en;da-DK</SupportedCultures>

Thanks to Claus, I solved my problems (I seem to have made all the mistakes getting there) but here's all the settings that work for me.
I'm supporting English and Spanish and changing the region of the emulator to see it work.
In the .csproj
<SupportedCultures>en;es;</SupportedCultures> <-- I was being too specific on language here
I also had AppResources-es.resx <-- Rather than .es
In my GamePage.xaml
I made the mistake here of having LocalisedStrings in both source and Path.
In the App.xaml I didn't add the namespace inline, but the same otherwise.
Hopefully it's a mistake in one of these steps as it was in my case.

I struggled with the same problem and I've just found the solution. In the csproj file the node is defined by default, but I didn't noticed that and I created another one at the first lines...
So if you remove this(or set your cultures here) it will probably work.
<SilverlightApplication>true</SilverlightApplication>
<SupportedCultures>
</SupportedCultures>
<XapOutputs>true</XapOutputs>

You have to change the csproj file as in the example bellow
http://msdn.microsoft.com/en-us/library/dd941931%28v=vs.95%29.aspx

Related

Cannot add Flyout Footer in Xamarin forms app

I have a xamarin forms application and I want to add a footer to the flyout menu. After reading the official documentation it should be straightforward, just adding a few lines into the AppShell.xaml like so:
<Shell.FlyoutFooterTemplate>
<DataTemplate>
<Label HeightRequest="300" BackgroundColor="Red"/>
</DataTemplate>
</Shell.FlyoutFooterTemplate>
This works perfectly fine when I tried it in a new project, but for some reason, it doesn't work in my current application giving this error:
Error XLS0415 The attachable property 'FlyoutFooterTemplate' was not found in type 'Shell'.
I tried to find the definition of FlyoutHeaderTemplate and I found this in Shell [from metadata] file:
[1]: https://i.stack.imgur.com/xqZub.png
public Shell();
...
public DataTemplate FlyoutHeaderTemplate { get; set; }
public FlyoutHeaderBehavior FlyoutHeaderBehavior { get; set; }
public object FlyoutHeader { get; set; }
There should be a similar definition for both, Header and Footer, but there is only one for the Header. The file cannot be edited and I was not able to locate it either. Any ideas why the definition for Footer is missing, how can I add it, or workarounds?
PS: Adding the footer from C# code doesn't work either and I tried to clean/rebuild and resetting both, PC and VS.
First, Confirm that you can add a simple Header:
<Shell.FlyoutHeader>
<Label Text="This is the header." />
</Shell.FlyoutFooter>
If that doesn't work, then you are doing something fundamentally wrong - post the code for the <Shell> XAML, within which you added those lines. Make sure you include those lines, to show where in the XAML they are. Make sure they are between <Shell> and </Shell>, but not nested inside some deeper node. For example, they musn't be inside a <StackLayout> or <ContentView> or other container - they must be direct children of the <Shell>.
Unless you are doing something fancy, you don't need a Template.
Try simply:
<Shell.FlyoutFooter>
<Label HeightRequest="300" BackgroundColor="Red"/>
</Shell.FlyoutFooter>
If 1) above works, but not 2), then your project is referencing an out-of-date version of Shell. Fixes:
A. Check that project's Xamarin.Forms nuget doesn't need an Update. (Solution / Manage Nugets.)
B. OR delete bin and obj folders. Then Rebuild Solution.
C. Worst case, start with a new project, in which you are able to use that functionality, and add back in all your files.

Xamarin.forms Stand-alone resource dictionaries don't update dynamically if there is no cs code behind file

Xamarin team introduced 2 different way of stand-alone-resource-dictionaries. Obviously there is a big difference but they neglected to write in their documentation.
Basically if i use without cs code behind file and without x:class defined as defined in the documentation and referencing like that in my page
<ResourceDictionary Source="GradientStyles.xaml"/>
GradientStyles.xaml look like below and i am updating GradientStartColor , GradientStopColor of PancakeView on runtime based on the theme selected.
<pancakeView:GradientStopCollection x:Key="BackgroundGradientStops">
<pancakeView:GradientStop Color="{StaticResource GradientStartColor}"
Offset="0"/>
<pancakeView:GradientStop Color="{StaticResource GradientStopColor}"
Offset="0.74"/>
</pancakeView:GradientStopCollection>
Implementation above just not working. It doesn't update Colors at all.
The same exact code;
Works in ContentPage.Resources directly. It will update
Works if i define a stand-alone-resource-dictionary with cs code behind file and implement on the page like this below
<ResourceDictionary.MergedDictionaries>
<resDics:GradientStyles />
</ResourceDictionary.MergedDictionaries>
There is that annotation in the documentation but i don't get what exactly it means.
This syntax doesnot instantiate the MyResourceDictionary class.
Instead, it references the XAML file.
Does it mean that it extends the contentpage using like partial class or inherits. Or is it cached only once and remains static? And when you use it with code behind class, it creates a new instance every time?
Finally is that a bug or feature? :)

When creating a template is there any reason to have a back end C# initializer?

I have this example in my application:
<?xml version="1.0" encoding="utf-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:J;assembly=J"
x:Class="J.Templates.HelpGrid"
Padding="15"
RowSpacing="10"
ColumnSpacing="10" />
and in c#
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace J.Templates
{
public partial class HelpGrid : Grid
{
public HelpGrid()
{
InitializeComponent();
}
}
}
From what I see the code works and I can use that template without or without my coding a back end c#.
Is there any reason why the developer might have chosen to add the back end C# code?
The backing class (c# file) is needed by the compiler to work, at this time you cant avoid it.
In theory, it is possible to eliminate the need of the backing class, but it's not an easy thing to do.
They could add a new task in the compile pipeline but this will slow the build a lot (more xaml, more slow).
At the end there is very little benefits and a lot of works, its not worth.
You can read the full explanation, made by the Xamarin staff, here:
https://github.com/xamarin/Xamarin.Forms/issues/4327#issuecomment-436921856

Image not displaying in Xamarin forms using XAML?

I want to display an Image in my Xamarin forms project using XAML but is is not displaying.
This is the code:
<Image x:Name="myimage" Source="logo.jpg"/>
I have set myimage's build action to embedded resource. I also tried this
<Image x:Name="myimage" Source="MyXamarinProject.logo.jpg"/>
Where MyXamarinProject is my namespace. But both not working. What is wrong here?
The recommended way to avoid decreasing application performance or problems displaying the images on different screen resolutions is adding each image on each platform specific folder:
Android: Project -> Resources -> Drawable (or each resolution drawable folder (-hdpi,-xhdpi...))
iOS: You can create an asset catalog for each image or use Project -> Resources folder.
And then, use the image name on XAML file. No code needed.
Upon inspecting the Microsoft Docs for this, it is not as easy as just specifying the path.
First, Create an IMarkupExtension
[ContentProperty (nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue (IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
return imageSource;
}
}
Then consume it in your XAML:
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
x:Class="WorkingWithImages.EmbeddedImagesXaml">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<!-- use a custom Markup Extension -->
<Image Source="{local:ImageResource WorkingWithImages.beach.jpg}" />
</StackLayout>
</ContentPage>
Note that you have to add a custom namespace to your page, in this case: xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
So, I can not display an image using XAML only. I am pretty satisfied with both the answers. Although this is how I displayed the image without pasting into the android and iOS(to their respective folders) and without making IMarkupExtension.
I put one line of code into C# code behind and that worked:
myimage.Source = ImageSource.FromResource("MyXamarinProject.logo.jpg");
//logo.jpg must be Embedded Resource.
This could not be the right answer but it can be a choice. Thanks!
I had the same problem on Visual Studio Mac:
my XAML :
<Image x:Name="myimage" Source="logo.jpg"/>
my resources was : logo.jpg
But the file was saved in the file system as Logo.jpg (Capital L).
I just modified it to logo.jpg and rebuild all.

joomla custom component - add rich text support

I am developing a cusom component and I would like it to support rich text fields. Possibly the same way it is done in com_content.
in the form definition I have the following field:
<field
name="description"
type="editor"
label="COM_MYCOMPONENT_DESCRIPTION_LABEL"
description="COM_MYCOMPONENT_DESCRIPTION_DESC"
class="inputbox"
filter="MyComponentHelper::filterText"
buttons="true"
/>
So basically what happens is that the editor appears as it is supposed to but the text is saved without formatting.
The MyComponentHelper::filterText method was added later after investigating com_content and copying the filterText method to my helper, but it did not work either with or without the line. I even tried to use ContentHelper::filterText but without success.
In joomla 1.5, you had to do this in the model (in function that does the saving):
$data['description'] = JRequest::getVar('description', '', 'post', 'string', JREQUEST_ALLOWRAW);
if (!$row->bind($data)) {
...
Don't know if it still exists in Joomla 1.6. Hope it helps.
OK, so this was my bad. As I have followed the tutorial MyComponenetHelper ended up as an abstract class. I made it a normal class and everything works fine.

Resources