本地化资源中的文字样式 [英] Text styles in localization resources

查看:108
本文介绍了本地化资源中的文字样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Windows Phone上的本地化资源有疑问.

I've a question about localization resources at windows-phone.

比方说,我的资源文件中有一个字符串,该字符串应类似于:

Let's say, I have a string in my Resources file, which should look like as:

这是一些文字. 此值为粗体. 这个是斜体的.

This is some text. This value is bold. This one is italic.

所有这些都存储在单个字符串字段中.如何在参考资料本身中定义诸如粗体或斜体这样的文本样式?我知道,我可以预定义一些自定义字体,如下所示:

And that all stored in a single string field. How could I define such text styles like bold or italic in the Resources itself? I know, I can predefine some custom fonts, like this:

<FontFamily x:Key="CustomBold">...</FontFamily>

,然后在页面中将其添加为{StaticResource CustomBold},但这可以解决,如果字符串字段中的整个文本为粗体.如果我想在词组的中间加粗一个单词怎么办?

and then add is as {StaticResource CustomBold} in page, but that can be a solution if the whole text in string field is bold. And what if I want to make bold a single word in the middle of the phrase?

我想使用本机c#风格的资源(即字符串名称->字符串值),而不是编写其他实现.

I want to use native c#-style Resources(i.e. string name -> string value), not writing different implementation.

推荐答案

当我需要类似iOS的东西时,我已经实现了非常基本的

When I needed something like this for iOS, I've implemented very basic BBCode-alike markup language with only a few tags: "[b]", "[/b]", "[[" and "]]" (in my project I didn't even needed the italic, only bold).

但是,.NET没有我用来解析语法的NSScanner类的类似物.相反,它对解析XML数据有更好的支持.因此,在WP7上,仅需< b>即可实现非常基本的XML子集.和< i>支持的标签.请参见获取示例代码.

However, .NET doesn't have an analog of NSScanner class that I used to parse the syntax. Instead, it has much better support for parsing XML data. So, on WP7 it's easier to implement a very basic subset of XML, with just <b> and <i> tags supported. See the end of this page for sample code.

这是将格式化的文本片段添加到WP7 TextBlock中的方法.

更新:好的,这是为您提供的完整解决方案:

Update: OK, here's the complete solution for you:

[Flags]
enum eParseState: byte
{
    bold = 1,
    italic = 2,
}

// Sample input: "<txt>This is <i>some</i> text. <b>This value is <i>bold</i>.</b> This one is not.</txt>"
static void parseRichText( TextBlock tb, string xml )
{
    tb.Inlines.Clear();
    XmlReader reader = XmlReader.Create( new StringReader( xml ), new XmlReaderSettings() { ConformanceLevel=ConformanceLevel.Fragment } );
    eParseState state = 0;

    var names = new Dictionary<string, eParseState>()
    {
        { "b", eParseState.bold },
        { "i", eParseState.italic },
    };

    Action<bool> actElement = ( bool isOpening ) =>
    {
        string name = reader.Name.ToLower();
        eParseState flag;
        if( !names.TryGetValue( name, out flag ) ) return;
        if( isOpening )
            state |= flag;
        else
            state &= ( ~flag );
    };

    while( reader.Read() )
    {
        switch( reader.NodeType )
        {
            case XmlNodeType.Element:
                actElement( true );
                break;
            case XmlNodeType.EndElement:
                actElement( false );
                break;
            case XmlNodeType.Text:
                var run = new Run() { Text = reader.Value };
                if( 0 != ( state & eParseState.bold ) ) run.FontWeight = FontWeights.Bold;
                if( 0 != ( state & eParseState.italic ) ) run.FontStyle = FontStyles.Italic;
                tb.Inlines.Add( run );
                break;
        }
    }
}

这篇关于本地化资源中的文字样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆