10

背景

Android 4.3 增加了很多对 RTL(从右到左)语言的支持,例如希伯来语和阿拉伯语。

问题

即使有“textDirection”、“layoutDirection”和“gravity”,我也找不到通知生成器的等价物,甚至在兼容性库中也找不到。

这意味着如果同时存在希伯来语和英语单词,则顺序是错误的。例如(为了简单起见,我用英文写):

你得到的不是 "X called Y" ,而是 "Y called X" (假设 " called" 是希伯来语中的一个词),因为字符串应该采用以下格式:

<string name="notification">%1$s called %2$s</string>

注意:X 和 Y 可以是 RTL 或 LTR 字(甚至是偶数)。

要求是,在希伯来语中,右边的词应该是 X ,然后是“被召唤”这个词(当然是希伯来语),然后是左边的 Y 。正如我试图在英语类比示例中展示的那样,情况恰恰相反。

我试过的

一个。我试图搜索文档,我发现我可能需要覆盖布局,但这不是一个好的解决方案。原因:

  1. 我可能没有使用正确的 Android 样式。
  2. 对于可能使用不同样式的下一个 Android 版本,这不是未来的证明。
  3. 它不支持股票代码文本。

湾。我还尝试调查哪些特殊字符会强制文本方向不同,它通过在要显示的文本的开头和结尾添加 '\u200f' 来工作,但它有一些缺陷:

  1. 它不像其他属性那样灵活。
  2. 我不确定我使用官方的方式来处理这个问题。
  3. 每次使用通知时我都需要添加它
  4. 它对tickerText 根本不起作用。仅适用于通知,即使如此,也不适用于所有情况。

这是一个示例代码:

/** prepares a string to be shown in a notification, so that it will be shown even on RTL languages */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static String prepareNotificationText(final Context context, final String text) {
    if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR1)
        return text;
    final boolean isRTL = context.getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
    if (!isRTL)
        return text;
    return '\u200f' + text + '\u200f';
}

C。我也可以在字符串中的 '1' 和 '2' 之间切换,但这并不能处理所有情况,而且翻译人员更加困惑。

问题

有什么方法可以让通知生成器正确处理文本(对于通知和 TickerText)?

有什么方法可以在不为通知(或更改字符串)实际制作全新布局的情况下对其进行调整,这可能与 Android 的原生样式不同?

处理这种事情的官方方式是什么?

4

1 回答 1

2

好的,找到了答案,基于thisthisthis

对于上述情况:

%1$s 叫 %2$s

为了将其正确更改为希伯来语,您需要添加特殊字符 "\u200f" ,如下所示:

  <string name="notification">%1$s התקשר ל %2$s</string>


    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (VERSION.SDK_INT >= VERSION_CODES.O) {
        String id = "my_channel_01";
        CharSequence name = "channelName";// getString(R.string.channel_name);
        String description = "channelDesc";//getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(id, name, importance);
        channel.setDescription(description);
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        notificationManager.createNotificationChannel(channel);
    }
    Intent resultIntent = new Intent(this, MainActivity.class);
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    String[] names1 = new String[]{"משה", "Moses"};
    String[] names2 = new String[]{"דוד", "David"};
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            String name1, name2;
            name1 = names1[i];
            name2 = names2[j];
            name1 = "\u200f" + name1 + "\u200f";
            name2 = "\u200f" + name2 + "\u200f";

            final String text = getString(R.string.notification, name1, name2);
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this, "TEST")
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(text)
                            .setContentIntent(resultPendingIntent)
                            .setChannelId("my_channel_01")
                            .setContentText(text);
            int notificationId = i*2+j;
            notificationManager.notify(notificationId, mBuilder.build());
        }
    }
}

在选择使用此解决方案之前,您还可以检查当前语言环境是否为 RTL。例子:

public static boolean isRTL() {
    return
            Character.getDirectionality(Locale.getDefault().getDisplayName().charAt(0)) ==
                    Character.DIRECTIONALITY_RIGHT_TO_LEFT;
}

结果:

在此处输入图像描述

于 2017-08-12T10:26:50.980 回答