📜 ⬆️ ⬇️

Porting and localizing the application on Windows Phone 8

In the previous article I described how to develop sudoku for windows 8.1 , in this I'll tell you how to port an application to Windows Phone 8 and localize it into several languages.

image

First, create a Windows Phone template project.

Windows Phone Application Template
')
In this solution, I copied the necessary controls from the Windows 8.1 project to the Windows Phone project. This solution is not the best, but it is the fastest. In the following articles I will show how to make cross-platform solutions.
Now back to porting the application. In the game on the phone, it was decided to make page-by-page navigation. Created 4 pages:

sudoku pages

The game begins on the GamePage page. If there is no saved game, you need to send the user to the screen to create a new game:

Source
protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); GameState game = SettingsProvider.LoadSavedGame(); if (game == null) { NavigationService.Navigate(new Uri("/Pages/NewGame.xaml", UriKind.Relative)); } else { //... } } 


Work with files and sessions in Windows 8.1


The Windows 8.1 template will automatically create the SuspensionManager class, which implements the mechanism for saving sessions. Then in the page class you need to define the NavigationHelperLoadState and NavigationHelperSaveState methods. The NavigationHelperLoadState code is shown below, state saving is similar.

Source
  private async void NavigationHelperLoadState(object sender, LoadStateEventArgs e) { try { if (SuspensionManager.SessionState.ContainsKey("game-data")) { string previousGame = SuspensionManager.SessionState["game-data"] as string; if (!string.IsNullOrEmpty(previousGame)) { GameStateModel game = GameStateModel.FromJson(previousGame); if (game != null) { LoadGameToBoard(game.ToGameState()); } } } } catch (FileNotFoundException fileNotFound) { } catch (Exception) { } } 


Work with files and sessions in Windows Phone 8


On the phone, working with files is slightly different. There are such concepts as IsolatedStorageSettings. This concept came from Silverlight version 2. As the name implies, this type is designed to store files and data in isolation on the local file system. This means that another application besides yours will not have access to this data.

Game Data Load Code
  public static GameState LoadSavedGame() { try { IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; if (settings.Contains(SavedGame)) { string previousGame = settings[SavedGame] as string; if (!string.IsNullOrEmpty(previousGame)) { GameStateModel game = GameStateModel.FromJson(previousGame); if (game != null) { return game.ToGameState(); } } } } catch (Exception exception) { } return null; } 


Features found


Windows Phone 8 does not allow fonts to be stored in xaml resources. This code on WP8 is not valid:

 <!-- Fonts --> <FontFamily x:Key="ThemeFontFamily">Segoe UI Light</FontFamily> <FontWeight x:Key="ThemeFontWeight">SemiLight</FontWeight> 


So I had to split the resource files and clear the excess from WP8.

Working with markup


Very pleased with the work with the markup. Firstly, there is no need to make an application layout for 3 types as in Win 8.1 (Full, Filled, Snapped). It is important to make a layout for the screen as a whole. In Sudoku, only the vertical position of the screen is supported (99% horizontal support will not be maintained).
After adapting the layout, styles and sizes of objects, it is necessary to test on various devices and screens.

Windows Phone 8

Localization of the application name


Unlike Windows 8.1, applications are described on https://dev.windowsphone.com when setting up publishing. And the name is stored in the application. But not everything is so simple :)

There is such a thing as Display Name and Tile Title . The first will be used in the market and the list of applications on the phone. The second is used as an inscription on the tile, if this option is selected.

As described in MSDN How to localize an app title for Windows Phone (links at the end) - we need to create a C ++ DLL with a resource file containing 2 fields: the name and the name for the tile. For each language you need to create your own dll. Yes, it's sad. But the WP8 Localize project comes to the rescue . Download the tool, fill in the fields and automatically create a dll for all the necessary languages. We saved a couple of hours for sure.

After creating all the dll, they must be added to the project. I prefer that they were all in a separate folder. I called her Langs and put them all there. Do not forget to change BuildAction = Content .

Localization of the name of the application Windows Phone 8

In the WPAppManifest.xml file, we change the name and the tile to @ Langs / AppResLib.dll, -100 and @ Langs / AppResLib.dll, -200, respectively.

Changing the name of the application

At this stage, the localization of the application name and the tile is completed. Getting started changing the language for the application content.

Localization Xaml


In Windows Phone 8, localization tools have become much better than in version 7. Create or find the LocalizedStrings class.

Source
 /// <summary> /// Provides access to string resources. /// </summary> public class LocalizedStrings { private static AppResources _localizedResources = new AppResources(); public AppResources LocalizedResources { get { return _localizedResources; } } } 


Create a folder Resources in the project. For each supported language, create a resource file AppResources.LOCALE.resx (for example, AppResources.resx and AppResources.ru.resx ). The second step is creating resources in the App.xaml file:

 <Application.Resources> <winPhone8:LocalizedStrings xmlns:local="clr-namespace:Oxozle.Sudoku.WinPhone8" x:Key="LocalizedStrings"/> </Application.Resources> 


Thus, we give the possibility of binding in xaml.

WP localization

The file itself looks like this:

localization file

The important difference is that here it is specifically a file of text resources, not objects. Since binding (binding) and resource code generation occurs - the dot separator cannot be used. Next, you need to configure a bundle for text elements to resources.

 <TextBlock Text="{Binding LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextNormalStyle}"/> 


For the New Game page:

Create localized markup

Localization is also supported in layout mode (everything is correctly picked up in Expression Blend)

Localization from code


ApplicationBar does not support localization on binders. To do this, when creating a project, the commented code of the BuildLocalizedApplicationBar method is BuildLocalizedApplicationBar . But there is nothing difficult to write a few lines to build the application menu.

BuildLocalizedApplicationBar
 private void BuildLocalizedApplicationBar() { // Set the page's ApplicationBar to a new instance of ApplicationBar. ApplicationBar = new ApplicationBar(); //// Create a new button and set the text value to the localized string from AppResources. //ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/edit.png", UriKind.Relative)); //appBarButton.Text = AppResources.GamePage_Pencil; //ApplicationBar.Buttons.Add(appBarButton); // Create a new menu item with the localized string from AppResources. ApplicationBarMenuItem appBarNewGame = new ApplicationBarMenuItem(AppResources.NewGameTitle); appBarNewGame.Click += delegate { NavigationService.Navigate(new Uri("/Pages/NewGame.xaml", UriKind.Relative)); }; ApplicationBar.MenuItems.Add(appBarNewGame); ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.GamePage_ButtonAbout); appBarMenuItem.Click += delegate { NavigationService.Navigate(new Uri("/Pages/AboutPage.xaml", UriKind.Relative)); }; ApplicationBar.MenuItems.Add(appBarMenuItem); ApplicationBarMenuItem appBarRate = new ApplicationBarMenuItem(AppResources.WinGame_Rate); appBarRate.Click += delegate { MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask(); marketplaceReviewTask.Show(); }; ApplicationBar.MenuItems.Add(appBarRate); } 


Enable localization



Unlike Windows 8.1, localization must be enabled manually. To do this, in app.xaml.cs, in the App method, add a call to the InitializeLanguage method.

InitializeLanguage
 // Language display initialization InitializeLanguage(); //  (    ). private void InitializeLanguage() { try { // Set the font to match the display language defined by the // ResourceLanguage resource string for each supported language. // // Fall back to the font of the neutral language if the Display // language of the phone is not supported. // // If a compiler error is hit then ResourceLanguage is missing from // the resource file. RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage); // Set the FlowDirection of all elements under the root frame based // on the ResourceFlowDirection resource string for each // supported language. // // If a compiler error is hit then ResourceFlowDirection is missing from // the resource file. FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection); RootFrame.FlowDirection = flow; } catch { // If an exception is caught here it is most likely due to either // ResourceLangauge not being correctly set to a supported language // code or ResourceFlowDirection is set to a value other than LeftToRight // or RightToLeft. if (Debugger.IsAttached) { Debugger.Break(); } throw; } } 



In AssemblyInfo, specify the default culture:

 [assembly: NeutralResourcesLanguageAttribute("en-US")] 


In the WPAppManifest.xml file on the Packaging tab, we specify the application settings: the list of supported languages, the default language.

language selection

findings


Localization of the Windows Phone 8 application in some places is fundamentally different from the same localization in Windows 8.1. However, nothing complicated about it. It is important to have full instructions before creating a localization or adding support for a new language. I hope this article will help when creating a multilingual application. You can see the result here:

Sudoku +

Sources


Source: https://habr.com/ru/post/206852/


All Articles