Search in sources :

Example 6 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class StringResourceDataTest method testAddingTranslation.

public void testAddingTranslation() {
    final Locale locale = Locale.create("en");
    final String key = "key4";
    assertNull(data.getStringResource(key).getTranslationAsResourceItem(locale));
    assertTrue(data.setTranslation(key, locale, "Hello"));
    VirtualFile file = resourceDirectory.findFileByRelativePath("values-en/strings.xml");
    assert file != null;
    XmlTag tag = getNthXmlTag(file, 4);
    assertEquals("key4", tag.getAttributeValue(SdkConstants.ATTR_NAME));
    assertEquals("Hello", tag.getValue().getText());
    assertEquals("Hello", data.getStringResource(key).getTranslationAsString(locale));
}
Also used : Locale(com.android.tools.idea.rendering.Locale) VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlTag(com.intellij.psi.xml.XmlTag)

Example 7 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class StringResourceParser method parseUnderReadLock.

@NotNull
private static StringResourceData parseUnderReadLock(AndroidFacet facet, LocalResourceRepository repository) {
    List<String> keys = Lists.newArrayList(repository.getItemsOfType(ResourceType.STRING));
    Collections.sort(keys);
    Map<String, StringResource> keyToResourceMap = new HashMap<>();
    Project project = facet.getModule().getProject();
    for (String key : keys) {
        List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
        if (items == null) {
            continue;
        }
        StringResource stringResource = new StringResource(key);
        for (ResourceItem item : items) {
            XmlTag tag = LocalResourceRepository.getItemTag(project, item);
            if (tag != null && SdkConstants.VALUE_FALSE.equals(tag.getAttributeValue(SdkConstants.ATTR_TRANSLATABLE))) {
                stringResource.setTranslatable(false);
            }
            String itemStringRepresentation = resourceToString(project, item);
            FolderConfiguration config = item.getConfiguration();
            LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
            if (qualifier == null) {
                stringResource.setDefaultValue(item, itemStringRepresentation);
            } else {
                Locale locale = Locale.create(qualifier);
                stringResource.putTranslation(locale, item, itemStringRepresentation);
            }
        }
        keyToResourceMap.put(key, stringResource);
    }
    return new StringResourceData(facet, keyToResourceMap);
}
Also used : Locale(com.android.tools.idea.rendering.Locale) HashMap(java.util.HashMap) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Project(com.intellij.openapi.project.Project) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class StringsWriteUtils method getStringResourceItem.

@Nullable
private static ResourceItem getStringResourceItem(@NotNull AndroidFacet facet, @NotNull String key, @Nullable Locale locale) {
    LocalResourceRepository repository = facet.getModuleResources(true);
    // Ensure that items *just* created are processed by the resource repository
    repository.sync();
    List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
    if (items == null) {
        return null;
    }
    for (ResourceItem item : items) {
        FolderConfiguration config = item.getConfiguration();
        LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
        if (qualifier == null) {
            if (locale == null) {
                return item;
            } else {
                continue;
            }
        }
        Locale l = Locale.create(qualifier);
        if (l.equals(locale)) {
            return item;
        }
    }
    return null;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class ConfigurationMatcher method getLocaleMatch.

private int getLocaleMatch() {
    java.util.Locale defaultLocale = java.util.Locale.getDefault();
    if (defaultLocale != null) {
        String currentLanguage = defaultLocale.getLanguage();
        String currentRegion = defaultLocale.getCountry();
        List<Locale> localeList = myManager.getLocales();
        final int count = localeList.size();
        for (int l = 0; l < count; l++) {
            Locale locale = localeList.get(l);
            LocaleQualifier qualifier = locale.qualifier;
            // test easy.
            if (qualifier.getLanguage().equals(currentLanguage) && (qualifier.getRegion() == null || qualifier.getRegion().equals(currentRegion))) {
                return l;
            }
        }
        // If no exact region match, try to just match on the language
        for (int l = 0; l < count; l++) {
            Locale locale = localeList.get(l);
            LocaleQualifier qualifier = locale.qualifier;
            // test easy.
            if (qualifier.getLanguage().equals(currentLanguage)) {
                return l;
            }
        }
    }
    // getPrioritizedLocales()
    return 0;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) java.util(java.util)

Example 10 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class ConfigurationMatcher method getPrioritizedLocales.

/** Like {@link ConfigurationManager#getLocales()}, but ensures that the currently selected locale is first in the list */
@NotNull
public List<Locale> getPrioritizedLocales() {
    List<Locale> projectLocales = myManager.getLocales();
    // Locale.ANY is not in getLocales() list
    List<Locale> locales = new ArrayList<Locale>(projectLocales.size() + 1);
    Locale current = myManager.getLocale();
    locales.add(current);
    for (Locale locale : projectLocales) {
        if (!locale.equals(current)) {
            locales.add(locale);
        }
    }
    return locales;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Locale (com.android.tools.idea.rendering.Locale)19 NotNull (org.jetbrains.annotations.NotNull)7 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)5 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 XmlTag (com.intellij.psi.xml.XmlTag)4 IAndroidTarget (com.android.sdklib.IAndroidTarget)3 Device (com.android.sdklib.devices.Device)3 State (com.android.sdklib.devices.State)3 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)3 Module (com.intellij.openapi.module.Module)3 ResourceItem (com.android.ide.common.res2.ResourceItem)2 ArrayList (java.util.ArrayList)2 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 DefaultActionGroup (com.intellij.openapi.actionSystem.DefaultActionGroup)1 Project (com.intellij.openapi.project.Project)1 java.util (java.util)1 HashMap (java.util.HashMap)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 Nullable (org.jetbrains.annotations.Nullable)1