@@ -72,6 +72,27 @@ public LibreTranslateTranslationService()
7272 apiUrl = JigasiBundleActivator .getConfigurationService ().getString (API_URL , DEFAULT_API_URL );
7373 }
7474
75+ /**
76+ * Utility function to extract the primary language code like:
77+ * 'en-GB', 'en_GB', 'enGB', 'zh-CN', 'zh-TW'
78+ *
79+ * Behaves equivalent to the function " _getPrimaryLanguageCode"
80+ * in jitsi-meet/blob/master/react/features/subtitles/middleware.ts
81+ *
82+ * @param language The language to use for translation or user requested.
83+ * @return Primary language code
84+ */
85+ private static String getPrimaryLanguageCode (String language )
86+ {
87+ // can this actually happen? If yes, avoid NPE.
88+ if (language == null )
89+ {
90+ return "auto" ;
91+ }
92+
93+ return language .replaceAll ("[-_A-Z].*" , "" );
94+ }
95+
7596 /**
7697 * {@inheritDoc}
7798 */
@@ -80,8 +101,8 @@ public String translate(String sourceText, String sourceLang, String targetLang)
80101 {
81102 String payload = "{"
82103 .concat ("\" q\" : \" " + sourceText + "\" ," )
83- .concat ("\" source\" : \" " + sourceLang . substring ( 0 , 2 ) + "\" ," )
84- .concat ("\" target\" : \" " + targetLang . substring ( 0 , 2 ) + "\" ," )
104+ .concat ("\" source\" : \" " + getPrimaryLanguageCode ( sourceLang ) + "\" ," )
105+ .concat ("\" target\" : \" " + getPrimaryLanguageCode ( targetLang ) + "\" ," )
85106 .concat ("\" format\" : \" text\" ," )
86107 .concat ("\" api_key\" : \" \" " )
87108 .concat ("}" );
0 commit comments