diff --git a/add/data/xqm/eutil.xqm b/add/data/xqm/eutil.xqm index 1d0cf76e5..4bb09a610 100644 --- a/add/data/xqm/eutil.xqm +++ b/add/data/xqm/eutil.xqm @@ -244,10 +244,10 @@ declare function eutil:getPartLabel($measureOrPerfRes as node(), $type as xs:str : Returns a language specific string : : @param $key The key to search for - : @param $values The values to include into the string - : @return The string + : @param $values The values to replace the placeholders with (from the language string) + : @return The looked up language string from a language file :) -declare function eutil:getLanguageString($key as xs:string, $values as xs:string*) as xs:string { +declare function eutil:getLanguageString($key as xs:string, $values as xs:string*) as xs:string? { eutil:getLanguageString($key, $values, eutil:getLanguage('')) @@ -255,23 +255,57 @@ declare function eutil:getLanguageString($key as xs:string, $values as xs:string (:~ : Returns a language specific string from the locale/edirom-lang files + : NB, the project specific language dictionaries are not being queried here. + : Please use the four-arity version for this! : : @param $key The key to search for - : @param $values The values to include into the string - : @param $lang The language - : @return The string + : @param $values The values to replace the placeholders with (from the language string) + : @param $lang The language code (e.g. "de" or "en") + : @return The looked up language string from a language file :) -declare function eutil:getLanguageString($key as xs:string, $values as xs:string*, $lang as xs:string) as xs:string { +declare function eutil:getLanguageString($key as xs:string, $values as xs:string*, $lang as xs:string) as xs:string? { - let $base := system:get-module-load-path() - let $file := eutil:getDoc(concat($base, '/../locale/edirom-lang-', $lang, '.xml')) + let $file := eutil:getDoc(concat('../locale/edirom-lang-', $lang, '.xml')) - let $string := $file//entry[@key = $key]/string(@value) - let $string := functx:replace-multi($string, for $i in (0 to (count($values) - 1)) return concat('\{',$i,'\}'), $values) + let $langString := $file//entry[@key = $key]/string(@value) return - $string + if($langString) + (: replace placeholders in the language string with values provided to the function as parameter :) + then functx:replace-multi($langString, for $i in (0 to (count($values) - 1)) return concat('\{',$i,'\}'), $values) + else util:log('error', concat('Failed to find the key `', $key, '` in the Edirom default language file')) + +}; + +(:~ + : Returns a language specific string from the locale/edirom-lang files or project specific language files. + : The latter takes precedence. + : + : @param $edition The URI of the Edition's document to process + : @param $key The key to search for + : @param $values The values to replace the placeholders with (from the language string) + : @param $lang The language code (e.g. "de" or "en") + : @return The looked up language string from a language file + :) +declare function eutil:getLanguageString($edition as xs:string, $key as xs:string, $values as xs:string*, $lang as xs:string) as xs:string? { + (: Try to load a custom language file :) + let $langFileCustom := + try { doc(edition:getLanguageFileURI($edition, $lang)) } + catch * { util:log-system-out('Failed to load the custom language file for edition "' || $edition || '"') } + + let $langString := + (: If there is a value for the key in the custom language file :) + if($langFileCustom//entry/@key = $key) then + $langFileCustom//entry[@key = $key]/@value => string() + (: If not, take the value for the key in the default language file :) + else + eutil:getDoc(concat('../locale/edirom-lang-', $lang, '.xml'))//entry[@key = $key]/@value => string() + return + if($langString) + (: replace placeholders in the language string with values provided to the function as parameter :) + then functx:replace-multi($langString, for $i in (0 to (count($values) - 1)) return concat('\{',$i,'\}'), $values) + else util:log('error', concat('Failed to find the key `', $key, '` in any language file')) }; (:~ diff --git a/testing/XQSuite/data/edition.xml b/testing/XQSuite/data/edition.xml new file mode 100644 index 000000000..37f9ac6c9 --- /dev/null +++ b/testing/XQSuite/data/edition.xml @@ -0,0 +1,8 @@ + + + Edirom Edition Example + + + + + \ No newline at end of file diff --git a/testing/XQSuite/data/language-de.xml b/testing/XQSuite/data/language-de.xml new file mode 100644 index 000000000..a45db561f --- /dev/null +++ b/testing/XQSuite/data/language-de.xml @@ -0,0 +1,8 @@ + + de + 1.0 + + + + + \ No newline at end of file diff --git a/testing/XQSuite/data/language-it.xml b/testing/XQSuite/data/language-it.xml new file mode 100644 index 000000000..94c125aff --- /dev/null +++ b/testing/XQSuite/data/language-it.xml @@ -0,0 +1,8 @@ + + it + 1.0 + + + + + \ No newline at end of file diff --git a/testing/XQSuite/eutil-tests.xqm b/testing/XQSuite/eutil-tests.xqm index c6c3f8293..26da85137 100644 --- a/testing/XQSuite/eutil-tests.xqm +++ b/testing/XQSuite/eutil-tests.xqm @@ -49,3 +49,75 @@ declare function eut:test-getDoc($uri as xs:string?) as document-node()? { eutil:getDoc($uri) }; + +declare + (: Test empty replacements with non-existing key :) + %test:arg("key", "foo1g4#") + %test:arg("values") + %test:arg("lang", "de") %test:assertEmpty + (: Test empty replacements with existing key :) + %test:arg("key", "view.desktop.Desktop_Maximize") + %test:arg("values") + %test:arg("lang", "de") %test:assertEquals("Maximieren") + (: Test empty replacements with existing key in another language :) + %test:arg("key", "view.desktop.Desktop_Maximize") + %test:arg("values") + %test:arg("lang", "en") %test:assertEquals("Maximize") + (: Test replacements with existing key :) + %test:args("view.desktop.TaskBar_Desktop", "5", "de") %test:assertEquals("Desktop 5") + (: Test replacements with existing key in another language :) + %test:args("view.desktop.TaskBar_Desktop", "foo", "en") %test:assertEquals("Desktop foo") + (: Test replacements with existing key without placeholders :) + %test:args("view.desktop.Desktop_Maximize", "foo", "de") %test:assertEquals("Maximieren") + (: Test replacements with existing key and non-existing language :) + %test:args("view.desktop.Desktop_Maximize", "foo", "foo1g4#lang") %test:assertEmpty + (: Test empty replacements with non-existing key and non-existing language :) + %test:arg("key", "foo1g4#") + %test:arg("values") + %test:arg("lang", "foo1g4#lang") %test:assertEmpty + function eut:test-getLanguageString-3-arity($key as xs:string, $values as xs:string*, $lang as xs:string) as xs:string? { + eutil:getLanguageString($key, $values, $lang) +}; + +declare + (: Test empty replacements with non-existing key :) + %test:arg("edition", "edirom_edition_example") + %test:arg("key", "foo1g4#") + %test:arg("values") + %test:arg("lang", "de") %test:assertEmpty + (: Test empty replacements with existing key :) + %test:arg("edition", "edirom_edition_example") + %test:arg("key", "global_cancel") + %test:arg("values") + %test:arg("lang", "de") %test:assertEquals("Test-Abbrechen") + (: Test empty replacements with existing key in another language :) + %test:arg("edition", "edirom_edition_example") + %test:arg("key", "global_cancel") + %test:arg("values") + %test:arg("lang", "it") %test:assertEquals("Test-it-Abbrechen") + (: Test replacements with existing key :) + %test:args("edirom_edition_example", "view.desktop.TaskBar_Desktop", "5", "de") %test:assertEquals("Test-Desktop 5") + (: Test replacements with existing key in another language :) + %test:args("edirom_edition_example", "view.desktop.TaskBar_Desktop", "foo", "it") %test:assertEquals("Test-it-Desktop foo") + (: Test replacements with existing key without placeholders :) + %test:args("edirom_edition_example", "global_cancel", "foo", "de") %test:assertEquals("Test-Abbrechen") + (: Test replacements with existing key and non-existing language :) + %test:args("edirom_edition_example", "global_cancel", "foo", "foo1g4#lang") %test:assertEmpty + (: Test empty replacements with non-existing key and non-existing language :) + %test:arg("edition", "edirom_edition_example") + %test:arg("key", "foo1g4#") + %test:arg("values") + %test:arg("lang", "foo1g4#lang") %test:assertEmpty + (: Test empty replacements with existing key from default language file :) + %test:arg("edition", "edirom_edition_example") + %test:arg("key", "view.desktop.Desktop_Maximize") + %test:arg("values") + %test:arg("lang", "de") %test:assertEquals("Maximieren") + (: Test empty replacements with non-existing editionID – with existing key from default language file :) + %test:arg("edition", "foobar4#_edition") + %test:arg("key", "view.desktop.Desktop_Maximize") + %test:arg("values") + %test:arg("lang", "de") %test:assertEquals("Maximieren") + function eut:test-getLanguageString-4-arity($edition as xs:string, $key as xs:string, $values as xs:string*, $lang as xs:string) as xs:string? { + eutil:getLanguageString($edition, $key, $values, $lang) +}; \ No newline at end of file