コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
//日本語がメインの言語となるアプリの場合
[assembly: NeutralResourcesLanguage("ja-JP")]
//英語がメインの言語となるアプリの場合
[assembly: NeutralResourcesLanguage("en-US")]
<root>
・
・途中省略
・
<data name="PageTitle" xml:space="preserve">
<value>ページタイトル</value>
<comment>タイトルの備考</comment>
</data>
</root>
<root>
・
・途中省略
・
<data name="PageTitle" xml:space="preserve">
<value>Page Title</value>
<comment>title description</comment>
</data>
</root>
using System;
namespace AppName.Library.Globalization
{
public class PlatformCulture
{
public PlatformCulture(string platformCultureString)
{
if (String.IsNullOrEmpty(platformCultureString))
{
throw new ArgumentException("Expected culture identifier", "platformCultureString"); // in C# 6 use nameof(platformCultureString)
}
PlatformString = platformCultureString.Replace("_", "-"); // .NET expects dash, not underscore
var dashIndex = PlatformString.IndexOf("-", StringComparison.Ordinal);
if (dashIndex > 0)
{
var parts = PlatformString.Split('-');
LanguageCode = parts[0];
LocaleCode = parts[1];
}
else
{
LanguageCode = PlatformString;
LocaleCode = "";
}
}
public string PlatformString { get; private set; }
public string LanguageCode { get; private set; }
public string LocaleCode { get; private set; }
public override string ToString()
{
return PlatformString;
}
}
}
using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using AppName.Services;
namespace AppName.Library.Globalization
{
// You exclude the 'Extension' suffix when using in Xaml markup
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
private static CultureInfo _ci = null;
private const string ResourceId = "AppName.Resources.AppResources";
public TranslateExtension()
{
if (Device.RuntimePlatform == Device.iOS ||
Device.RuntimePlatform == Device.Android)
{
if (_ci == null)
{
_ci = DependencyService.Get<ILocalizeService>().GetCurrentCultureInfo();
}
}
}
public string Text { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return "";
ResourceManager resmgr = new ResourceManager(ResourceId,
typeof(TranslateExtension).GetTypeInfo().Assembly);
var translation = resmgr.GetString(Text, _ci);
if (translation == null)
{
#if DEBUG
throw new ArgumentException(
String.Format("Key '{0}' was not found in resources '{1}' for culture '{2}'.", Text, ResourceId, _ci.Name),
"Text");
#else
translation = Text; // HACK: returns the key, which GETS DISPLAYED TO THE USER
#endif
}
return translation;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i18n="clr-namespace:AppName.Library.Globalization;assembly=AppName"
x:Class="AppName.Views.TestPage"
NavigationPage.TitleIcon="icon.png">
<Label x:Name="PageTitle" Text="{i18n:TranslateExtension Text=PageTitle}" />
<ContentPage>
public partial class TestPage : ContentPage
{
public TestPage() : base()
{
Title = AppResources.PageTitle;
}
}
public static class Common
{
public static string GetTranslateString(string key)
{
return AppResources.ResourceManager.GetString(key);
}
}
string title = Common.GetTranslateString("PageTitle");
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。