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));
}
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);
}
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;
}
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;
}
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;
}
Aggregations