如何处理混合RTL和通知中的LTR语言? [英] How to handle mixed RTL & LTR languages in notifications?

查看:110
本文介绍了如何处理混合RTL和通知中的LTR语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 4.3添加了对RTL(从右至左)语言的大量支持,例如希伯来语和阿拉伯语.

Android 4.3 has added a lot of support for RTL (Right-To-Left) languages, such as Hebrew and Arabic.

即使有"textDirection","layoutDirection"和"gravity",我也找不到通知生成器的等效项,即使在兼容性库中也是如此.

Even though there is "textDirection", "layoutDirection" and "gravity", I can't find the equivalents for the notification builder, not even in the compatibility library.

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

This means that if there are Hebrew and English words together, the order is wrong. For example (and I write in English for simplicity) :

您将获得"Y称为X"(假设被叫"是希伯来语中的单词),而不是"X被称为Y",因为该字符串应采用以下格式:

Instead of "X called Y" , you get "Y called X" (suppose "called" is a word in Hebrew), as the string is supposed to be in this format:

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

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

Note: X and Y can be either RTL or LTR words (and even numbers).

要求是在希伯来语中,右侧的单词应为X,然后是被叫"(当然是希伯来语),然后在左侧是Y.正如我试图在英语类比示例中显示的那样,情况恰恰相反.

The requirements is that in Hebrew, the word on the right should be X , then the word "called" (but in Hebrew, of course), and then Y on the left. As I've tried to show in the English analogy example, it's the opposite.

a.我尝试搜索文档,发现所有内容可能都需要覆盖布局,但这不是一个好的解决方案.原因:

a. I've tried to search the documentation, and all I've found is that I will probably need to override the layout, but that's not a good solution. The reasons:

  1. 我可能没有使用正确的Android样式.
  2. 对于未来的Android版本(可能会使用其他样式),这并不是未来的证明.
  3. 它不支持股票代码.

b.我还尝试研究了哪些特殊字符将迫使文本方向不同,并且通过在显示的文本的开头和结尾添加'\ u200f'来起作用,但是存在一些缺陷:

b. I've also tried to investigate which special characters will force the text direction to be different, and it worked by adding '\u200f' to the beginning and end of the text to show, but it has a few flaws:

  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"之间切换,但这并不能处理所有情况,而且翻译人员会更加困惑.

c. I could also switch between the '1' and '2' in the string, but this doesn't handle all cases, plus it's even more confusing to the translators.

有什么方法可以使通知构建器正确处理文本(对于通知和TickerText)?

Is there any way to make the notification builder handle texts correctly (for both notifications and TickerText) ?

在没有为通知(或更改字符串)实际制作全新布局的情况下进行任何调整的方式可能不是Android的本机样式吗?

Any way to tweak it without actually making totally new layouts for the notifications (or change strings), which might not be in the same native style of Android ?

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

What's the official way to handle such a thing?

推荐答案

好,根据

OK, found an answer, based on this, this and this.

对于上述情况:

%1 $ s称为%2 $ s

%1$s called %2$s

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

In order to change it correctly to Hebrew, you need to add the special character "\u200f" , as such:

  <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.示例:

You can also check if the current locale is RTL, before choosing to use this solution. Example:

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

结果:

这篇关于如何处理混合RTL和通知中的LTR语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆