A federal judge has ruled that the Trump administration violated the law by failing to promptly resolve visa applications for thousands of Afghans and Iraqis who worked for American troops and diplomats, and ordered the government to fix the delays.

U.S. District Court Judge Tanya Chutkan of Washington, D.C., said the government offered no convincing explanation why it has failed to abide by 2013 legislation requiring authorities to deliver a decision on visa applications for Afghans and Iraqis within nine months. Instead, many applicants — who risked their lives working for U.S. troops or other government agencies — have had to wait for several years to get an answer on their visa requests, the court said in the ruling handed down on Friday.

"This ruling could finally bring relief to these men and women and their families who have been waiting in fear for far too long," said Deepa Alagesan of the International Refugee Assistance Project, one of the lawyers who argued the case.

"They served bravely in support of our missions abroad, and we promised them a pathway to safety in return. This ruling ensures that we keep our promise," Alagesan said.

Calling the government's arguments "untenable," the court ordered the Trump administration to produce a plan within 30 days on how it plans to remedy the problem with the visa applications.

A State Department official, who was not authorized to speak on the record, said, "We are aware of the court’s decision and are reviewing with our colleagues at the U.S. Department of Justice."

The ruling against the administration came as the White House weighs how many refugees — including former Iraqi interpreters — to admit to the U.S. in the next fiscal year starting in October. The Pentagon has argued against further reducing refugee admissions that are already at historic lows, and cited the plight of Iraqis who worked with U.S. soldiers and diplomats at great personal risk.

The Special Immigrant Visa (SIV) program was set up to help Iraqis and Afghans who worked for U.S. troops or diplomats to resettle in the U.S. But the program has been plagued by delays in both Republican and Democratic administrations. Frustrated lawmakers introduced a law in 2013 ordering the State Department and other agencies to process visa applications within nine months and to report back to Congress on any reason for delay.

"It is clear that Congress did not intend to give Defendants (U.S. government) an unbounded, open-ended timeframe in which to adjudicate SIV applications," the federal judge said in her ruling.

Administration lawyers had argued in court that evaluating visa applications was a complex task and officials had to weigh the need to safeguard national security and not only the deadline imposed by Congress.

But Judge Chutkan dismissed the administration's argument, saying the law adopted by Congress recognized the complexity of the process and that it allowed for exceptions in cases deemed to pose a "high risk."

The judge wrote that "the fact that the adjudication process is a complex one does not excuse delays of such magnitude, because Congress explicitly referenced that complexity in the 9-month provision."

Documents revealed during the course of the case showed virtually all SIV applicants have faced delays beyond the nine-month limit for an answer on their visas and that an applicant can expect about a four-year wait.

The class action suit was filed last year by a group of five unnamed Afghans and Iraqis against Secretary of State Michael Pompeo as well as officials leading the Department of Homeland Security and U.S. Citizenship and Immigration Services. The plaintiffs had said they were forced to flee their homes and faced death threats from militants due to their work with the U.S. government.

Ryan Crocker, a retired senior diplomat and former ambassador to both Afghanistan and Iraq, filed a brief in support of the Iraqi and Afghan plaintiffs, saying failure to resolve visa applications damaged the country's national security interests.

"The perception that our country cannot or will not honor its promises to those who have risked literally everything to help our soldiers and diplomats is by itself an irreparable harm to our operational capacities," Crocker wrote. "Moreover, those perceptions have already become a propaganda tool that will certainly risk diminishing our ability to recruit allies in future conflicts."

The number of SIV visas being issued has declined under the Trump administration, and refugee advocates believe it's a result of wider restrictions on immigration and refugee admissions.

After 2014, Iraqis could no longer qualify for the SIV program and now have to apply as refugees. Last year, the U.S. government only admitted two former Iraqi interpreters, despite a backlog of tens of thousands, NBC News previously reported.

Members of Congress have accused the administration of rebuffing requests for information on why applicants are in limbo, and several lawmakers have raised questions about the case of one Afghan interpreter, Muhammad Kamran, who has been denied a visa despite facing death threats.