Search code examples
c#xamarin.formslocalizationmultilingualcode-behind

Xamarin Forms Localization in code behind


I've implemented from here and followed instructions. It works only in xaml with

<Label Text="{i18n:Translate SelectLanguage}"  />

But I need it in my code behind:

 Label selectLabel = new Label
        {
            Text = "{i18n:Translate SelectLanguage}",
            TextColor = Color.Black
        };

Output for that label is: {i18n:Translate SelectLanguage}. Is there any workaround?

UPDATE: As requested, I added my code (code behind and xaml pages). If you need something more please tell me. I've just uploaded xamarin to the latest version, and I have still the same problem.

My xaml page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MyProj.Forms.Pages.TestPages.ChangeLanguagePage"
         xmlns:i18n="clr-namespace:MyProj.Forms.Helpers"
         Title="{i18n:Translate ChangeLanguage}">>
<ContentPage.Content>
    <StackLayout Padding="20" VerticalOptions="CenterAndExpand">
        <Label   Text="{i18n:Translate SelectLanguage}"  />
        <Picker x:Name="picker" />
        <Button Text="{i18n:Translate Save}" Clicked="OnUpdateLangugeClicked" BackgroundColor="Black" TextColor="White"/>
    </StackLayout>
</ContentPage.Content>
</ContentPage>

My code behind page:

    [XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ChangeLanguagePage : ContentPage
{
    Picker picker;
    public ChangeLanguagePage()
    {
        InitializeComponent();
        StackLayout main = new StackLayout();

        Label selectLabel = new Label
        {
            //Text = "Select Language",
            Text = "{i18n:Translate SelectLanguage}",
            TextColor = Color.Black
        };

        picker = new Picker();

        Button btn = new Button
        {
            Text = "SAVE",
            TextColor = Color.White,
            BackgroundColor = Color.Black
        };
        btn.Clicked += Btn_Clicked;
        picker.Items.Add("English");
        picker.Items.Add("Spanish");
        picker.Items.Add("Portuguese");
        picker.Items.Add("French");
        picker.SelectedItem = CrossMultilingual.Current.CurrentCultureInfo.EnglishName;

        main.Children.Add(selectLabel);
        main.Children.Add(picker);
        main.Children.Add(btn);

        Content = main;
    }

    private void Btn_Clicked(object sender, EventArgs e)
    {
        CrossMultilingual.Current.CurrentCultureInfo = CrossMultilingual.Current.NeutralCultureInfoList.ToList().First(element => element.EnglishName.Contains(picker.SelectedItem.ToString()));
        AppResources.Culture = CrossMultilingual.Current.CurrentCultureInfo;
        App.Current.MainPage = new NavigationPage(new HomePage());
    }

    void OnUpdateLangugeClicked(object sender, System.EventArgs e)
    {

        CrossMultilingual.Current.CurrentCultureInfo = CrossMultilingual.Current.NeutralCultureInfoList.ToList().First(element => element.EnglishName.Contains(picker.SelectedItem.ToString()));
        AppResources.Culture = CrossMultilingual.Current.CurrentCultureInfo;
        App.Current.MainPage = new NavigationPage(new HomePage());

    }
}

Solution

  • There is no code-equivalent, the services are only available via XAML

    But you can easily replace it.

    const string ResourceId = "$rootnamespace$.AppResources";
    var resmgr = new ResourceManager(ResourceId,typeof(TranslateExtension).GetTypeInfo().Assembly));        
    var ci = CrossMultilingual.Current.CurrentCultureInfo;
    Label selectLabel = new Label
    {
      Text = resmgr.GetString("SelectLanguage",ci),
      TextColor = Color.Black
    };
    

    Remember to replace $rootnamespace$ with your project namespace.

    https://github.com/CrossGeeks/MultilingualPlugin/blob/master/content/TranslateExtension.txt.pp