NoNameSampleUser
NoNameSampleUser

Reputation: 45

Localize UWP Appllication

I'm porting application from Windows Phone 8.1 to UWP and I have problem with getting strings from resources. In WP 8.1 I could get string by writing:

Text="{Binding Path=LocalizedResources.lDownloadStatus , Source={StaticResource LocalizedStrings}}"

Is there any similar solution in UWP or I have to set x:UID, and then in resource file set for example: "Button.Content | SampleContent" ?

Upvotes: 0

Views: 649

Answers (4)

Hans Voralberg
Hans Voralberg

Reputation: 69

You should use x:Uid

For example, you want to set "Save" as Content of a button. So you can add x:Uid = "SaveButton" to the button and then create an entry in Resource file named "SaveButton.Content" and set its value to "Save".

Upvotes: 0

FloppyNotFound
FloppyNotFound

Reputation: 304

I have made very good experiences with the CustomResources.

Just create a new class anywhere in your UI project which inherits from CustomXamlResourceLoader and implement the method below:

protected override object GetResource(string resourceId, string objectType, string propertyName, string propertyType)
    {
        var parts = resourceId.Split('|');

        if (parts.Length != 2)
            throw new ArgumentException();

        return ResourceLoader.GetForViewIndependentUse(parts[0]).GetString(split[1]);
    }

Then you create Resource-Files, I used to name them like the page I am on, i.e. "MainPage.resw".

That's it, now you can start use your resources from XAML:

Text="{CustomResource MainPage|MyResourceKey}"

Of course, the seperator can be anything you want to, it is not limited to a Pipe. Just make sure you use the same seperator in your ResourceLoader and your XAMLs.

You won't get a preview in the Designer, but the big advantage is that you can split your resource files and don't have to put everything into one.

See https://msdn.microsoft.com/en-us/windows/uwp/xaml-platform/customresource-markup-extension for more information.

Upvotes: 0

Jet  Chopper
Jet Chopper

Reputation: 1488

You should create folder "Strings" in you project, for French language create inside it another folder with country LCID name (fr-fr for France). And create there resource.resw file.

If you want to use value in TextBlock.Text, name your field in .resw file as "Hello.Text" and give it a value "Bonjour".

Go to your TextBlock and set x:Uid="Hello".

Change yor language with ApplicationLanguages.PrimaryLanguageOverride = "fr-fr".

Upvotes: 2

Ann Yu
Ann Yu

Reputation: 137

Use x:uid to load your resource strings or you can get resource strings from back end by using ResourceManager. Here is a sample from MSDN: How to create a localization UWP App.

Hope it helps!

Upvotes: 0

Related Questions