Android 上的日语字符看起来像中文
前言:自 API 17 (Android 4.2) 起,有一个方法 TextView.setTextLocale()
可以显式解决 TextView 和派生类的此问题。指定日语区域设置 (Locale.JAPAN
),Unihan 字符将显示为日语。
我在 Android 上有一个应用程序,可以在 WebView 和 TextView 中显示日语文本。按照惯例,有些汉字(汉字)在中国和日本看起来不同,但共享相同的 Unicode 代码点。通常,浏览器将依赖 lang
标签来选择正确的字形。在Android上,它们都默认为中国形状,而我想要日本形状。
这篇文章很好地解释了这个问题。这篇文章也完美地说明了问题——在Android(最高2.2)上观看时,“依赖语言的字符示例”中的字符看起来都一样,而且是中文。
使用 lang="ja"
属性没有帮助。将整个系统区域设置切换为日语也没有帮助。
我想知道在日本销售的 Android 手机的情况。像“直”、“今”、“化”这样的字在上面看起来也有中国风格吗?我假设不会。
那么问题是:有 Android 官方本地化镜像吗?我可以在模拟器上运行一个吗? DroidSansFallback 字体仍然是其中唯一支持 CJK 的字体吗?如果是的话,它与普通美国 Android 上的一样吗?
我有点希望日语字形隐藏在字体深处的某个地方(Unicode 私有区域或其他东西)。如果是这样,我可以利用它们...
编辑:找到 DroidSansJapan.ttf,通过复制到 /system/fonts 将其安装在模拟器上,然后重新启动。这对 Unihan 文章的外观没有影响。甚至日语文本输入的提示区域(应该更了解)也显示得像中文一样。
我如何知道 DroidSansJapan.ttf 的字体名称?我有一种感觉,它仍然是 Droid Sans,与内置的 DroidSansFallback 字体相同。但如果它们包含相同的字体,那么什么决定哪一个应该优先呢?人们可能会认为 - 系统区域设置,但显然不是。 Android中的字体是通过复制安装的吧?
PREAMBLE: since API 17 (Android 4.2), there's a method TextView.setTextLocale()
that explicitly solves this problem for TextViews and derived classes. Assign a Japanese locale (Locale.JAPAN
), and Unihan characters will look Japanese.
I have an application on Android that displays Japanese text in WebViews and TextViews. There are some Chinese characters (kanji) that look, by convention, differently in China and in Japan, but share the same Unicode codepoint. Normally, the browser would rely upon the lang
tag to choose the correct glyph. On Android, they all default to their Chinese shapes, and I want Japanese shapes.
The problem is well explained in this article. This article also serves as a perfect illustration of the problem - when watched on Android (up to 2.2), the characters in the "Examples of language-dependent characters" all look the same, and Chinese.
Using the lang="ja"
attribute does not help. Switching the whole system locale to Japanese does not help either.
I'm wondering about Android phones that are sold in Japan. Do characters like 直, 今, 化 look Chinese-style on those, too? I'm assuming not.
So the questions are: are there official localized images of Android out there? Can I get one to run on the emulator? Is the DroidSansFallback font still the only CJK-enabled font on those? And if it is, is it the same as on the vanilla USA Android?
I'm kind of hoping that the Japanese glyphs are hidden somewhere deep in the font (Unicode private area or something). If so, I could leverage them...
EDIT: located DroidSansJapanese.ttf, installed it on the emulator by copying into /system/fonts, restarted. It made no difference on the look of the Unihan article. Even the hint area of the Japanese text input (which should know better) displays as if Chinese.
How do I know the typeface name of the DroidSansJapanese.ttf? I have a feeling it's still Droid Sans, same as in the built-in DroidSansFallback font. But if they contain the same typeface, what governs which one should take precedence? One would think - system locale, but apparently not. Fonts in Android are installed just by copying, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有些字体完全支持日语。我听过一些人谈论 DroidSansJapan.tff 和 TakaoPGothic。
There are fonts with full Japanese support. I've heard some people talking about DroidSansJapanese.tff and TakaoPGothic.
找到了一个有点笨拙的解决方案。
DroidSansJapan.ttf 字体存在并且可供下载,例如 此处。我下载了它,将其重命名为 DroidSansJapan.mp3 以避免 1MB 压缩资源限制,并将其放置在
assets/web
下。然后,我引入了以下 CSS 语句:然后,我将“DroidJP”添加到每个相关 CSS 样式的
font-family
中。在我加载 HTML 的方式中,assets/web 文件夹已被指定为加载链接内容的基础。经过仔细检查,我发现应用程序中的几个地方
TextView
中有日语。对于这些,我已经加载了这样的字体:并在每个相关的 TextView 上调用
setTypeface()
。现在为了利润!这使我的 APK 扩大了约 1 MB。还有一种替代方法,我可以将字体以压缩形式存储在资源中 - 这可以为我节省大约 500 KB 的 APK 大小,但是我必须在第一次运行时将其扩展到手机内存,担心关于数据文件夹路径,并失去与 Android 1.5 的兼容性。
一些功劳应归功于:此处和此处。不适用于 Android 2.1(WebView,而不是 TextView) - 这是 已知错误。
现在,问题仍然存在 - 如何识别默认字体已包含日语形状的设备?
编辑回复:mp3 hack。我首先实现了分块解决方案,但后来决定在资源中使用字体。分块方法有一个优点 - 较小的 APK 大小 - 以及以下缺点:
Typeface.createFromFile()
在 API 级别 4 中引入 使将字体存储为压缩资源也不是一个选项 - 然后字体不会显示在 WebView 中,并且您可以在 LogCat 中清楚地看到有关“数据超出 UNCOMPRESS_DATA_MAX”的消息。
Found a somewhat cludgey solution.
The DroidSansJapanese.ttf font exists and is available for download, for example, here. I downloaded it, renamed it to DroidSansJapanese.mp3 to avoid the 1MB compressed asset limit, and placed it under
assets/web
. I then introduced the following CSS statement:Then I added 'DroidJP' to the
font-family
of every relevant CSS style. The way I load my HTML, theassets/web
folder is already designated as the base for loading linked content.Upon careful examination, I found several places in the app where Japanese was in
TextView
s. For those, I've loaded the typeface like this:and called
setTypeface()
on every relevant TextView. Now for PROFIT!This expanded my APK by about 1 MB. There was an alternative way, where I'd store the font in the assets in compressed form - that'd save me about 500 KB of APK size, but then I'd have to expand it to phone memory on the first run, worry about data folder path, and lose compatibility with Android 1.5.
Some credit is due: here and here. Does not work on Android 2.1 (the WebViews, not the TextViews) - that's a known bug.
Now, the question remains - how do I identify devices where the default font already contains the Japanese shapes?
EDIT re: the mp3 hack. I've implemented the chunked solution at first, but then decided to go with font in the assets. The chunked approach has one upside - smaller APK size - and the following downsides:
Typeface.createFromFile()
was introduced in API level 4Storing the font as a compressed asset is not an option either - the font then doesn't show up in WebView, and you can clearly see in LogCat the message about "Data exceeds UNCOMPRESS_DATA_MAX".
在 onCreate/OnCreateView 中使用以下内容:
然后对于文本视图:
请注意,并非 100% 的所有字体都会在 Android 中显示。对于日语字符,我没有看到太多问题,但无论您使用什么 ttf 文件,其他 CJK 字符都可能显示为空白框。
Using the following in your onCreate/OnCreateView:
And then for your textviews:
Be aware that not 100% of all fonts will be displayed in Android. I haven't seen too many problems for Japanese characters, but other CJK characters may appear as blank boxes, no matter what ttf file you use.