use of com.android.ide.common.resources.configuration.LocaleQualifier in project android by JetBrains.
the class LocaleMenuAction method getRelevantLocales.
/**
* Like {@link ConfigurationManager#getLocales} but filters out locales not compatible
* with language and region qualifiers in the current configuration's folder config
*
* @return the list of relevant locales in the project
*/
@NotNull
private List<Locale> getRelevantLocales() {
List<Locale> locales = new ArrayList<Locale>();
Configuration configuration = myRenderContext.getConfiguration();
if (configuration == null) {
return Collections.emptyList();
}
Module module = configuration.getConfigurationManager().getModule();
LocaleQualifier specificLocale = configuration.getEditedConfig().getLocaleQualifier();
// locale.
if (specificLocale != null) {
List<VirtualFile> variations = ResourceHelper.getResourceVariations(configuration.getFile(), false);
for (VirtualFile variation : variations) {
FolderConfiguration config = FolderConfiguration.getConfigForFolder(variation.getParent().getName());
if (config != null && config.getLocaleQualifier() == null) {
specificLocale = null;
break;
}
}
}
LocalResourceRepository projectResources = ProjectResourceRepository.getProjectResources(module, true);
Set<LocaleQualifier> languages = projectResources != null ? projectResources.getLocales() : Collections.<LocaleQualifier>emptySet();
for (LocaleQualifier l : languages) {
if (specificLocale != null && !specificLocale.isMatchFor(l)) {
continue;
}
locales.add(Locale.create(l));
}
return locales;
}
use of com.android.ide.common.resources.configuration.LocaleQualifier in project android by JetBrains.
the class LocaleMenuAction method getAllLocales.
@NotNull
public static List<Locale> getAllLocales() {
List<String> sorted = LocaleManager.getLanguageCodes(true);
List<Locale> locales = new ArrayList<Locale>(sorted.size());
for (String language : sorted) {
LocaleQualifier qualifier = new LocaleQualifier(language.length() == 2 ? language : BCP_47_PREFIX + language, language, null, null);
Locale locale = Locale.create(qualifier);
locales.add(locale);
}
return locales;
}
use of com.android.ide.common.resources.configuration.LocaleQualifier in project android by JetBrains.
the class InlinedResource method getResolvedString.
@Nullable
public String getResolvedString() {
if (myResourceRepository != null) {
if (myResourceRepository.hasResourceItem(myType, myKey)) {
FolderConfiguration referenceConfig = new FolderConfiguration();
// Nonexistent language qualifier: trick it to fall back to the default locale
referenceConfig.setLocaleQualifier(new LocaleQualifier("xx"));
ResourceValue value = myResourceRepository.getConfiguredValue(myType, myKey, referenceConfig);
if (value != null) {
String text = value.getValue();
if (text != null) {
if (myElement instanceof PsiMethodCallExpression) {
text = insertArguments((PsiMethodCallExpression) myElement, text);
}
if (myType == ResourceType.PLURALS && text.startsWith(STRING_PREFIX)) {
value = myResourceRepository.getConfiguredValue(ResourceType.STRING, text.substring(STRING_PREFIX.length()), referenceConfig);
if (value != null && value.getValue() != null) {
text = value.getValue();
return '"' + StringUtil.shortenTextWithEllipsis(text, FOLD_MAX_LENGTH - 2, 0) + '"';
}
}
if (myType == ResourceType.STRING || myElement instanceof XmlAttributeValue) {
return '"' + StringUtil.shortenTextWithEllipsis(text, FOLD_MAX_LENGTH - 2, 0) + '"';
} else if (text.length() <= 1) {
// This is similar to how IntelliJ 14 handles call parameters
return myKey + ": " + text;
} else {
return StringUtil.shortenTextWithEllipsis(text, FOLD_MAX_LENGTH, 0);
}
}
}
}
}
return null;
}
use of com.android.ide.common.resources.configuration.LocaleQualifier in project android by JetBrains.
the class RestrictedLocaleTest method testIntersect.
/**
* Tests {@link RestrictedLocale#intersect(RestrictedQualifier)}
*/
public void testIntersect() {
RestrictedLocale first = new RestrictedLocale();
RestrictedLocale second = new RestrictedLocale();
RestrictedLocale expected = new RestrictedLocale();
first.setRestrictions(null, ImmutableList.<ResourceQualifier>of(new LocaleQualifier("kz")));
second.setRestrictions(null, ImmutableList.<ResourceQualifier>of(new LocaleQualifier("en")));
expected.setRestrictions(null, ImmutableList.<ResourceQualifier>of(new LocaleQualifier("en"), new LocaleQualifier("kz")));
assertEquals(expected, first.intersect(second));
first = new RestrictedLocale();
second = new RestrictedLocale();
expected = new RestrictedLocale();
first.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
second.setRestrictions(null, ImmutableList.<ResourceQualifier>of(new LocaleQualifier("kz"), new LocaleQualifier("ru")));
expected.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
assertEquals(expected, first.intersect(second));
first = new RestrictedLocale();
second = new RestrictedLocale();
expected = new RestrictedLocale();
first.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
second.setRestrictions(null, ImmutableList.<ResourceQualifier>of(new LocaleQualifier("kz"), new LocaleQualifier("ru")));
expected.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
assertEquals(expected, first.intersect(second));
assertEquals(expected, second.intersect(first));
first = new RestrictedLocale();
second = new RestrictedLocale();
first.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
second.setRestrictions(null, ImmutableList.<ResourceQualifier>of(new LocaleQualifier("kz"), new LocaleQualifier("en")));
assertEquals(null, first.intersect(second));
first = new RestrictedLocale();
second = new RestrictedLocale();
expected = new RestrictedLocale();
first.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
second.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
expected.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
assertEquals(expected, first.intersect(second));
first = new RestrictedLocale();
second = new RestrictedLocale();
first.setRestrictions(new LocaleQualifier("en"), ImmutableList.<ResourceQualifier>of());
second.setRestrictions(new LocaleQualifier("kz"), ImmutableList.<ResourceQualifier>of());
assertEquals(null, first.intersect(second));
}
use of com.android.ide.common.resources.configuration.LocaleQualifier in project android by JetBrains.
the class LocaleTest method test.
public void test() {
LocaleQualifier qualifier1 = LocaleQualifier.getQualifier("nb");
LocaleQualifier qualifier2 = LocaleQualifier.getQualifier("no");
LocaleQualifier qualifier3 = LocaleQualifier.getQualifier("nb-rNO");
LocaleQualifier qualifier4 = LocaleQualifier.getQualifier("nb-rSE");
LocaleQualifier qualifier5 = LocaleQualifier.getQualifier("no-rSE");
assertNotNull(qualifier1);
assertNotNull(qualifier2);
assertNotNull(qualifier3);
assertNotNull(qualifier4);
assertNotNull(qualifier5);
assertEquals(Locale.ANY, Locale.ANY);
assertFalse(Locale.ANY.hasLanguage());
assertFalse(Locale.ANY.hasRegion());
//noinspection ConstantConditions
assertFalse(Locale.create(new LocaleQualifier(LocaleQualifier.FAKE_VALUE)).hasLanguage());
//noinspection ConstantConditions
assertFalse(Locale.create(new LocaleQualifier(LocaleQualifier.FAKE_VALUE)).hasRegion());
assertEquals(Locale.create(qualifier1), Locale.create(qualifier1));
assertTrue(Locale.create(qualifier1).hasLanguage());
assertFalse(Locale.create(qualifier1).hasRegion());
assertTrue(Locale.create(qualifier3).hasLanguage());
assertTrue(Locale.create(qualifier3).hasRegion());
assertEquals(Locale.create(qualifier3), Locale.create(qualifier3));
assertEquals(Locale.create(qualifier1), Locale.create(qualifier1));
assertTrue(Locale.create(qualifier1).equals(Locale.create(qualifier1)));
assertTrue(Locale.create(qualifier3).equals(Locale.create(qualifier3)));
assertFalse(Locale.create(qualifier3).equals(Locale.create(qualifier4)));
assertFalse(Locale.create(qualifier1).equals(Locale.create(qualifier3)));
assertFalse(Locale.create(qualifier1).equals(Locale.create(qualifier2)));
assertFalse(Locale.create(qualifier3).equals(Locale.create(qualifier5)));
assertEquals("nb", Locale.create(qualifier1).toString());
assertEquals("nb-NO", Locale.create(qualifier3).toString());
assertEquals(Locale.create(qualifier1), Locale.create("b+nb"));
assertEquals(Locale.create(qualifier3), Locale.create("b+nb+NO"));
}
Aggregations