1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.utils; 18 19 import android.icu.text.ListFormatter; 20 import android.text.TextUtils; 21 22 import java.util.List; 23 import java.util.Locale; 24 25 /** 26 * This class implements some common methods to process with locales 27 */ 28 public class LocaleUtils { 29 30 /** 31 * Returns a character sequence concatenating the items with the localized comma. 32 * 33 * @param items items to be concatenated 34 */ getConcatenatedString(List<CharSequence> items)35 public static CharSequence getConcatenatedString(List<CharSequence> items) { 36 final ListFormatter listFormatter = ListFormatter.getInstance(Locale.getDefault()); 37 final CharSequence lastItem = items.get(items.size() - 1); 38 items.add("fake last item"); 39 40 // For English with "{0}, {1}, and {2}", the pattern is "{0}, {1}, and {2}". 41 // To get "{0}, {1}, {2}", we add a {fake item}, then the pattern result would be 42 // "{0}, {1}, {2} and {fake item}", then get the substring with the end index of the 43 // last item. 44 final String formatted = listFormatter.format(items); 45 return formatted.subSequence(0, TextUtils.indexOf(formatted, lastItem) + lastItem.length()); 46 } 47 } 48