Skip to content

Instantly share code, notes, and snippets.

@conceptdev
Last active April 26, 2017 16:33
Show Gist options
  • Save conceptdev/47986fd40c2f9a8b390a to your computer and use it in GitHub Desktop.
Save conceptdev/47986fd40c2f9a8b390a to your computer and use it in GitHub Desktop.
TranslateExtension for Xamarin.Forms Xaml. Note: the xmlns declaration in the <ContentPage> tag
/* FRENCH translation*/
/* NOTE: because i'm using Vernacular, the 'Vernacular_P0_' prefix is not required in the Xaml expression. */
/* Untranslated: <new task> */
"Vernacular_P0_60newtask62" = "nouveau";
/* Untranslated: ApplicationHeading */
"Vernacular_P0_ApplicationHeading" = "LionTodo";
/* Untranslated: Delete */
"Vernacular_P0_Delete" = "Supprimer";
/* Untranslated: Done */
"Vernacular_P0_Done" = "Terminer";
/* Untranslated: Name */
"Vernacular_P0_Name" = "Nom";
/* Untranslated: Notes */
"Vernacular_P0_Notes" = "Notes";
/* Untranslated: Save */
"Vernacular_P0_Save" = "Enregistrer";
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i18n="clr-namespace:QuickTodo;assembly=QuickTodo"
x:Class="TodoXaml.TodoItemXaml">
<ContentPage.Content>
<StackLayout VerticalOptions="StartAndExpand">
<Label Text="{i18n:Translate Name}" />
<Entry Text="" x:Name="nameEntry" Placeholder="task name" />
<Label Text="{i18n:Translate Notes}" />
<Entry Text="{Binding Path=Notes}" x:Name="notesEntry" />
<Label Text="{i18n:Translate Done}" />
<Switch IsToggled="{Binding Path=Done}" x:Name="DoneSwitch" />
<Button Text="{i18n:Translate Save}" Clicked="OnSaveActivated" />
<Button Text="{i18n:Translate Delete}" Clicked="OnDeleteActivated" />
<Button Text="{i18n:Translate Cancel}" Clicked="OnCancelActivated" />
<Button Text="{i18n:Translate Speak}" Clicked="OnSpeakActivated" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
using System;
using Xamarin.Forms.Xaml;
using Xamarin.Forms;
using Vernacular; // I'm using Vernacular to translate, you might have your own custom class
namespace QuickTodo
{
// You exclude the 'Extension' suffix when using in Xaml markup
[ContentProperty ("Text")]
public class TranslateExtension : IMarkupExtension
{
public string Text { get; set; }
public object ProvideValue (IServiceProvider serviceProvider)
{
if (Text == null)
return null;
// Do your translation lookup here, using whatever method you require
var translated = Catalog.GetString (Text);
return translated;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment