code: static DetectLanguageResponse detectLanguageOfText(
        String projectId, String location, String text) {
  
      try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
  
        LocationName locationName =
            LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        DetectLanguageRequest detectLanguageRequest =
            DetectLanguageRequest.newBuilder()
                .setParent(locationName.toString())
                .setMimeType("text/plain")
                .setContent(text)
                .build();
  
        // Call the API
        DetectLanguageResponse response =
            translationServiceClient.detectLanguage(detectLanguageRequest);
        System.out.format("Detected Language Code: %s", response.getLanguages(0).getLanguageCode());
        return response;
  
      } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
      }
    }
docs: [START translate_detect_language_beta]