1 /* 2 * Copyright (C) 2007 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 android.content.res; 18 19 import java.util.Locale; 20 21 /* 22 * Yuck-o. This is not the right way to implement this. When the ICU PluralRules 23 * object has been integrated to android, we should switch to that. For now, yuck-o. 24 */ 25 26 abstract class PluralRules { 27 28 static final int QUANTITY_OTHER = 0x0000; 29 static final int QUANTITY_ZERO = 0x0001; 30 static final int QUANTITY_ONE = 0x0002; 31 static final int QUANTITY_TWO = 0x0004; 32 static final int QUANTITY_FEW = 0x0008; 33 static final int QUANTITY_MANY = 0x0010; 34 35 static final int ID_OTHER = 0x01000004; 36 37 abstract int quantityForNumber(int n); 38 39 final int attrForNumber(int n) { 40 return PluralRules.attrForQuantity(quantityForNumber(n)); 41 } 42 43 static final int attrForQuantity(int quantity) { 44 // see include/utils/ResourceTypes.h 45 switch (quantity) { 46 case QUANTITY_ZERO: return 0x01000005; 47 case QUANTITY_ONE: return 0x01000006; 48 case QUANTITY_TWO: return 0x01000007; 49 case QUANTITY_FEW: return 0x01000008; 50 case QUANTITY_MANY: return 0x01000009; 51 default: return ID_OTHER; 52 } 53 } 54 55 static final String stringForQuantity(int quantity) { 56 switch (quantity) { 57 case QUANTITY_ZERO: 58 return "zero"; 59 case QUANTITY_ONE: 60 return "one"; 61 case QUANTITY_TWO: 62 return "two"; 63 case QUANTITY_FEW: 64 return "few"; 65 case QUANTITY_MANY: 66 return "many"; 67 default: 68 return "other"; 69 } 70 } 71 72 static final PluralRules ruleForLocale(Locale locale) { 73 String lang = locale.getLanguage(); 74 if ("cs".equals(lang)) { 75 if (cs == null) cs = new cs(); 76 return cs; 77 } 78 else { 79 if (en == null) en = new en(); 80 return en; 81 } 82 } 83 84 private static PluralRules cs; 85 private static class cs extends PluralRules { 86 int quantityForNumber(int n) { 87 if (n == 1) { 88 return QUANTITY_ONE; 89 } 90 else if (n >= 2 && n <= 4) { 91 return QUANTITY_FEW; 92 } 93 else { 94 return QUANTITY_OTHER; 95 } 96 } 97 } 98 99 private static PluralRules en; 100 private static class en extends PluralRules { 101 int quantityForNumber(int n) { 102 if (n == 1) { 103 return QUANTITY_ONE; 104 } 105 else { 106 return QUANTITY_OTHER; 107 } 108 } 109 } 110 } 111 112