{"record":{"id":"1c4a172112a3de7d","repo":"pentaho/pentaho-kettle","slug":"locale-is-not-2-characters-long","errorCode":null,"errorMessage":"Locale is not 2 characters long.","messagePattern":"Locale 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":1141,"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          java.util.Date dArg1 = (java.util.Date) ArgList[0];\n          DateFormat dfFormatter;\n          String sArg2 = (String) ArgList[1];\n          String sArg3 = (String) ArgList[2];\n          if ( sArg3.length() == 2 ) {\n            Locale dfLocale = EnvUtil.createLocale( sArg3.toLowerCase() );\n            dfFormatter = new SimpleDateFormat( sArg2, dfLocale );\n            oRC = dfFormatter.format( dArg1 );\n          } else {\n            throw new RuntimeException( \"Locale is not 2 characters long.\" );\n          }\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"Could not convert to the given local format.\" );\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          java.util.Date dArg1 = (java.util.Date) ArgList[0];\n          DateFormat dfFormatter;\n          String sArg2 = (String) ArgList[1];\n          String sArg3 = (String) ArgList[2];\n          String sArg4 = (String) ArgList[3];\n","sourceCodeStart":1123,"sourceCodeEnd":1159,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1123-L1159","documentation":"In the 3-argument date2str branch (date, format, locale), the locale argument must be exactly 2 characters. When it isn't, the code throws 'Locale is not 2 characters long.' before attempting to build the SimpleDateFormat with EnvUtil.createLocale.","triggerScenarios":"date2str(date, format, locale) with locale length != 2, e.g. date2str(d,'yyyy-MM-dd','en_US'), 'eng', or an empty string.","commonSituations":"Passing full locale tags or country codes; locale sourced from a variable that is empty; confusion with java.util.Locale constructors that accept language+country separately.","solutions":["Pass a 2-letter language code: date2str(d,'yyyy-MM-dd','de').","Truncate regional values with substring(0,2) when needed.","Validate the locale variable length before the call.","If you need country-specific formatting, note this API only accepts the 2-letter language code."],"exampleFix":"// before\nvar s = date2str(d, 'yyyy-MM-dd', 'de_DE');\n// after\nvar s = date2str(d, 'yyyy-MM-dd', 'de');","handlingStrategy":"validation","validationCode":"if (typeof loc !== 'string' || loc.length !== 2) throw new Error('date2str locale must be 2 chars, got: ' + loc);","typeGuard":"function isTwoLetterLocale(v) { return typeof v === 'string' && /^[a-zA-Z]{2}$/.test(v); }","tryCatchPattern":"try { var s = date2str(d, fmt, loc); } catch (e) { if (String(e.message).indexOf('not 2 characters long') >= 0) { s = date2str(d, fmt, String(loc).substring(0,2)); } else { throw e; } }","preventionTips":["Pass only 2-letter ISO 639-1 language codes","Normalize 'de_DE' style values with substring(0,2) before the call","Validate the locale variable is non-empty and exactly 2 chars","Note the API accepts language code only, not country"],"tags":["javascript","locale","date-formatting","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"}