Skip to content

try to detect yaml translation files also on language pattern #1955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.indexing.*;
Expand All @@ -18,6 +19,7 @@
import fr.adrienbrault.idea.symfony2plugin.util.ProjectUtil;
import gnu.trove.THashMap;
import kotlin.Pair;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -28,6 +30,8 @@
import java.io.InputStream;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
Expand Down Expand Up @@ -98,18 +102,40 @@ public Map<String, Set<String>> map(@NotNull FileContent inputData) {
}

private boolean isValidTranslationFile(@NotNull FileContent inputData) {
String fileName = inputData.getFileName();

// every direct match
if (fileName.contains("+intl-icu") || fileName.startsWith("messages.") || fileName.startsWith("validators.")) {
return true;
}

VirtualFile file = inputData.getFile();
String name = file.getNameWithoutExtension();

// unknown-2.fr.yml
Matcher matcher = Pattern.compile("^.*\\.([\\w]{2})$").matcher(name);
if (matcher.find()) {
return ArrayUtils.contains(Locale.getISOLanguages(), matcher.group(1));
}

// unknown-3.sr_Cyrl.yml
Matcher matcher2 = Pattern.compile("^.*\\.([\\w]{2})_[\\w]{2,4}$").matcher(name);
if (matcher2.find()) {
return ArrayUtils.contains(Locale.getISOLanguages(), matcher2.group(1));
}

// dont index all yaml files; valid:
// - "Resources/translations"
// - "translations/[.../]foo.de.yml"
String relativePath = VfsUtil.getRelativePath(inputData.getFile(), ProjectUtil.getProjectDir(inputData.getProject()), '/');
String relativePath = VfsUtil.getRelativePath(file, ProjectUtil.getProjectDir(inputData.getProject()), '/');
if (relativePath != null) {
return relativePath.contains("/translations") || relativePath.startsWith("translations/");
String replace = relativePath.replace("\\", "/");
return replace.contains("/translations") || replace.startsWith("translations/");
}

// Resources/translations/messages.de.yml
// @TODO: Resources/translations/de/messages.yml
String path = inputData.getFile().getPath();
return path.endsWith("/translations/" + inputData.getFileName());
String path = file.getPath();
return path.replace("\\", "/").endsWith("/translations/" + fileName);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package fr.adrienbrault.idea.symfony2plugin.tests.stubs.indexes;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.Processor;
import com.intellij.util.containers.ArrayListSet;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.twig.TwigFileType;
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TranslationStubIndex;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -36,12 +42,35 @@ public void setUp() throws Exception {

myFixture.copyFileToProject("fr.xlf", "+intl-icu.fr.xlf");
myFixture.copyFileToProject("fr.xlf", " .fr.xlf");

myFixture.copyFileToProject("apple.de.yml", "unknown/unknown-1+intl-icu.fr.yml");
myFixture.copyFileToProject("apple.de.yml", "unknown/messages.ar.yml");
myFixture.copyFileToProject("apple.de.yml", "unknown/validators.ar.yml");
myFixture.copyFileToProject("apple.de.yml", "unknown/unknown-2.fr.yml");
myFixture.copyFileToProject("apple.de.yml", "unknown/unknown-3.sr_Cyrl.yml");
myFixture.copyFileToProject("apple.de.yml", "unknown/unknown-3.sr_Cyrla.yml");
myFixture.copyFileToProject("apple.de.yml", "unknown/unknown-3.xx.yml");
myFixture.copyFileToProject("apple.de.yml", "unknown/unknown-3.yml");
}

public String getTestDataPath() {
return "src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures";
}

public void testNamingMatchingForTranslations() {
Set<String> filenames = new HashSet<>();

for (String key : new String[]{"unknown-1", "unknown-2", "unknown-3", "validators", "messages"}) {
FileBasedIndex.getInstance().getFilesWithKey(TranslationStubIndex.KEY, new HashSet<>(List.of(key)), virtualFile -> {
filenames.add(virtualFile.getName());
return true;
}, GlobalSearchScope.allScope(getProject()));
}

assertContainsElements(filenames, "unknown-1+intl-icu.fr.yml", "messages.ar.yml", "validators.ar.yml", "unknown-2.fr.yml", "unknown-3.sr_Cyrl.yml");
assertDoesntContain(filenames, "unknown-3.sr_Cyrla.yml", "unknown-3.xx.yml", "unknown-3.yml");
}

public void testThatDomainFromFileIsExtracted() {
assertIndexContains(TranslationStubIndex.KEY, "foo.bar");
assertIndexNotContains(TranslationStubIndex.KEY, "");
Expand Down