本地化资源中的文字样式

编程入门 行业动态 更新时间:2024-10-11 15:22:35
本文介绍了本地化资源中的文字样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对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的东西时,我已经实现了非常基本的类似BBCode的标记语言,只有几个标签:" [b]," [/b]," [[和"]](在我的项目,我什至不需要斜体,只需要粗体).

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; } } }

更多推荐

本地化资源中的文字样式

本文发布于:2023-10-19 18:52:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   文字   资源

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!