Skip to content

Commit

Permalink
Lang name generator from cldr and using it (#290)
Browse files Browse the repository at this point in the history
* CLDR locale display names: generate data. Update Node and ICU4J

* Working for language names, not locale names

* Locale names now working without variants

* Rename langnames.rs --> localenames.rs

* Updating locale display names with dialect and standard

* Move CPP LangNames to LocaleDisplayNames

* Cleaning up some code

* Rename node langNames to localeDisplayNames

* Renaming node langnames stuff

* Updated based on suggestions
  • Loading branch information
sven-oly authored Aug 26, 2024
1 parent e22373b commit 3dd5903
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 98 deletions.
2 changes: 1 addition & 1 deletion executors/cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TARGET=executor

# All object files (C or C++)

OBJECTS=main.o coll.o datetime_fmt.o langnames.o likely_subtags.o list_fmt.o message_fmt2.o number_fmt.o plural_rules.o relativedatetime_fmt.o util.o
OBJECTS=main.o coll.o datetime_fmt.o localedisplaynames.o likely_subtags.o list_fmt.o message_fmt2.o number_fmt.o plural_rules.o relativedatetime_fmt.o util.o

#### rules
# Load in standard makefile definitions
Expand Down
66 changes: 0 additions & 66 deletions executors/cpp/langnames.cpp

This file was deleted.

82 changes: 82 additions & 0 deletions executors/cpp/localedisplaynames.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/********************************************************************
* testing icu4c for locale display names
*/

#include <json-c/json.h>

#include <unicode/utypes.h>

#include <unicode/locdspnm.h>
#include <unicode/uldnames.h>
#include <unicode/unistr.h>

#include <string>
#include <cstring>

#include "./util.h"

using icu::Locale;
using icu::UnicodeString;
using icu::LocaleDisplayNames;

const string TestLocaleDisplayNames (json_object *json_in) {
UErrorCode status = U_ZERO_ERROR;

json_object *label_obj = json_object_object_get(json_in, "label");
string label_string = json_object_get_string(label_obj);

// The locale in which the name is given.
json_object *locale_label_obj =
json_object_object_get(json_in, "locale_label");
string locale_string = json_object_get_string(locale_label_obj);

// The locales's name to be displayed.
json_object *displayed_locale_label_obj = json_object_object_get(
json_in, "language_label");
string displayed_locale_label_string =
json_object_get_string(displayed_locale_label_obj);

// Either standard or dialect names for the locale.
string language_display_string = "standard";
json_object *language_display_obj = json_object_object_get(
json_in, "languageDisplay");
if (language_display_obj) {
language_display_string = json_object_get_string(language_display_obj);
}

// In what language to show the locale name
Locale displayLocale(locale_string.c_str());

// The id of the locale to be formatted.
Locale testLocale(displayed_locale_label_string.c_str());

// Create display names object with the kind of locale name.
// Default is "standard".
UDialectHandling display_handling = ULDN_STANDARD_NAMES;
if (language_display_string == "dialect") {
display_handling = ULDN_DIALECT_NAMES;
}

LocaleDisplayNames* ldn =
LocaleDisplayNames::createInstance(displayLocale, display_handling);

// Get the resulting string for this testLocale
UnicodeString locale_name_result;
ldn->localeDisplayName(testLocale, locale_name_result);
delete ldn;

string result_string;
locale_name_result.toUTF8String(result_string);

// Create the output with label and resulting locale name.
json_object *return_json = json_object_new_object();
json_object_object_add(return_json, "label", label_obj);


json_object_object_add(return_json,
"result",
json_object_new_string(result_string.c_str()));

string return_string = json_object_to_json_string(return_json);
return return_string;
}
4 changes: 2 additions & 2 deletions executors/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ using std::string;
// Test functions
extern const string TestCollator(json_object *json_in);
extern const string TestDatetimeFmt(json_object *json_in);
extern const string TestLangNames(json_object *json_in);
extern const string TestLocaleDisplayNames(json_object *json_in);
extern const string TestLikelySubtags(json_object *json_in);
extern const string TestListFmt(json_object *json_in);

Expand Down Expand Up @@ -127,7 +127,7 @@ int main(int argc, const char** argv) {
} else if (test_type == "list_fmt") {
outputLine = TestListFmt(json_input);
} else if (test_type == "lang_names") {
outputLine = TestLangNames(json_input);
outputLine = TestLocaleDisplayNames(json_input);
} else if (test_type == "plural_rules") {
outputLine = TestPluralRules(json_input);
} else if (test_type == "rdt_fmt") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.unicode.conformance.testtype.langnames;

public enum LangNamesDisplayOptions {
STANDARD,
DIALECT; // ULDN_DIALECT_NAMES is the ICU4C enum

public static org.unicode.conformance.testtype.langnames.LangNamesDisplayOptions DEFAULT = STANDARD;

public static org.unicode.conformance.testtype.langnames.LangNamesDisplayOptions getFromString(String s) {
try {
return org.unicode.conformance.testtype.langnames.LangNamesDisplayOptions.valueOf(s.toUpperCase());
} catch (Exception e){
return DEFAULT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public class LangNamesInputJson implements ITestTypeInputJson {

public String locale_label;

public LangNamesDisplayOptions language_display; // For standard or dialect

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public ITestTypeInputJson inputMapToJson(Map<String, Object> inputMapData) {
result.language_label = (String) inputMapData.get("language_label", null);
result.locale_label = (String) inputMapData.get("locale_label", null);

String lang_display_string = (String) inputMapData.get("languageDisplay", null);
result.language_display = LangNamesDisplayOptions.getFromString(lang_display_string);

return result;
}

Expand Down Expand Up @@ -71,6 +74,10 @@ public String formatOutputJson(ITestTypeOutputJson outputJson) {
public String getDisplayLanguageString(LangNamesInputJson input) {
String localeID = input.language_label;
String displayLocaleID = input.locale_label;
return ULocale.getDisplayNameWithDialect(localeID, displayLocaleID);
if (input.language_display == LangNamesDisplayOptions.STANDARD) {
return ULocale.getDisplayName(localeID, displayLocaleID);
} else {
return ULocale.getDisplayNameWithDialect(localeID, displayLocaleID);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@ public void testLocaleAndDisplayLocale() {
assertEquals("Französisch", output.result);
}

@Test
public void testLocaleNameStandardDutchBelgium() {
String testInput =
"{\"test_type\": \"lang_names\", \"label\": \"nl1\", \"language_label\": \"nl-BE\", \"locale_label\": \"en\", \"languageDisplay\": \"standard\"}";

LangNamesOutputJson output =
(LangNamesOutputJson) LangNamesTester.INSTANCE.getStructuredOutputFromInputStr(testInput);

assertEquals("Dutch (Belgium)", output.result);
}

@Test
public void testLocaleNameDialectFlemish() {
String testInput =
"{\"test_type\": \"lang_names\", \"label\": \"nl2\", \"language_label\": \"nl-BE\", \"locale_label\": \"en\", \"languageDisplay\": \"dialect\"}";

LangNamesOutputJson output =
(LangNamesOutputJson) LangNamesTester.INSTANCE.getStructuredOutputFromInputStr(testInput);

assertEquals("Flemish", output.result);
}

}
8 changes: 4 additions & 4 deletions executors/node/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let numberformatter = require('./numberformat.js');

let displaynames = require('./displaynames.js');

let langnames = require('./langnames.js');
let localedisplaynames = require('./localedisplaynames.js')

let likely_subtags = require('./likely_subtags.js');

Expand Down Expand Up @@ -62,8 +62,8 @@ const testTypes = {
TestDateTimeFormat : Symbol("datetime_fmt"),
TestPluralRules : Symbol("plural_rules"),
TestDisplayNames : Symbol("display_names"),
TestLangNames : Symbol("language_display_name"),
TestListFmt : Symbol("list_fmt"),
TestLocaleDisplayNames : Symbol("language_display_name"),
TestRelativeDateTimeFormat : Symbol("rdt_fmt")
};

Expand Down Expand Up @@ -138,7 +138,7 @@ function parseJsonForTestId(parsed) {
}

if (testId == "language_display_name" || testId == "lang_names") {
return testTypes.TestLangNames;
return testTypes.TestLocaleDisplayNames;
}

if (testId == "datetime_fmt") {
Expand Down Expand Up @@ -231,7 +231,7 @@ rl.on('line', function(line) {
outputLine = displaynames.testDisplayNames(parsedJson);
} else
if (test_type == "language_display_name" || test_type == "lang_names") {
outputLine = langnames.testLangNames(parsedJson);
outputLine = localedisplaynames.testLocaleDisplayNames(parsedJson);
} else
if (test_type == "likely_subtags") {
outputLine = likely_subtags.testLikelySubtags(parsedJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = {

testLangNames: function (json) {
testLocaleDisplayNames: function (json) {
let locale = 'en'; // Default
let options = {};
if (json['locale_label']) {
Expand All @@ -15,9 +15,12 @@ module.exports = {
let label = json['label'];
let input = json['language_label'].replace(/_/g, '-');

if (json['languageDisplay']) {
// Fix to use dash, not underscore.
options['languageDisplay'] = json['languageDisplay'];
}

let outputLine;
//console.log("langnames input: " + input +
// " options: " + JSON.stringify(options) + " locale " + locale);

let dn;
try {
Expand All @@ -42,12 +45,12 @@ module.exports = {
"result": resultString
};
} catch (error) {
//console.log("LangName problem: input = " + input + ", error = " + error);
outputLine = {"label": json['label'],
"locale_label": locale,
"language_label": input,
"result": resultString,
"error": error.toString()
"error": error.toString(),
"actual_options": options.toString()
};
}
return outputLine;
Expand Down
Loading

0 comments on commit 3dd5903

Please sign in to comment.