<bdo id='za6Nu'></bdo><ul id='za6Nu'></ul>
  • <small id='za6Nu'></small><noframes id='za6Nu'>

      <legend id='za6Nu'><style id='za6Nu'><dir id='za6Nu'><q id='za6Nu'></q></dir></style></legend>
      <tfoot id='za6Nu'></tfoot>

    1. <i id='za6Nu'><tr id='za6Nu'><dt id='za6Nu'><q id='za6Nu'><span id='za6Nu'><b id='za6Nu'><form id='za6Nu'><ins id='za6Nu'></ins><ul id='za6Nu'></ul><sub id='za6Nu'></sub></form><legend id='za6Nu'></legend><bdo id='za6Nu'><pre id='za6Nu'><center id='za6Nu'></center></pre></bdo></b><th id='za6Nu'></th></span></q></dt></tr></i><div id='za6Nu'><tfoot id='za6Nu'></tfoot><dl id='za6Nu'><fieldset id='za6Nu'></fieldset></dl></div>

        如何在 Swift 中格式化本地化字符串?

        How to format localised strings in Swift?(如何在 Swift 中格式化本地化字符串?)

          <i id='5bGPU'><tr id='5bGPU'><dt id='5bGPU'><q id='5bGPU'><span id='5bGPU'><b id='5bGPU'><form id='5bGPU'><ins id='5bGPU'></ins><ul id='5bGPU'></ul><sub id='5bGPU'></sub></form><legend id='5bGPU'></legend><bdo id='5bGPU'><pre id='5bGPU'><center id='5bGPU'></center></pre></bdo></b><th id='5bGPU'></th></span></q></dt></tr></i><div id='5bGPU'><tfoot id='5bGPU'></tfoot><dl id='5bGPU'><fieldset id='5bGPU'></fieldset></dl></div>
            <tbody id='5bGPU'></tbody>
          <legend id='5bGPU'><style id='5bGPU'><dir id='5bGPU'><q id='5bGPU'></q></dir></style></legend>
              <tfoot id='5bGPU'></tfoot>

                • <bdo id='5bGPU'></bdo><ul id='5bGPU'></ul>

                  <small id='5bGPU'></small><noframes id='5bGPU'>

                • 本文介绍了如何在 Swift 中格式化本地化字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在学习将我的应用本地化为简体中文.我正在关注如何做到这一点的本教程.p>

                  由于教程是基于Obj-C的,格式化字符串可以这样写:

                  "昨天你卖了 %@ 应用程序" = "Ayer le vendi&oacute; %@ aplicaciones";

                  你喜欢吗?"= "~Es bueno?~";

                  但我使用的是 Swift.而在 Swift 中,我认为你不能使用 %@ 来表示有什么东西可以放在那里.我们有字符串插值对吗?

                  我的应用有点与数学有关.我想在表格视图单元格的详细标签中显示用于计算结果的输入.例如

                  --------------1234.5678from x, y <---- 这里是详细标签--------------

                  这里,From x, y 的意思是结果是从 x 和 y 计算出来的".我想把它翻译成中文:

                  从 x, y 得出

                  以前,我只能用这个:

                  "来自 (someVariable)"

                  使用字符串文件:

                  "From" = "从得出";

                  这就是我在代码中使用它的方式

                  "(NSLocalizedString("From", comment: "")) (someVariable)"

                  但是如果用中文版的话,最终的字符串会是这样的:

                  "从得出 x, y"

                  我的意思是我可以把 放在字符串文件的两个不同的条目中.但是有没有更好的方法呢?

                  解决方案

                  可以在 Swift 的 String(format:...),可以替换通过 Swift StringNSObject 子类的任何实例.例如,如果 Localizable.strings 文件包含定义

                  "From %@, %@" = "从%@, %@ 得出";

                  然后

                  让 x = 1.2让 y = 2.4let text = String(format: NSLocalizedString("From %@, %@", comment: ""), "(x)", "(y)")//或者:let text = String(format: NSLocalizedString("From %@, %@", comment: ""), NSNumber(double: x), NSNumber(double: y))

                  产生从 1.2, 2.4 的结果".另一种选择是使用%f 双浮点数格式:

                  "From %f, %f" = "从%f, %f 得出";

                  let text = String(format: NSLocalizedString("From %f, %f", comment: ""), x, y)

                  请参阅 Niklas 的回答获得更好的解决方案,将数字表示本地化也是.

                  I am learning to localise my app to Simplified Chinese. I am following this tutorial on how to do this.

                  Because the tutorial is based on Obj-C, formatted strings can be written like this:

                  "Yesterday you sold %@ apps" = "Ayer le vendi&oacute; %@ aplicaciones";
                  

                  "You like?" = "~Es bueno?~";

                  But I am using Swift. And in Swift I don't think you can use %@ to indicate that there is something to be placed there. We have string interpolation right?

                  My app is kind of related to maths. And I want to display which input(s) is used to compute the result in a detailed label of a table view cell. For example

                  --------------
                  1234.5678
                  From x, y <---- Here is the detailed label
                  --------------
                  

                  Here, From x, y means "The result is computed from x and y". I want to translate this to Chinese:

                  从 x, y 得出
                  

                  Before, I can just use this:

                  "From (someVariable)"
                  

                  with the strings file:

                  "From" = "从 得出";
                  

                  And this is how I would use it in code

                  "(NSLocalizedString("From", comment: "")) (someVariable)"
                  

                  But if this were used in the Chinese version, the final string will be like this:

                  "从 得出 x, y"
                  

                  I mean I can put the and 得出 in two different entries in the strings file. But is there a better way to do it?

                  解决方案

                  You can use %@ in Swift's String(format:...), it can be substituted by a Swift String or any instance of a NSObject subclass. For example, if the Localizable.strings file contains the definition

                  "From %@, %@" = "从 %@, %@ 得出";
                  

                  then

                  let x = 1.2
                  let y = 2.4
                  let text = String(format: NSLocalizedString("From %@, %@", comment: ""), "(x)", "(y)")
                  // Or alternatively:
                  let text = String(format: NSLocalizedString("From %@, %@", comment: ""), NSNumber(double: x), NSNumber(double: y))
                  

                  produces "从 1.2, 2.4 得出". Another option would be to use the %f format for double floating point numbers:

                  "From %f, %f" = "从 %f, %f 得出";
                  

                  with

                  let text = String(format: NSLocalizedString("From %f, %f", comment: ""), x, y)
                  

                  See Niklas' answer for an even better solution which localizes the number representation as well.

                  这篇关于如何在 Swift 中格式化本地化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5(自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员
                    <tbody id='U94xs'></tbody>
                  <legend id='U94xs'><style id='U94xs'><dir id='U94xs'><q id='U94xs'></q></dir></style></legend>

                  <small id='U94xs'></small><noframes id='U94xs'>

                        <bdo id='U94xs'></bdo><ul id='U94xs'></ul>

                        <tfoot id='U94xs'></tfoot>

                            <i id='U94xs'><tr id='U94xs'><dt id='U94xs'><q id='U94xs'><span id='U94xs'><b id='U94xs'><form id='U94xs'><ins id='U94xs'></ins><ul id='U94xs'></ul><sub id='U94xs'></sub></form><legend id='U94xs'></legend><bdo id='U94xs'><pre id='U94xs'><center id='U94xs'></center></pre></bdo></b><th id='U94xs'></th></span></q></dt></tr></i><div id='U94xs'><tfoot id='U94xs'></tfoot><dl id='U94xs'><fieldset id='U94xs'></fieldset></dl></div>