Search in sources :

Example 11 with LocaleQualifier

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;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ArrayList(java.util.ArrayList) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Module(com.intellij.openapi.module.Module) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with LocaleQualifier

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;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) ArrayList(java.util.ArrayList) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with LocaleQualifier

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;
}
Also used : ResourceValue(com.android.ide.common.rendering.api.ResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with LocaleQualifier

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));
}
Also used : LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier)

Example 15 with LocaleQualifier

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"));
}
Also used : LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier)

Aggregations

LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)16 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)10 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)6 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)6 CountryCodeQualifier (com.android.ide.common.resources.configuration.CountryCodeQualifier)5 KeyboardStateQualifier (com.android.ide.common.resources.configuration.KeyboardStateQualifier)5 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)5 NavigationMethodQualifier (com.android.ide.common.resources.configuration.NavigationMethodQualifier)5 NetworkCodeQualifier (com.android.ide.common.resources.configuration.NetworkCodeQualifier)5 NightModeQualifier (com.android.ide.common.resources.configuration.NightModeQualifier)5 ScreenDimensionQualifier (com.android.ide.common.resources.configuration.ScreenDimensionQualifier)5 ScreenOrientationQualifier (com.android.ide.common.resources.configuration.ScreenOrientationQualifier)5 ScreenRatioQualifier (com.android.ide.common.resources.configuration.ScreenRatioQualifier)5 ScreenSizeQualifier (com.android.ide.common.resources.configuration.ScreenSizeQualifier)5 TextInputMethodQualifier (com.android.ide.common.resources.configuration.TextInputMethodQualifier)5 TouchScreenQualifier (com.android.ide.common.resources.configuration.TouchScreenQualifier)5 UiModeQualifier (com.android.ide.common.resources.configuration.UiModeQualifier)5 Locale (com.android.tools.idea.rendering.Locale)5 NotNull (org.jetbrains.annotations.NotNull)4 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)3