PCL Localizer.

How to use Pcl Localizer step by step
PclLocalizer is a project for creating cross platorm localization for your apps. You can use any Excel-like program for managing your resources.
PclLocalizer is on GitHub HERE

Step 1

Create a simple solution with a Portable Class Library and a Console App:

enter image description here

Add Pcl.Localizer Nuget to both projects:

enter image description here

Add Pcl.Localizer.Generator to Portable Class Library:

enter image description here

This will add a Loc with 3 files:

  • example.txt
  • Runner.bat
  • PclLocalizer.Console.exe

enter image description here

the bat file will be:

PclLocalizer.Console.exe -f example.txt -d MyLoc.cs -n MyNameSpace -c MyLoc -s ;

where: - -f is the input file - -d is the destination file - -n is the namespace for generated class - -c is the name of the generated class - -s is the separator

so if you run the bat file the example.txt file will be parsed, a MyLoc.cs file will be generated with a class called MyLoc in MyNameSpace namespace using ';' like separator.

You can modify the bat for include yout files and respect your namespaces

Try run it now:

enter image description here

To add "Run Batch" from Visual studio context menu follow this guide.

to add MyLoc.cs to the project you must click on "Show all files" (red circle) right click on the file and "Include In Project"

Now reference HelloLocalizer.Pcl from Console and modify Program.cs like:

 class Program
{
    static void Main(string[] args)
    {
        PclResMan.SetLanguage(CultureInfo.CurrentCulture.Name);

        System.Console.WriteLine(MyLoc.Test);

        System.Console.Read();
    }
}

The class PclResMan (in PclLocalizer package) has a SetLanguage method for init the language of your app. So you can easily use localization like:

MyLoc.Test

The generated class expose all your localization keys like property so you can easily find them with Visual Studio intellisense. You can use

MyLoc.GetValue("yourkey")

for call localization by string.

PclResMan has a SetDefault method too for settings the fallback culture if the passed culture not exist in localization.

So you can easily use a Excel (or similar) file for create localization, then export it as csv with a safe separator and generate your resource!!

The generated code could be used on Xamarin.Android, Xamarin.iOS, Xamarin.Mac, Xamarin.Forms or any other Windows Project!

Happy coding!

Mark Jack Milian

12-September-2016 @ 22:26

share post :