如何以本地化方式改变 Android 的 XML 布局?
您可能知道,Mozilla 的目标是在 fennec 上将移动 UI 作为原生 Java/Android UI 进行设计。
这包括使用布局 XML 文件,默认情况下使用诸如
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/text_a"
/>
@string/text_a
之类的内容在 strings.xml
文件中解析为 text_a
,做本地化。
我们想使用其他东西,我想知道我们是否/如何将其连接到 XML 文件中。
因此,我很想获得指向实际执行字符串查找的 android 源代码的指针。
我对如何连接一些自定义的东西有三个想法,这些想法没有经过事实检查,基本上是因为我还没有找到可以做这些事情的代码:
@moz-l10n/text_a
,使用 java 实现的服务返回该资源类型moz:l10n="text_a"
自定义属性的值,该属性将连接到后处理生成的小部件,- 将我们想要使用本地化的小部件子类化方案,添加(2)
我希望这里有人有好主意,可以为我指出好的路径或击落一些路径。
PS:我希望没有关于 android l10n 方案是否好用的自行车棚。
As you probably know, Mozilla aims to do the mobile UI on fennec as native Java/Android UI.
That includes using the layout XML files, which by default use stuff like
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/text_a"
/>
with @string/text_a
being resolved to text_a
in a strings.xml
file, doing l10n.
We'd like to use something else, and I wonder if/how we can hook that up in the XML files.
So, I'd love to get pointers to android source code that actually does the string lookup, for one.
And I have three ideas on how to hook up something custom, which are not fact-checked, basically because I haven't managed to find the code that does stuff yet:
@moz-l10n/text_a
, with a java-implemented service giving back values for that resource typemoz:l10n="text_a"
custom attribute that would get hooked up to post process the generated widgets- subclass the widgets we want to localize with our scheme, adding (2)
I hope that there are folks out here that have a good idea to point me to good paths or shoot some down.
PS: I'd appreciate a lack of bike-shed about whether android l10n scheme is good or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
android.content.res.Resources
委托给android.content.res.AssetManager
和getResourceText()
方法。这又深入到本机loadResourceValue()
方法。从那里你就只能靠自己了......:-)除非您打算使用自己的构建工具预处理人造资源文件,否则将有效的 Android 资源文件生成到
res/
目录中,您无法发明新的资源类型(例如,@moz-10n
)。这需要修改构建工具和固件。您的选项 #3 绝对是可能的,并且在以下情况下相当典型创建自定义小部件。可以想象,它的技术(通常涉及带有
declare-styleable
资源的res/values/attrs.xml
文件)可以以某种方式应用于标准小部件类,但我从来没有见过这样做。当然,您始终可以按照完成选项 #1 的方式进行预处理。android.content.res.Resources
delegates toandroid.content.res.AssetManager
and thegetResourceText()
method. That in turn dives into a nativeloadResourceValue()
method. And you're on your own from there... :-)Unless you are going to pre-process your faux resource files with your own build tools, generating valid Android resource files into the
res/
directory, you cannot invent new resource types (e.g.,@moz-10n
). That would require modifications to the build tools and the firmware.Your option #3 is definitely possible and is fairly typical when creating custom widgets. It's conceivable that the techniques for it (usually involving a
res/values/attrs.xml
file with adeclare-styleable
resource) could somehow be applied to a standard widget class, but I've never seen that done. Of course, you could always do the pre-processing as in how you'd accomplish option #1.