{"record":{"id":"0f294504e0f8f83c","repo":"pentaho/pentaho-kettle","slug":"locale-sarg3-is-not-2-characters-long","errorCode":null,"errorMessage":"Locale \" + sArg3 + \" is not 2 characters long.","messagePattern":"Locale \" \\+ sArg3 \\+ \" is not 2 characters long\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1044,"sourceCode":"        }\n        break;\n      case 3:\n        try {\n          if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {\n            return null;\n          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {\n            return undefinedValue;\n          }\n          sArg1 = (String) ArgList[0];\n          Format dfFormatter;\n          sArg2 = (String) ArgList[1];\n          sArg3 = (String) ArgList[2];\n          if ( sArg3.length() == 2 ) {\n            Locale dfLocale = EnvUtil.createLocale( sArg3 );\n            dfFormatter = new SimpleDateFormat( sArg2, dfLocale );\n            oRC = dfFormatter.parseObject( sArg1 );\n          } else {\n            throw new RuntimeException( \"Locale \" + sArg3 + \" is not 2 characters long.\" );\n          }\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"Could not apply the local format for locale \"\n            + sArg3 + \" with the given format \" + sArg2 + \" on the string for \" + sArg1 + \" : \" + e.getMessage() );\n        }\n        break;\n      case 4:\n        try {\n          if ( isNull( ArgList, new int[] { 0, 1, 2, 3 } ) ) {\n            return null;\n          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2, 3 } ) ) {\n            return undefinedValue;\n          }\n          sArg1 = (String) ArgList[0];\n          DateFormat dfFormatter;\n          sArg2 = (String) ArgList[1];\n          sArg3 = (String) ArgList[2];\n          sArg4 = (String) ArgList[3];","sourceCodeStart":1026,"sourceCodeEnd":1062,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1026-L1062","documentation":"Thrown by the str2date JavaScript function implementation in ScriptAddedFunctions when the third argument (a locale code) is not exactly 2 characters long. The library requires a 2-letter language code (e.g. \"en\", \"de\") to build a java.util.Locale via EnvUtil.createLocale before constructing a SimpleDateFormat. It fails fast with this message rather than passing a malformed locale to the JDK.","triggerScenarios":"Calling str2date(dateString, format, locale) in a JavaScript (Rhino/JS) script step where ArgList[2] has length != 2, e.g. str2date('2024/01/01','yyyy/MM/dd','en_US') or 'enu' or '' or null-length strings.","commonSituations":"Developers copy full BCP-47 tags like 'en-US' or 'en_US' instead of the 2-letter language code; they pass a country code ('US') or a 3-letter code ('eng'); the value comes from a variable/field that is empty or null.","solutions":["Pass a 2-letter language code as the third argument, e.g. str2date(str,'yyyy/MM/dd','en') instead of 'en_US'.","Strip a regional suffix before calling: locale.substring(0,2) if the value looks like 'en_US'.","Verify the variable/field feeding the locale argument is populated and exactly 2 characters.","If you need locale+country behavior, note this API only accepts the language code; pre-normalize or use a different parsing approach."],"exampleFix":"// before (JS in a Modified Java Script Value step)\nvar d = str2date('2024-01-01', 'yyyy-MM-dd', 'en_US');\n// after\nvar d = str2date('2024-01-01', 'yyyy-MM-dd', 'en');","handlingStrategy":"validation","validationCode":"function validLocaleArg(loc) { return typeof loc === 'string' && loc.length === 2; }\nif (!validLocaleArg(loc)) throw new Error('locale must be a 2-letter code, got: ' + loc);","typeGuard":"function isTwoLetterLocale(v) { return typeof v === 'string' && /^[a-zA-Z]{2}$/.test(v); }","tryCatchPattern":"try { var d = str2date(s, fmt, loc); } catch (e) { if (String(e.message).indexOf('is not 2 characters long') >= 0) { loc = String(loc).substring(0,2); d = str2date(s, fmt, loc); } else { throw e; } }","preventionTips":["Always pass 2-letter ISO 639-1 codes ('en','de','fr') as the locale argument","Strip region suffixes ('en_US' -> 'en') before calling str2date","Validate the locale variable length before the call","Never pass null/empty strings as the locale argument"],"tags":["javascript","locale","date-parsing","scripting"],"backgroundTag":"invalid-argument-value","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}