View
643
Download
0
Category
Preview:
Citation preview
@loriblalonde solola.ca
CTTDNUG
Me, Myself and I
Twitter: @loriblalonde
Email: loriblalonde@gmail.com
Blog: solola.ca
Web: westerndevs.com
@loriblalonde solola.ca
2
@loriblalonde solola.ca
Appeal to a Broader Market
Shared App Logic in C#
*photo attribution: created by niroworld at fotolia (https://www.fotolia.com/id/66976406)
@loriblalonde solola.ca
Shared App Logic in C#
*photo attribution: created by photosoup at fotolia (https://us.fotolia.com/id/61647429)
@loriblalonde solola.ca
Xamarin Android String Resources
Strings stored in XML file(s) in values directory
Place XML files in values directory for each supported language code
@loriblalonde solola.ca
Xamarin Android Image Resources
Images with text should be localized
Set Image BuildAction as AndroidResource
Place localized images in drawabledirectory for each supported language code and device resolution
@loriblalonde solola.ca
Xamarin.Android: Accessing Resources
In layout file:Strings - @string/[id]
Images - @drawable/[id]
In code: textView.Text = Resources.GetString(Resource.String.greeting);
imageView.SetImageResource(Resource.Drawable.stopsign);
@loriblalonde solola.ca
Xamarin iOS String Resources
Shared App Logic in C#
Strings stored in .strings files in .lprojdirectories
Base.lproj – default language
<languagecode>.lproj directory –supported language
.strings file format: “key” = “value”;
@loriblalonde solola.ca
Xamarin iOS Image Resources
Shared App Logic in C#
Place localized images in respective .lproj directories
Set Image BuildAction as BundleResource
@loriblalonde solola.ca
Xamarin.iOS: Accessing Resources
//app display namevar title = NSBundle.MainBundle
.ObjectForInfoDictionary("CFBundleDisplayName"); AppTitleLabel.Text = title.ToString();
//how to get the localized string in codeHelloLabel.Text = NSBundle.MainBundle.LocalizedString("hello", "comment");
//how to get localized image in codeStopImage.Image = UIImage.FromBundle("stopsign");
@loriblalonde solola.ca
Escape Characters
Shared App Logic in C#
Android and iOS string resources must use the following escape characters:
\" quote
\\ backslash
\n newline
@loriblalonde solola.ca
Universal Windows Platform (UWP) Resources
Resource file (.resw) localization
Compiled into the application package
Place resource files in named directory for each supported language code
@loriblalonde solola.ca
Resw File Structure
Shared App Logic in C#
Name/Value pair. Optionally append a qualifier on key.
Qualifier represents the element property that will be set to the resource value
UWP supports translations for app display name and description
@loriblalonde solola.ca
UWP: Accessing Resources
Shared App Logic in C#
In layout file:
Strings – set x:Uid of element to resource
Images – set source to path of default image
In code: var resourceLoader = new Windows.ApplicationModel.Resources.ResourceLoader();string res = resourceLoader.GetString("DisplayName");
@loriblalonde solola.ca
Localizing Application Metadata
Shared App Logic in C#
@loriblalonde solola.ca
@loriblalonde solola.ca
Drawbacks
Shared App Logic in C#
Maintain separate set of resource files for each platform
Platform-specific code needed to access localized resources
@loriblalonde solola.ca
Leverage .NET Localization
Create a Portable Class Library that will contain the resource (.resx) files
Leverage ResourceManager to load the strings
Create a Translation Helper class to retrieve translations by language and key
Use Reflection to retrieve handle to Assembly
@loriblalonde solola.ca
Resx File Structure
Shared App Logic in C#
Name/Value pair
Reference in code using ResourceManager
Can leverage data binding to retrieve translation
@loriblalonde solola.ca
@loriblalonde solola.ca
Supported across all platforms (iOS, Android, Windows)
Only 1 set of resource files to maintain
3rd party tools available such as ResX Manager, making it easy for translators to work with
Supported in Xamarin.Forms, with data binding support
Advantages
@loriblalonde solola.ca
Drawbacks
Shared App Logic in C#
Application name will not be localized
Workaround: include native strings file in each language for the application name only
@loriblalonde solola.ca
Download translations for the selected language from your own web service in JSON or XML format
Logic can be encapsulated in Portable Class Library
Deserialize result into a Translations collection (Key/Value pair) Use Reflection to retrieve handle to Assembly
Create a Translation Helper class to retrieve translations by language and key
Custom Web Service Provider
@loriblalonde solola.ca
Supported across all platforms
Only download the translation file that is needed
Supported in Xamarin.Forms
c
Advantages
@loriblalonde solola.ca
Drawbacks
Shared App Logic in C#
Application name will not be localized
Dependent on network connectivity when switching languages
Difficult for translators to work with JSON
@loriblalonde solola.ca
General Considerations
Shared App Logic in C#
Design your app to support multiple languages
Never hardcode strings
Store strings in language resource files
Avoid string concatenation
Ensure your UI is flexible to adapt to variable string length
@loriblalonde solola.ca
Additional Resources
Shared App Logic in C#
Xamarin Localization Documentation
iOS: http://bitly.com/XamiOSLocalization
Android: http://bitly.com/XamAndLocalization
Xamarin.Forms: http://bitly.com/XamFormsLocalization
Twitter: @loriblalonde
Email: loriblalonde@gmail.com
Blog: solola.ca
westerndevs.com
@westerndevs
Recommended